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:
model
is a required field for Embed, Rerank, Classify, and Chat.
Embed
- v2:
embedding_types
is a required field for Embed.
Chat
Messages and preamble
-
Message structure:
- v1: uses separate
preamble
andmessage
parameters. - v2: uses a single
messages
parameter consisting of a list of roles (system
,user
,assistant
, ortool
). Thesystem
role in v2 replaces thepreamble
parameter in v1.
- v1: uses separate
-
Chat history:
- v1: manages the chat history via the
chat_history
parameter. - v2: manages the chat history via the
messages
list.
- 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
documents
parameter supports a list of objects with multiple fields per document. - v2: the
documents
parameter supports a few different options for structuring documents:- List of objects with
data
object: same as v1 described above, but each document passed as adata
object (with an optionalid
field to be used in citations). - List of objects with
data
string (with an optionalid
field 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 thesources
field
- v1:
v1
v2
Search query generation
- v1: Uses
search_queries_only
parameter - v2: Supported via tools. We recommend using the v1 API for this functionality in order to leverage the
force_single_step
feature. Support in v2 will be coming soon.
Connectors
- v1: Supported via the
connectors
parameter - v2: Supported via user-defined tools.
Web search
- v1: Supported via the
web-search
connector in theconnectors
parameter - 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_calls
andtool_plan
) to themessages
list
- 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
call
andoutputs
to the chat history - v2: Append
tool_call_id
andtool_content
tomessages
to the chat history
- v1: Append
-
Tool execution: Tool results
- v1: Passed as
tool_results
parameter - v2: Incorporated into the
messages
list 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 thesources
field
- 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_quality
parameter - v2: controlled via
citation_options
parameter (withmode
as a key)
Unsupported features in v2
The following v1 features are not supported in v2:
- General chat
conversation_id
parameter (chat history is now managed by the developer via themessages
parameter)
- RAG
search_queries_only
parameterconnectors
parameterprompt_truncation
parameter
- Tool use
force_single_step
parameter (all tool calls are now multi-step by default)