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.

PYTHON
1from langchain_cohere import ChatCohere
2from langchain_core.messages import AIMessage, HumanMessage
3
4# Define the Cohere LLM
5llm = ChatCohere(
6 cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
7)
8
9# Send a chat message without chat history
10current_message = [HumanMessage(content="knock knock")]
11print(llm.invoke(current_message))
12
13# Send a chat message with chat history, note the last message is the current user message
14current_message_and_history = [
15 HumanMessage(content="knock knock"),
16 AIMessage(content="Who's there?"),
17 HumanMessage(content="Tank"),
18]
19print(llm.invoke(current_message_and_history))

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.

PYTHON
1from langchain_cohere import ChatCohere
2from langchain_core.messages import HumanMessage
3
4# Define the Cohere LLM
5llm = ChatCohere(
6 cohere_api_key="COHERE_API_KEY",
7 model="command-a-reasoning-08-2025",
8)
9
10response = llm.invoke(
11 [
12 HumanMessage(
13 content="Alice has 3 brothers and 2 sisters. How many sisters does Alice's brother have?"
14 )
15 ]
16)
17
18# The reasoning model returns its content as a list of blocks: a "reasoning"
19# block (the model's reasoning) followed by a "text" block (the final answer).
20for block in response.content:
21 if block["type"] == "reasoning":
22 for step in block["summary"]:
23 print("Reasoning:", step["text"])
24 elif block["type"] == "text":
25 print("Answer:", block["text"])

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.

PYTHON
1from langchain_cohere import ChatCohere
2from langchain_core.messages import HumanMessage
3
4# Define the Cohere LLM
5llm = ChatCohere(
6 cohere_api_key="COHERE_API_KEY",
7 model="command-a-vision-07-2025",
8)
9
10# Use a publicly accessible image URL (a base64 data URI also works)
11image_url = "https://raw.githubusercontent.com/cohere-ai/cohere-developer-experience/main/fern/assets/images/waste-management-request.png"
12
13# Pass the image alongside a text prompt
14message = HumanMessage(
15 content=[
16 {"type": "text", "text": "What is shown in this image?"},
17 {"type": "image_url", "image_url": {"url": image_url}},
18 ]
19)
20
21print(llm.invoke([message]).content)

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.)

PYTHON
1import os
2
3from langchain.agents import create_agent
4from langchain_cohere import ChatCohere
5from langchain_tavily import TavilySearch
6
7# Internet search tool. Replace the placeholder with your Tavily API key.
8os.environ["TAVILY_API_KEY"] = "TAVILY_API_KEY"
9internet_search = TavilySearch()
10
11# Define the Cohere LLM
12llm = ChatCohere(
13 cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
14)
15
16# Create an agent with the internet search tool
17agent = create_agent(llm, tools=[internet_search])
18
19# Run the agent
20result = agent.invoke(
21 {"messages": [("user", "I want to write an essay. Any tips?")]}
22)
23
24# See Cohere's response
25print(result["messages"][-1].content)

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:

PYTHON
1import wikipedia
2
3from langchain_cohere import ChatCohere
4from langchain_community.retrievers import WikipediaRetriever
5
6# Wikipedia requires a descriptive User-Agent; set one before querying.
7wikipedia.set_user_agent("my-app/1.0 (you@example.com)")
8
9# User query we will use for the generation
10user_query = "What is Cohere?"
11
12# Define the Cohere LLM
13llm = ChatCohere(
14 cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
15)
16
17# Retrieve documents with any LangChain retriever
18wiki_retriever = WikipediaRetriever()
19wiki_docs = wiki_retriever.invoke(user_query)
20
21# Ground the answer in the retrieved documents
22response = llm.invoke(user_query, documents=wiki_docs)
23
24# Print the answer
25print("Answer:")
26print(response.content)
27# Print the citations that ground the answer in the documents
28print("Citations:")
29print(response.additional_kwargs.get("citations"))

Using Documents

In this example, we take documents (which might be generated in other parts of your application) and pass them to ChatCohere directly:

PYTHON
1from langchain_cohere import ChatCohere
2from langchain_core.documents import Document
3
4# Define the Cohere LLM
5llm = ChatCohere(
6 cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
7)
8
9# Supply your own documents (these might come from elsewhere in your application)
10documents = [
11 Document(
12 page_content="LangChain supports Cohere RAG!",
13 metadata={"id": "id-1"},
14 ),
15 Document(
16 page_content="The sky is blue!", metadata={"id": "id-2"}
17 ),
18]
19
20# Ground the answer in the documents
21response = llm.invoke(
22 "Does LangChain support Cohere RAG?", documents=documents
23)
24
25# Print the answer
26print("Answer:")
27print(response.content)
28# Print the citations that ground the answer in the documents
29print("Citations:")
30print(response.additional_kwargs.get("citations"))

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.

PYTHON
1import os
2
3from langchain_cohere import ChatCohere
4from langchain_core.messages import HumanMessage, ToolMessage
5from langchain_tavily import TavilySearch
6
7# Web search tool. Replace the placeholder with your Tavily API key.
8os.environ["TAVILY_API_KEY"] = "TAVILY_API_KEY"
9web_search = TavilySearch()
10
11# Define the Cohere LLM and bind the search tool
12llm = ChatCohere(
13 cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
14)
15llm_with_tools = llm.bind_tools([web_search])
16
17# 1. Force a search on the first turn so the answer is grounded
18messages = [HumanMessage("Who founded Cohere?")]
19ai_message = llm_with_tools.invoke(messages, tool_choice="REQUIRED")
20messages.append(ai_message)
21
22# 2. Run the search and pass the results back to the model
23for tool_call in ai_message.tool_calls:
24 results = web_search.invoke(tool_call["args"])
25 messages.append(
26 ToolMessage(
27 content=str(results), tool_call_id=tool_call["id"]
28 )
29 )
30
31# 3. The model answers, grounded in the search results
32final = llm_with_tools.invoke(messages)
33print("Answer:", final.content)
34# Cohere returns citations that ground the answer in the search results
35# (`.get` avoids a KeyError if the answer came back without any citations)
36print("Citations:", final.additional_kwargs.get("citations"))

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.

PYTHON
1from langchain_cohere import ChatCohere
2from langchain_core.documents import Document
3from langchain_core.prompts import ChatPromptTemplate
4from langchain_classic.chains.combine_documents import (
5 create_stuff_documents_chain,
6)
7
8prompt = ChatPromptTemplate.from_messages(
9 [("human", "What are everyone's favorite colors:\n\n{context}")]
10)
11
12# Define the Cohere LLM
13llm = ChatCohere(
14 cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
15)
16
17chain = create_stuff_documents_chain(llm, prompt)
18
19docs = [
20 Document(page_content="Jesse loves red but not yellow"),
21 Document(
22 page_content="Jamal loves green but not as much as he loves orange"
23 ),
24]
25
26res = chain.invoke({"context": docs})
27print(res)

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.

PYTHON
1from langchain_cohere import ChatCohere
2
3# Define the Cohere LLM
4llm = ChatCohere(
5 cohere_api_key="COHERE_API_KEY", model="command-a-03-2025"
6)
7
8res = llm.invoke(
9 "John is five years old",
10 response_format={
11 "type": "json_object",
12 "schema": {
13 "title": "Person",
14 "description": "Identifies the age and name of a person",
15 "type": "object",
16 "properties": {
17 "name": {
18 "type": "string",
19 "description": "Name of the person",
20 },
21 "age": {
22 "type": "number",
23 "description": "Age of the person",
24 },
25 },
26 "required": [
27 "name",
28 "age",
29 ],
30 },
31 },
32)
33
34print(res)

Text Summarization

You can use the load_summarize_chain chain to perform text summarization.

PYTHON
1from langchain_cohere import ChatCohere
2from langchain_classic.chains.summarize import load_summarize_chain
3from langchain_community.document_loaders import WebBaseLoader
4
5loader = WebBaseLoader("https://docs.cohere.com/docs/cohere-toolkit")
6docs = loader.load()
7
8# Define the Cohere LLM
9llm = ChatCohere(
10 cohere_api_key="COHERE_API_KEY",
11 model="command-a-03-2025",
12 temperature=0,
13)
14
15chain = load_summarize_chain(llm, chain_type="stuff")
16
17result = chain.invoke({"input_documents": docs})
18print(result["output_text"])

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
1llm = ChatCohere(
2 base_url="<YOUR_DEPLOYMENT_URL>",
3 cohere_api_key="COHERE_API_KEY",
4 model="MODEL_NAME",
5)