EU law retrievers and agent tools for LlamaIndex.
llama-index-retrievers-lexapi and llama-index-tools-lexapi give a LlamaIndex app retrievers over EU legislation and Court of Justice case law, and a tool spec for agents. Every node ends with a Source: line: CELEX number, article or ECLI, EUR-Lex link. They call the LexAPI REST API with your key; a search costs 5 credits, and the free plan starts with 500. LexAPI is not affiliated with LlamaIndex, EUR-Lex or the Publications Office.
pip install llama-index-retrievers-lexapi llama-index-tools-lexapi
from llama_index.retrievers.lexapi import LexAPILegislationRetriever
retriever = LexAPILegislationRetriever(similarity_top_k=3) # reads LEXAPI_API_KEY
for hit in retriever.retrieve("right to erasure of personal data"):
print(hit.node.metadata.get("cite_celex"), hit.node.metadata.get("article_ref"))
print(hit.node.get_content()) # the article, then its Source: linePrints each hit’s CELEX number and article, then the text ending in its Source: line.
Install the LlamaIndex packages.
Install either or both. The imports are llama_index.retrievers.lexapi and llama_index.tools.lexapi. Version 0.1.0. Set the LEXAPI_API_KEY environment variable, or pass api_key=.
LlamaIndex retriever for EU legislation: EUR-Lex articles as nodes.
LexAPILegislationRetriever searches EU legislation by meaning; each hit becomes a TextNode with its score. With as_of, a hit is the version listed for that date, or a notice when LexAPI could not establish it.
from llama_index.retrievers.lexapi import LexAPILegislationRetriever
# The articles in the version LexAPI lists for 1 June 2019.
retriever = LexAPILegislationRetriever(similarity_top_k=3, as_of="2019-06-01")
for hit in retriever.retrieve("right to erasure"):
meta = hit.node.metadata
print(meta.get("cite_celex"), meta.get("article_ref"))
print(hit.node.get_content())- A node’s text is the article followed by a
Source:line with the CELEX number, the article and the EUR-Lex link. Its id is stable across calls. metadatacarries the citation fields;cite_celexis the act to cite. A consolidated text has no legal force: only the act as published in the Official Journal is authentic.- When LexAPI could not establish a hit’s text for the date, the node 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.
A query engine, with your LLM.
The retriever plugs into a query engine like any other. LexAPI does not call a model: llm is your LlamaIndex LLM.
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.retrievers.lexapi import LexAPILegislationRetriever
retriever = LexAPILegislationRetriever(similarity_top_k=5)
engine = RetrieverQueryEngine.from_args(retriever, llm=llm) # any LlamaIndex LLM
print(engine.query("When must a controller erase personal data?"))CJEU case law retriever for LlamaIndex.
LexAPICaseLawRetriever searches CJEU case law by meaning. A node’s text is the whole decision; max_chars cuts it and marks the cut.
from llama_index.retrievers.lexapi import LexAPICaseLawRetriever
retriever = LexAPICaseLawRetriever(similarity_top_k=2, max_chars=4000, court="Court of Justice")
for hit in retriever.retrieve("right to be forgotten and search engines"):
meta = hit.node.metadata
print(meta.get("ecli"), meta.get("decision_date"))Filters: court, date_from, date_to. A case-law search costs 5 credits.
Agent tools, as a tool spec.
LexAPIToolSpec gives an agent the tools below, each with an async variant; llm is your LlamaIndex LLM.
import asyncio
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.tools.lexapi import LexAPIToolSpec
async def main():
async with LexAPIToolSpec() as spec:
agent = FunctionAgent(
tools=spec.to_tool_list(),
llm=llm, # any LlamaIndex LLM that supports function calling
system_prompt="Answer from EU law. Cite the CELEX number of every source you use.",
)
print(await agent.run("What did Article 17 GDPR say on 1 June 2019?"))
asyncio.run(main())| 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.
- Every successful call sends an instrumentation event with the call’s report, so your code can read what it cost.
What the packages do not do.
similarity_top_kis the number of hits asked for; fewer can come back. The date is set per retriever: for another date, build another retriever.- They are clients 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 LangChain instead: EU law in LangChain.
Asked & answered.
N° 01Is there a LlamaIndex integration for EU law?+
N° 02Can I use the LexAPI retriever in a LlamaIndex query engine?+
N° 03Which tools does LexAPIToolSpec give a LlamaIndex agent?+
N° 04Can I store the LexAPI nodes in my own vector index?+
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 LangChainlangchain-lexapi: retrievers over EU legislation and case law, and agent tools for LangGraph.
- 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.