Cohere LangChain Integration
langchain-cohere is an integration package that connects Cohere's powerful language AI models (Chat, Embeddings, Rerank) with the LangChain framework. It simplifies the process of building LLM-powered applications using Cohere's capabilities. The library is actively developed, with frequent releases often occurring on a weekly or bi-weekly cadence, and the current version is 0.5.0.
Warnings
- breaking The `create_csv_agent` abstraction was deprecated in v0.3.5 and subsequently removed in v0.4.1. Users relying on this function will need to refactor their code.
- breaking Cohere integrations were moved from `langchain_community` (and other `langchain` paths) to the dedicated `langchain-cohere` package. Direct imports from `langchain_community.chat_models.ChatCohere`, `langchain.embeddings.CohereEmbeddings`, or `langchain.retrievers.document_compressors.CohereRerank` are deprecated and will break in newer `langchain` versions.
- breaking The Cohere Chat and Rerank APIs migrated to v2 starting from v0.4.1. This introduced significant changes to message structure (using a single `messages` parameter with roles), chat history handling, tool definitions (JSON schema), and requires the `model` parameter to be explicitly specified for all calls.
- deprecated The `create_cohere_tools_agent` and general `connectors` functionality were deprecated in v0.3.3.
- gotcha The `langchain-cohere` package now manages the compatible `cohere` Python SDK version (v5+). Using an older or incompatible `cohere` SDK version installed separately may lead to unexpected behavior or errors.
Install
-
pip install langchain-cohere
Imports
- ChatCohere
from langchain_cohere import ChatCohere
- CohereEmbeddings
from langchain_cohere import CohereEmbeddings
- CohereRerank
from langchain_cohere import CohereRerank
- create_cohere_react_agent
from langchain_cohere.react_multi_hop.agent import create_cohere_react_agent
Quickstart
import os
from langchain_cohere import ChatCohere
from langchain_core.messages import HumanMessage
# Ensure your COHERE_API_KEY is set as an environment variable
# or pass it directly to the ChatCohere constructor.
# For example: cohere_api_key="your_api_key"
llm = ChatCohere(
model="command-r-plus", # or another supported Cohere chat model
temperature=0,
cohere_api_key=os.environ.get('COHERE_API_KEY', '')
)
messages = [HumanMessage(content="What is the capital of Canada?")]
try:
response = llm.invoke(messages)
print(response.content)
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure COHERE_API_KEY is set and the model name is correct.")