Migrating From API v1 to API v2
This guide serves as a reference for developers looking to update their code that uses Cohere API v1 in favor of the new v2 standard. It outlines the key differences and necessary changes when migrating from Cohere API v1 to v2 and the various aspects of the API, including chat functionality, RAG (Retrieval-Augmented Generation), and tool use. Each section provides code examples for both v1 and v2, highlighting the structural changes in request formats, response handling, and new features introduced in v2.
General
- v2:
modelis a required field for Embed, Rerank, Classify, and Chat.
Embed
- v2:
embedding_typesis a required field for Embed.
Chat
Messages
-
Message structure:
- v1: uses separate
preambleandmessageparameters. - v2: uses a single
messagesparameter consisting of a list of roles (system,user,assistant, ortool). Thesystemrole in v2 replaces thepreambleparameter in v1.
- v1: uses separate
-
Chat history:
- v1: manages the chat history via the
chat_historyparameter. - v2: manages the chat history via the
messageslist.
- v1: manages the chat history via the
v1
v2
Response content
- v1: Accessed via
text - v2: Accessed via
message.content[0].text
v1
v2
Streaming
-
Events containing content:
- v1:
chunk.event_type == "text-generation" - v2:
chunk.type == "content-delta"
- v1:
-
Accessing response content:
- v1:
chunk.text - v2:
chunk.delta.message.content.text
- v1:
v1
v2
RAG
Documents
- v1: the
documentsparameter supports a list of objects with multiple fields per document. - v2: the
documentsparameter supports a few different options for structuring documents:- List of objects with
dataobject: same as v1 described above, but each document passed as adataobject (with an optionalidfield to be used in citations). - List of objects with
datastring (with an optionalidfield to be used in citations). - List of strings.
- List of objects with
v1
v2
The following is a list of the the different options for structuring documents for RAG in v2.
Citations
- Citations access:
- v1:
citations - v2:
message.citations
- v1:
- Cited documents access:
- v1:
documents - v2: as part of
message.citations, in thesourcesfield
- v1:
v1
v2
Search query generation
- v1: Uses
search_queries_onlyparameter - v2: Supported via tools. We recommend using the v1 API for this functionality in order to leverage the
force_single_stepfeature. Support in v2 will be coming soon.
Connectors
- v1: Supported via the
connectorsparameter - v2: Supported via user-defined tools.
Web search
- v1: Supported via the
web-searchconnector in theconnectorsparameter - v2: Supported via user-defined tools.
v1
Uses the web search connector to search the internet for information relevant to the user’s query.
v2
Web search functionality is supported via tools.
Streaming
-
Event containing content:
- v1:
chunk.event_type == "text-generation" - v2:
chunk.type == "content-delta"
- v1:
-
Accessing response content:
- v1:
chunk.text - v2:
chunk.delta.message.content.text
- v1:
-
Events containing citations:
- v1:
chunk.event_type == "citation-generation" - v2:
chunk.type == "citation-start"
- v1:
-
Accessing citations:
- v1:
chunk.citations - v2:
chunk.delta.message.citations
- v1:
v1
v2
Tool use
Tool definition
- v1: uses Python types to define tools.
- v2: uses JSON schema to define tools.
v1
v2
Tool calling
-
Response handling
- v1: Tool calls accessed through
response.tool_calls - v2: Tool calls accessed through
response.message.tool_calls
- v1: Tool calls accessed through
-
Chat history management
- v1: Tool calls stored in the response’s
chat_history - v2: Append the tool call details (
tool_callsandtool_plan) to themessageslist
- v1: Tool calls stored in the response’s
v1
v2
Tool call ID
- v1: Tool calls do not emit tool call IDs
- v2: Tool calls emit tool call IDs. This will help the model match tool results to the right tool call.
v1
v2
Response generation
-
Tool execution: Chat history management
- v1: Append
callandoutputsto the chat history - v2: Append
tool_call_idandtool_contenttomessagesto the chat history
- v1: Append
-
Tool execution: Tool results
- v1: Passed as
tool_resultsparameter - v2: Incorporated into the
messageslist as tool responses
- v1: Passed as
-
User message
- v1: Set as empty (
"") - v2: No action required
- v1: Set as empty (
v1
v2
Citations
- Citations access:
- v1:
citations - v2:
message.citations
- v1:
- Cited tools access:
- v1:
documents - v2: as part of
message.citations, in thesourcesfield
- v1:
v1
v2
Streaming
-
Event containing content:
- v1:
chunk.event_type == "text-generation" - v2:
chunk.type == "content-delta"
- v1:
-
Accessing response content:
- v1:
chunk.text - v2:
chunk.delta.message.content.text
- v1:
-
Events containing citations:
- v1:
chunk.event_type == "citation-generation" - v2:
chunk.type == "citation-start"
- v1:
-
Accessing citations:
- v1:
chunk.citations - v2:
chunk.delta.message.citations
- v1:
v1
v2
Citation quality (both RAG and tool use)
- v1: controlled via
citation_qualityparameter - v2: controlled via
citation_optionsparameter (withmodeas a key)
Unsupported features in v2
The following v1 features are not supported in v2:
- General chat
conversation_idparameter (chat history is now managed by the developer via themessagesparameter)
- RAG
search_queries_onlyparameterconnectorsparameterprompt_truncationparameter
- Tool use
force_single_stepparameter (all tool calls are now multi-step by default)