EU law · 24 official languagesArticle-level JSON · typed citation graphMCP server · Claude, Cursor, VS CodeSDKs · Python, TypeScriptFree plan · 500 credits to start
Brussels · CET
Vol. II·Section S — Solutions Desk

§ LlamaIndex — For developers

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.

Create a free accountGitHubExample: the law on a date

LexAPI’s own packages for LlamaIndex, on PyPI
pip install llama-index-retrievers-lexapi llama-index-tools-lexapi
Python
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: line

Prints each hit’s CELEX number and article, then the text ending in its Source: line.

§ 01 — Install

Install the LlamaIndex packages.

You need: Python 3.11 or later and an API key

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=.

§ 02 — Retrieve

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.

Python
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.
  • metadata carries the citation fields; cite_celex is 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.
§ 03 — Query engine

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.

Python
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?"))

The retrieval it wraps, over REST

§ 04 — Case law

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.

Python
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.

§ 05 — Agent

Agent tools, as a tool spec.


LexAPIToolSpec gives an agent the tools below, each with an async variant; llm is your LlamaIndex LLM.

Python
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())
ToolCreditsWhat it does
lexapi_search_legislation5; up to 20 with as_ofArticles of EU legislation by meaning, optionally the text for a date
lexapi_read_document1, 4 or 5 by sourceA document by CELEX number, or one article; within a character budget, the rest listed
lexapi_legislation_as_of1, 4 or 5 by sourceThe 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.
§ 06 — Scope

What the packages do not do.


  • similarity_top_k is 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.
§ 07 — FAQ

Asked & answered.

N° 01Is there a LlamaIndex integration for EU law?+
LexAPI publishes two packages on PyPI: llama-index-retrievers-lexapi, with retrievers over EU legislation and Court of Justice case law, and llama-index-tools-lexapi, a tool spec for agents. They call the LexAPI REST API and need a LexAPI API key. LexAPI is not affiliated with LlamaIndex, EUR-Lex or the Publications Office.
N° 02Can I use the LexAPI retriever in a LlamaIndex query engine?+
Yes. RetrieverQueryEngine.from_args(retriever, llm=...) answers from the nodes the retriever returns, with the LLM of your choice. Each node’s text ends with a Source: line naming the CELEX number and the article (for case law, the ECLI).
N° 03Which tools does LexAPIToolSpec give a LlamaIndex agent?+
Three: lexapi_search_legislation, lexapi_read_document and lexapi_legislation_as_of, each with an async variant. to_tool_list() hands them to a FunctionAgent or any other agent that takes tools.
N° 04Can I store the LexAPI nodes in my own vector index?+
Nodes with text can be indexed under their id, which is stable across calls. Do not index a node whose metadata has text_withheld set to true: it holds a notice, not the text. Metadata is excluded from embeddings.
§ 08 — Next

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.