EU law retrievers and tools for LangChain and LangGraph.
langchain-lexapi gives a LangChain app a retriever over EU legislation, one over Court of Justice case law, and three agent tools. Every document ends with a Source: line: CELEX number, article or ECLI, EUR-Lex link. It calls the LexAPI REST API with your key; a search costs 5 credits, and the free plan starts with 500. LexAPI is not affiliated with LangChain, EUR-Lex or the Publications Office.
pip install langchain-lexapi
from langchain_lexapi import LexAPILegislationRetriever
retriever = LexAPILegislationRetriever(k=3) # reads LEXAPI_API_KEY
for doc in retriever.invoke("right to erasure of personal data"):
print(doc.metadata.get("cite_celex"), doc.metadata.get("article_ref"))
print(doc.page_content) # the article, then its Source: linePrints each hit’s CELEX number and article, then the text ending in its Source: line.
Install langchain-lexapi.
The import is langchain_lexapi; it needs langchain-core 1.4 or later. Version 0.1.0. Set the LEXAPI_API_KEY environment variable, or pass api_key=.
LangChain retriever for EU legislation: EUR-Lex articles as documents.
LexAPILegislationRetriever searches EU legislation by meaning and returns one document per hit, each an article or other part of an act. With as_of, a hit is the version listed for that date, or a notice when LexAPI could not establish it.
from langchain_lexapi import LexAPILegislationRetriever
# The articles in the version LexAPI lists for 1 June 2019.
retriever = LexAPILegislationRetriever(k=3, as_of="2019-06-01")
for doc in retriever.invoke("right to erasure"):
print(doc.metadata.get("cite_celex"), doc.metadata.get("article_ref"))
print(doc.page_content)page_contentis the article text followed by aSource:line with the CELEX number, the article and the EUR-Lex link.metadatacarries the same fields:cite_celexis the act to cite. A consolidated text is labelled as such: it has no legal force, and only the act as published in the Official Journal is authentic.- When LexAPI could not establish a hit’s text for the date, the document is a notice with the reason, not the text, and
metadata["text_withheld"]is true. Do not index those.unresolved="drop"leaves them out. - A search costs 5 credits; with
as_of, up to 20.
CJEU case law retriever for LangChain.
LexAPICaseLawRetriever searches CJEU case law by meaning. A document’s text is the whole decision; max_chars cuts it and marks the cut.
from langchain_lexapi import LexAPICaseLawRetriever
retriever = LexAPICaseLawRetriever(k=2, max_chars=4000, court="Court of Justice")
docs, report = retriever.invoke_with_report("right to be forgotten and search engines")
for doc in docs:
print(doc.metadata.get("ecli"), doc.metadata.get("decision_date"))
print(report.units_charged, "credits charged")invoke_with_report returns the documents and what the call cost; plain invoke returns the documents only. A case-law search costs 5 credits.
LangGraph agent tools.
The tools are ordinary LangChain tools. create_agent builds a LangGraph agent from them; define model first, a chat model of your choice.
The agent examples also need pip install langchain (it brings LangGraph).
from langchain.agents import create_agent
from langchain_lexapi import (
LexAPILegislationAsOfTool,
LexAPIReadDocumentTool,
LexAPISearchLegislationTool,
)
agent = create_agent(
model, # any LangChain chat model that supports tool calling
tools=[LexAPISearchLegislationTool(), LexAPIReadDocumentTool(), LexAPILegislationAsOfTool()],
system_prompt="Answer from EU law. Cite the CELEX number of every source you use.",
)
result = agent.invoke(
{"messages": [{"role": "user", "content": "What did Article 17 GDPR say on 1 June 2019?"}]}
)
print(result["messages"][-1].content)In a graph of your own, the same tools go in a ToolNode:
from langgraph.prebuilt import ToolNode
from langchain_lexapi import (
LexAPILegislationAsOfTool,
LexAPIReadDocumentTool,
LexAPISearchLegislationTool,
)
tools = ToolNode([LexAPISearchLegislationTool(), LexAPIReadDocumentTool(), LexAPILegislationAsOfTool()])| Tool | Credits | What it does |
|---|---|---|
lexapi_search_legislation | 5; up to 20 with as_of | Articles of EU legislation by meaning, optionally the text for a date |
lexapi_read_document | 1, 4 or 5 by source | A document by CELEX number, or one article; within a character budget, the rest listed |
lexapi_legislation_as_of | 1, 4 or 5 by source | The version of an act listed for a date, and one provision of it |
A read costs 1 credit when LexAPI already holds the document, 4 or 5 when it is fetched from the source first. The as-of tool costs 1 credit when no text is fetched.
- A failed call raises
ToolException;handle_tool_error=Truehands its message to the model. - Each tool message carries the call’s report and each passage’s metadata in its artifact, so your code can read what the call cost.
What the package does not do.
- It does not call a language model, build prompts or chains, embed texts or cache results.
kis the number of hits asked for; fewer can come back.celex=filters the hits after the search.- It is a client of LexAPI, not of EUR-Lex: every call goes to the LexAPI REST API with your key and is billed like the endpoint it calls. In LlamaIndex instead: EU law in LlamaIndex.
Asked & answered.
N° 01Is there a LangChain integration for EU law?+
N° 02Does langchain-lexapi work with LangGraph?+
N° 03How does the model know which act a LangChain document comes from?+
N° 04What does a LangChain retriever return when the text for a date is not known?+
Keep building.
- Legal data API for AI agentsThe MCP server, hosted or local, the REST API and the SDKs, all on the same credit pool.
- European legal RAGSemantic search to article text with provenance, citations and the text for a date.
- EU law in LlamaIndexRetrievers over EU legislation and case law, and a tool spec for LlamaIndex agents.
- QuickstartA first request by curl, MCP, Python and TypeScript.
- API referenceEvery endpoint, parameter, response field and error.
- MCP serverSetup for Claude, Cursor and VS Code, and the tool list.
- PricingPlans, credit pools and what each call costs.
LexAPI returns source texts and their identifiers. It does not assess compliance and gives no legal advice.