LangChain Retrievers
LangChain retrievers are Runnables that take a query string and return a list of relevant Documents. A vector store is the most common source, but any search system can be a retriever. Because retrievers are Runnables, they slot straight into LCEL chains.
From Vector Store to Retriever
retriever = store.as_retriever(search_kwargs={"k": 2})
docs = retriever.invoke("Do you deliver to Dubai?")
for d in docs:
print(d.page_content)Sample output: We ship to Pakistan, UAE and the UK. Shipping takes 3-5 business days.
Search Types
- similarity (default): the k closest chunks.
- mmr: Maximal Marginal Relevance, balances relevance with diversity to avoid near-duplicates.
- similarity_score_threshold: only return results above a relevance score.
MMR Example
mmr = store.as_retriever(
search_type="mmr",
search_kwargs={"k": 3, "fetch_k": 20, "lambda_mult": 0.5},
)
print(len(mmr.invoke("shipping")))Sample output: 3
fetch_k candidates are retrieved first, then k diverse ones are chosen. lambda_mult closer to 1 favours relevance, closer to 0 favours diversity.
A Simple RAG Chain with a Retriever
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_ollama import ChatOllama
def format_docs(docs):
return "\n\n".join(d.page_content for d in docs)
prompt = ChatPromptTemplate.from_template(
"Answer using only this context. If unsure, say you don't know.\n\n"
"Context:\n{context}\n\nQuestion: {question}"
)
rag = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt | ChatOllama(model="llama3.2", temperature=0) | StrOutputParser()
)
print(rag.invoke("Can I return an item after two weeks?"))Sample output: Yes. Returns are free within 30 days, so two weeks is fine.
The dictionary runs two branches: one retrieves and formats context, the other passes the question through. This is the classic two-step RAG pattern. The RAG tutorial on this site goes much deeper.
Custom Retrievers
Any function from string to list of Documents can be wrapped with RunnableLambda and used as a retriever, for example a SQL full-text search or an API call.
Frequently asked questions
What is the difference between a vector store and a retriever?
A vector store stores and searches embeddings. A retriever is the simpler interface (query in, documents out) that chains and agents use; a vector store can produce one with as_retriever().
What does k mean in a LangChain retriever?
It is the number of documents to return. Common values are 3 to 6.
When should I use MMR?
Use MMR when your top results are often near-duplicates, such as overlapping chunks from the same page.