{"id":"cohere","title":"Cohere Python SDK","library":"cohere","description":"Official Python SDK for Cohere API. Has two coexisting client classes: Client (v1, legacy) and ClientV2 (v2, current). Most LLM-generated code uses v1 patterns. The Generate endpoint is deprecated. model is now required in all v2 calls.","status":"active","version":"5.20.4","install":[{"cmd":"pip install cohere","lang":"bash","label":"pip"},{"cmd":"uv add cohere","lang":"bash","label":"uv"}],"dependencies":[{"package":"cohere>=5.0.0","reason":"v5 was a full rewrite. Anything below 5.x is incompatible with current API.","optional":false}],"imports":[{"symbol":"ClientV2","correct":"import cohere; co = cohere.ClientV2()","wrong":"import cohere; co = cohere.Client()","note":"Client() is v1 (legacy). ClientV2() is v2 (current). Use ClientV2 for all new code. Both exist in the same package."},{"symbol":"response text (v2)","correct":"response.message.content[0].text","wrong":"response.text","note":"v1 Client returns response.text directly. v2 ClientV2 returns response.message.content[0].text — extra nesting."},{"symbol":"system prompt (v2)","correct":"messages=[{'role': 'system', 'content': '...'}, {'role': 'user', 'content': '...'}]","wrong":"preamble='...'","note":"preamble parameter removed in v2. Use a system role message at the start of the messages array instead."},{"symbol":"AsyncClientV2","correct":"import cohere; co = cohere.AsyncClientV2()","wrong":"import cohere; co = cohere.AsyncClient()","note":"AsyncClient is v1. AsyncClientV2 is the async v2 client."}],"quickstart":{"code":"import cohere\n\nco = cohere.ClientV2()  # reads CO_API_KEY env var\n\nresponse = co.chat(\n    model='command-a-03-2025',\n    messages=[{'role': 'user', 'content': 'Hello'}]\n)\nprint(response.message.content[0].text)","lang":"python","description":"Minimal chat completion using v2 client"},"warnings":[{"severity":"breaking","message":"Generate endpoint (co.generate()) deprecated as of Aug 26, 2025. Calls will fail after sunset.","affected_versions":"all v1","fix":"Migrate to co.chat() with ClientV2. See https://docs.cohere.com/v1/docs/migrating-from-cogenerate-to-cochat"},{"severity":"breaking","message":"model is a required parameter in all v2 API calls. Omitting it raises an error. v1 had a default model fallback.","affected_versions":"v2 (ClientV2)","fix":"Always pass model= explicitly. Current recommended: command-a-03-2025"},{"severity":"breaking","message":"preamble parameter removed in v2. System instructions must be passed as a system role message.","affected_versions":"v1 → v2 migration","fix":"Replace preamble='...' with messages=[{'role': 'system', 'content': '...'}, ...]"},{"severity":"breaking","message":"conversation_id removed in v2. Cohere no longer manages chat history server-side.","affected_versions":"v1 → v2 migration","fix":"Manage conversation history client-side by passing full messages array each turn"},{"severity":"breaking","message":"connectors (built-in RAG) removed in v2. Web search now requires passing a web search tool explicitly.","affected_versions":"v1 → v2 migration","fix":"Implement web search as a tool_use pattern. See v2 migration guide."},{"severity":"breaking","message":"rerank v2 deprecated. Use rerank v3.5.","affected_versions":"current","fix":"Update rerank calls to use v3.5 endpoint. Check SDK release notes for exact method changes."},{"severity":"gotcha","message":"Response text access changed between v1 and v2. v1: response.text. v2: response.message.content[0].text. Mixing clients and response patterns is the most common error.","affected_versions":"all","fix":"Check which client you instantiated (Client vs ClientV2) and use the matching response access pattern."},{"severity":"gotcha","message":"num_generations, stop_sequences, logit_bias, and truncate parameters from Generate are not supported in Chat. No direct equivalent for num_generations.","affected_versions":"all v2","fix":"For num_generations=n, call co.chat() n times. Trim outputs on your side instead of stop_sequences."},{"severity":"gotcha","message":"CO_API_KEY is the expected environment variable name. Not COHERE_API_KEY. Using the wrong var name results in silent auth failure.","affected_versions":"all","fix":"Set export CO_API_KEY=your_key. ClientV2() reads this automatically."}],"last_verified":"2026-02-28","next_check":"2026-03-28","source_url":"https://docs.cohere.com/v2/docs/chat-api","language":"python","source_language":"en","tags":["cohere","llm","chat","embeddings","rerank","rag","command"]}