Cohere Upgrade & Migration
Overview
Guide for migrating from Cohere API v1 to v2 and upgrading the cohere-ai TypeScript / cohere Python SDK. Covers every breaking change with before/after code.
Prerequisites
- Current Cohere SDK installed
- Git for version control
- Test suite available
Instructions
Step 1: Check Current Version
npm list cohere-ai
pip show cohere
npm view cohere-ai version
pip index versions cohere
Step 2: Create Upgrade Branch
git checkout -b upgrade/cohere-v2-migration
npm install cohere-ai@latest
Step 3: API v1 to v2 Breaking Changes
Client Import
import { CohereClient } from 'cohere-ai';
const cohere = new CohereClient({ token: '...' });
import { CohereClientV2 } from 'cohere-ai';
const cohere = new CohereClientV2({ token: '...' });
Chat Endpoint — Messages Format
const response = await cohere.chat({
message: 'Hello',
preamble: 'You are helpful.',
chatHistory: [
{ role: 'USER', message: 'Hi' },
{ role: 'CHATBOT', message: 'Hello!' },
],
});
console.log(response.text);
const response = await cohere.chat({
model: 'command-a-03-2025',
messages: [
{ role: 'system', content: 'You are helpful.' },
{ role: 'user', content: 'Hi' },
{ role: 'assistant', content: 'Hello!' },
{ role: 'user', content: 'Hello' },
],
});
console.log(response.message?.content?.[0]?.text);
Role Names
| v1 | v2 |
|---|
USER | user |
CHATBOT | assistant |
SYSTEM | system |
TOOL | tool |
Embed Endpoint — Required Fields
const response = await cohere.embed({
texts: ['hello'],
});
const response = await cohere.embed({
model: 'embed-v4.0',
texts: ['hello'],
inputType: 'search_document',
embeddingTypes: ['float'],
});
Rerank Endpoint — Model Required
const response = await cohere.rerank({
query: 'best language',
documents: ['Python', 'Rust'],
});
const response = await cohere.rerank({
model: 'rerank-v3.5',
query: 'best language',
documents: ['Python', 'Rust'],
topN: 2,
});
Classify Endpoint — Model Required
const response = await cohere.classify({
inputs: ['great product'],
examples: [],
});
const response = await cohere.classify({
model: 'embed-english-v3.0',
inputs: ['great product'],
examples: [],
});
Streaming Changes
const stream = cohere.chatStream({ message: 'hello' });
for await (const event of stream) {
if (event.eventType === 'text-generation') {
process.stdout.write(event.text);
}
}
const stream = await cohere.chatStream({
model: 'command-a-03-2025',
messages: [{ role: 'user', content: 'hello' }],
});
for await (const event of stream) {
if (event.type === 'content-delta') {
process.stdout.write(event.delta?.message?.content?.text ?? '');
}
}
RAG / Documents
const response = await cohere.chat({
message: 'question',
documents: [{ title: 'Doc', snippet: 'content' }],
connectors: [{ id: 'web-search' }],
});
const response = await cohere.chat({
model: 'command-a-03-2025',
messages: [{ role: 'user', content: 'question' }],
documents: [
{ id: 'doc1', data: { text: 'content' } },
],
});
Tool Use
const tools = [{
name: 'get_weather',
description: 'Get weather',
parameterDefinitions: {
city: { type: 'str', required: true },
},
}];
const tools = [{
type: 'function',
function: {
name: 'get_weather',
description: 'Get weather',
parameters: {
type: 'object',
properties: { city: { type: 'string' } },
required: ['city'],
},
},
}];
Step 4: Model Name Updates
| Old Name | Current Name | Status |
|---|
command | command-r7b-12-2024 | Use new ID |
command-r | command-r-08-2024 | Use new ID |
command-r-plus | command-r-plus-08-2024 | Use new ID |
command-nightly | command-a-03-2025 | Use Command A |
embed-english-v3.0 | embed-v4.0 | Upgrade recommended |
rerank-english-v3.0 | rerank-v3.5 | Upgrade recommended |
Step 5: Python Migration
import cohere
co = cohere.Client(api_key="...")
response = co.chat(message="hello")
print(response.text)
import cohere
co = cohere.ClientV2()
response = co.chat(
model="command-a-03-2025",
messages=[{"role": "user", "content": "hello"}],
)
print(response.message.content[0].text)
Step 6: Run Tests and Verify
npm test
git add -A
git commit -m "chore: migrate to Cohere API v2"
Migration Codemod (Find & Replace)
grep -rn "CohereClient\b" src/ --include="*.ts" | grep -v "CohereClientV2"
grep -rn "\.chat({" src/ --include="*.ts" -A2 | grep "message:"
grep -rn "response\.text\b" src/ --include="*.ts"
grep -rn "eventType.*text-generation" src/ --include="*.ts"
Output
- Updated SDK to latest version
- Migrated all endpoints from v1 to v2 format
- Updated model IDs to current names
- All tests passing against v2 API
Error Handling
| Error After Migration | Cause | Fix |
|---|
model is required | Missed adding model param | Add model to every call |
response.text is undefined | v1 response shape | Use response.message?.content?.[0]?.text |
embedding_types required | v2 embed requirement | Add embeddingTypes: ['float'] |
input_type required | v2 embed requirement | Add inputType: 'search_document' |
Resources
Next Steps
For CI integration during upgrades, see cohere-ci-integration.