What is .max_marginal_relevance_search()?
.max_marginal_relevance_search() implements the Maximum Marginal Relevance (MMR) algorithm to return a set of k documents that are collectively more informative than the top-k by pure similarity. The algorithm works in two stages: first, it embeds the query and retrieves the fetch_k most similar candidate documents by vector distance (just like similarity_search); second, it iteratively selects documents from the candidate set by balancing relevance to the query against redundancy with already-selected documents.
The trade-off between relevance and diversity is controlled by lambda_mult, a float between 0.0 and 1.0. lambda_mult=1.0 means pure similarity (identical to similarity_search); lambda_mult=0.0 means pure diversity (maximum spread from each other, ignoring relevance). In practice, values around 0.5 give a useful balance for RAG — you get documents that are relevant to the query but cover different aspects of the topic rather than paraphrasing each other.
The fetch_k parameter determines the candidate pool size before the MMR selection step. A small fetch_k (say, equal to k) leaves the algorithm little room to find diverse alternatives. The recommended rule of thumb is fetch_k = 3× to 5× k — for k=4 results, fetch_k=20 is a good starting point. Not every vector store backend implements MMR natively: Chroma, FAISS, Pinecone, and Weaviate support it; some smaller backends fall back to post-hoc reranking on the Python side, which can be slower.
Use Cases
- • Diverse RAG
- • Redundancy reduction
- • Varied information
- • Coverage improvement
- • Multi-faceted search
- • Better context
Key Features
- ✓ Diversity balancing
- ✓ Relevance preservation
- ✓ fetch_k optimization
- ✓ Parameter tuning
- ✓ Better coverage
- ✓ Intelligent ranking
When NOT to Use
When similarity is paramount. For single-best-match searches.
Notes
fetch_k should be 3–5× k for good diversity
Setting fetch_k too close to k leaves the MMR algorithm no room to find diverse alternatives — it just picks from the same top-k candidates. A ratio of fetch_k = 3× k is the minimum useful starting point; 5× k gives more diversity without meaningfully increasing latency for most hosted vector stores.
lambda_mult=0.5 is a safe default — tune per use case
For RAG pipelines where topic coverage matters (long-form Q&A, research summaries), lower lambda_mult values (0.3–0.5) produce better results. For precise factual retrieval where the best single match is what you need, use similarity_search() instead — MMR overhead is wasted there.
Not all vector stores implement MMR natively
Chroma, FAISS, Pinecone, and Weaviate have native MMR implementations. Some other backends implement it in Python by fetching fetch_k documents and running the selection algorithm locally — this is slower and loads more vectors into memory. Check your vector store's documentation to know which approach it uses.
Use as_retriever(search_type="mmr") to access MMR in LCEL chains
The Runnable retriever returned by .as_retriever(search_type="mmr", search_kwargs={...}) is composable with LCEL pipes, RunnableParallels, and RetrievalQA. This is the idiomatic way to plug MMR into a production RAG chain without calling max_marginal_relevance_search() directly.
Method Signature
docs = vector_store.max_marginal_relevance_search(query, k=4, fetch_k=20)
Parameters
| Parameter | Type | Required | Purpose |
|---|---|---|---|
| query | str | Yes | Query text |
Return Value
Type:
List[Document]
Description:
k diverse documents
Example Output:
[Document(...), Document(...), ...]
Code Examples
Basic MMR search
docs = vector_store.max_marginal_relevance_search(
'What is AI?', k=4, fetch_k=20,
lambda_mult=0.5
)
MMR with metadata filter and high diversity
docs = vector_store.max_marginal_relevance_search(
'machine learning applications',
k=6,
fetch_k=30,
lambda_mult=0.3, # 70% diversity weight
filter={'category': 'research'}
)
for doc in docs:
print(doc.metadata["source"])
MMR via as_retriever() for LCEL chains
from langchain_community.vectorstores import FAISS
retriever = vector_store.as_retriever(
search_type="mmr",
search_kwargs={"k": 5, "fetch_k": 25, "lambda_mult": 0.5}
)
docs = retriever.invoke('explain embeddings')
Common Mistakes
❌ Use similarity_search() when you need diversity
✅ Use max_marginal_relevance_search() for diverse results
Related LangChain References
Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with .max_marginal_relevance_search() and the wider framework.
.max_marginal_relevance_search() FAQ
What does .max_marginal_relevance_search() do in LangChain?
Search with diversity to reduce redundancy. .max_marginal_relevance_search() implements the Maximum Marginal Relevance (MMR) algorithm to return a set of k documents that are collectively more informative than the top-k by pure similarity. The algorithm works in two stages: first, it embeds the query and retrieves the fetch_k most similar candidate documents by vector distance (just like similarity_search); second, it iteratively selects documents from the candidate set by balancing relevance to the query against redun…
Which LangChain classes support .max_marginal_relevance_search()?
.max_marginal_relevance_search() is available on Vector stores. Pin your installed LangChain version and verify the method exists in that release before deploying.
When should I use .max_marginal_relevance_search()?
Use .max_marginal_relevance_search() when your LangChain chains, agents, or pipelines need the behavior described in this guide.
What does .max_marginal_relevance_search() return?
.max_marginal_relevance_search() returns a List[Document]. k diverse documents
Does .max_marginal_relevance_search() have an async equivalent?
.max_marginal_relevance_search() does not have a documented async variant. Avoid .max_marginal_relevance_search() When similarity is paramount. For single-best-match searches.
Where can I explore more LangChain API reference pages?
Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.