LangChain Retrievers

A retriever answers one question: which pieces of my data match this query? It takes a text question and returns a short list of relevant documents. Retrievers act as the search engine inside every document-based AI application, and they feed the model the right facts at the right time.

Librarian Analogy

Imagine asking a librarian for books about monsoon farming. The librarian does not hand you the entire library. A skilled librarian checks the catalog and returns three useful books. A retriever does the same job for your text chunks.

 Question: "How does monsoon affect rice crops?"
                    |
                    v
             +-------------+
             |  Retriever  |  ← searches the knowledge base
             +-------------+
                    |
                    v
 [Chunk 12]  [Chunk 47]  [Chunk 8]   (top matches)

Retriever vs Vector Store

A vector store saves and searches embeddings. A retriever is a thin, standard layer on top. The retriever accepts a string and returns documents, and it hides how the search works. Many retrievers exist that do not use vector stores at all, such as keyword search or web search. Because every retriever follows one interface, you can swap them freely.

Creating a Retriever from a Vector Store

retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

docs = retriever.invoke("How does monsoon affect rice crops?")
for doc in docs:
    print(doc.page_content[:100])
    print(doc.metadata)

The setting k controls how many documents return. A small k keeps prompts short. A large k improves coverage and raises cost.

Search Types

Similarity Search

The default mode returns the chunks closest in meaning to the question. The top results sometimes repeat the same idea.

MMR Search

Maximal Marginal Relevance balances relevance and variety. It picks a strong match first, then prefers chunks that add new information.

retriever = vectorstore.as_retriever(
    search_type="mmr",
    search_kwargs={"k": 4, "fetch_k": 20},
)

MMR fetches 20 candidates and selects 4 diverse ones.

Score Threshold Search

This mode drops weak matches. The retriever returns fewer documents, or none, when nothing is close enough.

retriever = vectorstore.as_retriever(
    search_type="similarity_score_threshold",
    search_kwargs={"score_threshold": 0.5, "k": 5},
)

Metadata Filtering

Every document carries metadata such as file name, year, or department. Filters narrow the search to documents that match the metadata before ranking begins. The exact filter format depends on the vector store. Here is the Chroma style.

retriever = vectorstore.as_retriever(
    search_kwargs={"k": 3, "filter": {"department": "finance"}},
)
 All 10,000 chunks
        |
        v
 Filter: department = finance  →  800 chunks
        |
        v
 Similarity search inside those 800  →  top 3

Retrievers as Runnables

A retriever plugs straight into a chain with the pipe symbol. This position makes retrievers easy to combine with prompts and models.

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

def join_docs(docs):
    return "\n\n".join(d.page_content for d in docs)

prompt = ChatPromptTemplate.from_template(
    "Answer using only this context:\n{context}\n\nQuestion: {question}"
)

chain = (
    {"context": retriever | join_docs, "question": RunnablePassthrough()}
    | prompt
    | model
    | StrOutputParser()
)

Tuning Tips

  • Test queries by hand and read the returned chunks before building the full app.
  • Start with k between 3 and 5.
  • Switch to MMR when results look repetitive.
  • Add metadata filters when users search inside one product, year, or team.
  • Improve chunk size first when results feel off. Retrievers can only return what the splitter created.

Real-World Scenario

A company stores two thousand pages of employee policies. A new hire asks about travel expense limits. The retriever searches the policy chunks, filters by the finance department, and returns three matches. The model reads only those three chunks and answers in plain words. Without the retriever, the model would guess or the app would paste two thousand pages into the prompt. The retriever acts as a fast, focused search desk between the question and the knowledge.

Practice Tasks

  1. Create a retriever with k equal to 2, then 5, and compare the returned chunks for one query.
  2. Switch to MMR search and note whether duplicates disappear.
  3. Add a metadata filter and confirm that results come only from the chosen source.
  4. Write down two queries that return poor results and guess why.

Key Points

  • A retriever turns a question into a list of matching documents.
  • as_retriever converts any vector store into a retriever.
  • Similarity, MMR, and threshold modes suit different needs.
  • Metadata filters narrow the search before ranking.
  • Retrievers plug directly into chains.