> This page is for version v2 API (default).
> For other versions, use one of these documentation indexes:
> - v2 API (default): https://docs.cohere.com/v2/llms.txt
> - v1 API: https://docs.cohere.com/v1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.cohere.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.cohere.com/_mcp/server.

# Cohere Rerank on LangChain (Integration Guide)

> This page describes how to integrate Cohere's ReRank models with LangChain.

Cohere supports various integrations with LangChain, a large language model (LLM) framework which allows you to quickly create applications based on Cohere's models. This doc will guide you through how to leverage Rerank with LangChain.

## Prerequisites

Running Cohere Rerank with LangChain doesn't require many prerequisites, consult the [top-level document](../docs/cohere-and-langchain) for more information.

## Cohere ReRank with LangChain

To use Cohere's [rerank functionality](../docs/reranking) with LangChain, start with instantiating a [CohereRerank](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/retrievers/document_compressors/cohere_rerank.py) object as follows: `cohere_rerank = CohereRerank(cohere_api_key="{API_KEY}")`.

You can then use it with LangChain retrievers, embeddings, and RAG. The example below uses the vector DB chroma, for which you will need to install `pip install chromadb`. Other vector DB's [from this list](https://python.langchain.com/docs/integrations/vectorstores) can also be used. After reranking, we pass the top documents to `ChatCohere` through its `documents` argument to get a grounded answer with citations.

**`PYTHON`**

```python PYTHON
from langchain_classic.retrievers import (
    ContextualCompressionRetriever,
)
from langchain_cohere import (
    ChatCohere,
    CohereEmbeddings,
    CohereRerank,
)
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.document_loaders import WebBaseLoader

user_query = "what is Cohere Toolkit?"

# Define the Cohere LLM
llm = ChatCohere(
    cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
)

# Define the Cohere embedding model
embeddings = CohereEmbeddings(
    cohere_api_key="COHERE_API_KEY", model="embed-english-light-v3.0"
)

# Load text and split into chunks, you can also use data gathered elsewhere in your application
raw_documents = WebBaseLoader(
    "https://docs.cohere.com/docs/cohere-toolkit"
).load()
text_splitter = CharacterTextSplitter(
    chunk_size=1000, chunk_overlap=0
)
documents = text_splitter.split_documents(raw_documents)

# Create a vector store from the documents
db = Chroma.from_documents(documents, embeddings)

# Create Cohere's reranker with the vector DB using Cohere's embeddings as the base retriever
reranker = CohereRerank(
    cohere_api_key="COHERE_API_KEY", model="rerank-v5.0-pro"
)

compression_retriever = ContextualCompressionRetriever(
    base_compressor=reranker, base_retriever=db.as_retriever()
)
compressed_docs = compression_retriever.invoke(user_query)
# Print the reranked documents from using the embeddings and reranker
print(compressed_docs)

# Ground the answer in the reranked documents
response = llm.invoke(user_query, documents=compressed_docs)

# Print the answer
print("Answer:")
print(response.content)
# Print the citations that ground the answer in the documents
print("Citations:")
print(response.additional_kwargs.get("citations"))
```

## Using LangChain on Private Deployments

You can use LangChain with privately deployed Cohere models. To use it, specify your model deployment URL in the `base_url` parameter.

**`PYTHON`**

```python PYTHON
llm = CohereRerank(
    base_url="<YOUR_DEPLOYMENT_URL>",
    cohere_api_key="COHERE_API_KEY",
    model="MODEL_NAME",
)
```