How to beginner · 3 min read

How to use CohereRerank in LangChain

Quick answer
Use CohereRerank in LangChain by importing CohereRerank from langchain.retrievers, initializing it with your Cohere API key, and calling rerank on a list of documents and a query. This enables efficient document reranking leveraging Cohere's reranking model.

PREREQUISITES

  • Python 3.8+
  • Cohere API key (free tier available)
  • pip install langchain cohere

Setup

Install the required packages and set your Cohere API key as an environment variable.

  • Install LangChain and Cohere SDK: pip install langchain cohere
  • Set environment variable COHERE_API_KEY with your Cohere API key.
bash
pip install langchain cohere

Step by step

Use CohereRerank from langchain.retrievers to rerank documents based on a query. Initialize the reranker with your API key, then call rerank passing the query and documents.

python
import os
from langchain.retrievers import CohereRerank

# Load API key from environment
api_key = os.environ["COHERE_API_KEY"]

# Initialize CohereRerank
reranker = CohereRerank(cohere_api_key=api_key)

# Example documents to rerank
documents = [
    "LangChain is a framework for building LLM apps.",
    "Cohere provides powerful NLP models.",
    "Python is a popular programming language."
]

# Query to rerank documents against
query = "What is LangChain?"

# Perform reranking
reranked_docs = reranker.rerank(query=query, documents=documents)

print("Reranked documents:")
for i, doc in enumerate(reranked_docs, 1):
    print(f"{i}. {doc}")
output
Reranked documents:
1. LangChain is a framework for building LLM apps.
2. Cohere provides powerful NLP models.
3. Python is a popular programming language.

Common variations

You can customize CohereRerank with parameters like model to specify the reranking model version. Async reranking is not natively supported in LangChain's CohereRerank as of now. You can also use CohereRerank as a retriever wrapper in LangChain chains for enhanced retrieval quality.

python
from langchain.retrievers import CohereRerank

# Specify a different model version
reranker = CohereRerank(cohere_api_key=os.environ["COHERE_API_KEY"], model="rerank-english-v2.0")

# Use reranker in a LangChain retrieval pipeline (example)
# retriever = SomeRetriever()
# reranked_retriever = CohereRerank(retriever=retriever, cohere_api_key=os.environ["COHERE_API_KEY"])

# Note: Async and streaming are not supported in CohereRerank currently.

Troubleshooting

  • If you get authentication errors, verify your COHERE_API_KEY environment variable is set correctly.
  • If reranking results seem incorrect, ensure your documents and query are properly formatted strings.
  • Check your network connectivity if API calls fail.

Key Takeaways

  • Initialize CohereRerank with your Cohere API key to enable document reranking in LangChain.
  • Call rerank(query, documents) to reorder documents by relevance to the query.
  • Customize the reranking model via the model parameter if needed.
  • Ensure your environment variable COHERE_API_KEY is set to avoid authentication errors.
Verified 2026-04 · rerank-english-v2.0
Verify ↗