Cohere Chat on LangChain (Integration Guide)
Cohere Chat on LangChain (Integration Guide)
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 Cohere Chat with LangChain.
Prerequisites
Running Cohere Chat with LangChain doesn’t require many prerequisites, consult the top-level document for more information.
Cohere Chat with LangChain
To use Cohere chat with LangChain, simply create a ChatCohere object and pass in the message or message history. In the example below, you will need to add your Cohere API key.
Reasoning with LangChain
When you select a reasoning model (like command-a-reasoning-08-2025), its response includes both the model’s reasoning and its final answer as structured content blocks—no extra parameters required. For background on how reasoning models work, see Cohere’s Reasoning guide.
Reasoning support requires langchain-cohere >= 0.5.1.
Image Inputs with LangChain
Command A Vision (command-a-vision-07-2025) can interpret images. Pass image content blocks using the image_url type—a publicly accessible image URL or a base64 data URI—together with your text prompt. For more on Cohere’s vision capabilities, see the Image Inputs guide.
Vision support requires langchain-cohere >= 0.6.0.
Cohere Agents with LangChain
An agent uses a language model to choose a sequence of actions to take. With LangChain v1, the idiomatic way to build one is create_agent from langchain.agents (it ships with langchain, so no extra install is needed). Pass in the LangChain tools you would like the agent to use.
In the example below we give the agent an internet search tool (Tavily) and let it decide when to call it, then print its answer. To run it, install the search tool with pip install langchain-tavily and set a TAVILY_API_KEY environment variable; you can swap in any other LangChain tool. (For grounded answers with citations, see the Web Search and RAG examples below.)
Cohere Chat and RAG with LangChain
To use Cohere’s retrieval augmented generation (RAG) functionality with LangChain, pass your documents to ChatCohere through its documents argument. Cohere grounds its answer in those documents and returns citations that point back to them. The next few sections show a couple of ways to supply the documents.
Using LangChain’s Retrievers
In this example, we use the wikipedia retriever but any retriever supported by LangChain can be used here. In order to set up the wikipedia retriever you need to install the wikipedia python package using %pip install --upgrade --quiet wikipedia. Wikipedia also requires a descriptive User-Agent, so set one with wikipedia.set_user_agent(...) before querying. With that done, you can execute this code to see how a retriever works:
Using Documents
In this example, we take documents (which might be generated in other parts of your application) and pass them to ChatCohere directly:
Web Search
To answer a query with up-to-date information from the web, give the model a search tool with tool use: the model requests a search, you run it and pass the results back, and Cohere grounds its answer in those results and returns citations. Set a TAVILY_API_KEY environment variable to run the example, or swap in any other search tool.
Using the create_stuff_documents_chain Chain
This chain takes a list of documents and formats them all into a prompt, then passes that prompt to an LLM. It passes ALL documents, so you should make sure it fits within the context window of the LLM you are using.
Note: this feature is currently in beta.
Structured Output Generation
Cohere supports generating JSON objects to structure and organize the model’s responses in a way that can be used in downstream applications.
You can specify the response_format parameter to indicate that you want the response in a JSON object format.
Text Summarization
You can use the load_summarize_chain chain to perform text summarization.
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.