tezvyn:

Self-Querying Retriever: Let an LLM Write Its Own Filters

AI-drafted, machine-checkedSource: reference.langchain.comadvanced

A self-querying retriever uses an LLM to turn a natural language question into a structured query with metadata filters. It lets users ask things like "Find documents about Python from before 2020," which a simple vector search can't do.

WHY IT EXISTS: Standard vector search excels at finding semantically similar content but is blind to structured metadata. You can't ask it to find documents "from last year" or "by a specific author" using natural language. The self-querying retriever was created to bridge this gap, allowing a user's conversational question to control precise database filters.

THE MENTAL MODEL: Think of a self-querying retriever as a research assistant working with a librarian. You don't just ask for "books about space exploration." You ask for "books about space exploration published after 1990 by NASA authors." The assistant (the LLM) deconstructs your request into a precise instruction card: Topic: 'space exploration', Filter 1: 'year > 1990', Filter 2: 'author contains NASA'. It then hands this card to the librarian (the vector store), who can now perform a highly specific search.

HOW IT WORKS: When a user submits a query, it's first sent to an LLM. The LLM, guided by a specific prompt, parses the text to extract two key components: the core semantic concept for the vector search and any metadata filters mentioned. For example, in "What did our docs say about authentication in Q4?", the query is "authentication" and the filter is "quarter = Q4". This structured output is then passed to a translator that converts it into the specific syntax the underlying vector store requires to execute a filtered search.

WHEN TO USE IT: Use a self-querying retriever when your documents have rich, structured metadata and you want to let users filter on it with natural language. It's ideal for RAG systems built on product catalogs, legal case databases, or any document set where attributes like date, author, or status are crucial for finding the right information.

WHEN NOT TO USE IT: Avoid this pattern if your documents lack consistent metadata, as the LLM will have nothing to filter on. It's also overkill for simple semantic search applications where no filtering is needed. The added latency and cost of the LLM call can be a significant drawback if performance is critical and queries are simple.

ONE CANONICAL EXAMPLE: In a LangChain application, a user might ask, "Find me summaries of movies from the 1990s that mention 'time travel'." The SelfQueryRetriever uses an LLM to generate a structured query like { "query": "time travel", "filter": { "year": { "gte": 1990, "lt": 2000 } } }. This is then executed against a vector store like Chroma or Pinecone that supports metadata filtering, returning only the relevant movie summaries.

Read the original → reference.langchain.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.