{"id":12,"library":"cohere","title":"Cohere Python SDK","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","language":"python","source_language":"en","source_url":"https://docs.cohere.com/v2/docs/chat-api","tags":["cohere","llm","chat","embeddings","rerank","rag","command"],"install":[{"cmd":"pip install cohere","lang":"bash","label":"pip"},{"cmd":"uv add cohere","lang":"bash","label":"uv"}],"dependencies":[{"reason":"v5 was a full rewrite. Anything below 5.x is incompatible with current API.","package":"cohere>=5.0.0","optional":false}],"imports":[{"note":"Client() is v1 (legacy). ClientV2() is v2 (current). Use ClientV2 for all new code. Both exist in the same package.","wrong":"import cohere; co = cohere.Client()","symbol":"ClientV2","correct":"import cohere; co = cohere.ClientV2()"},{"note":"v1 Client returns response.text directly. v2 ClientV2 returns response.message.content[0].text — extra nesting.","wrong":"response.text","symbol":"response text (v2)","correct":"response.message.content[0].text"},{"note":"preamble parameter removed in v2. Use a system role message at the start of the messages array instead.","wrong":"preamble='...'","symbol":"system prompt (v2)","correct":"messages=[{'role': 'system', 'content': '...'}, {'role': 'user', 'content': '...'}]"},{"note":"AsyncClient is v1. AsyncClientV2 is the async v2 client.","wrong":"import cohere; co = cohere.AsyncClient()","symbol":"AsyncClientV2","correct":"import cohere; co = cohere.AsyncClientV2()"}],"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":[{"fix":"Migrate to co.chat() with ClientV2. See https://docs.cohere.com/v1/docs/migrating-from-cogenerate-to-cochat","message":"Generate endpoint (co.generate()) deprecated as of Aug 26, 2025. Calls will fail after sunset.","severity":"breaking","affected_versions":"all v1"},{"fix":"Always pass model= explicitly. Current recommended: command-a-03-2025","message":"model is a required parameter in all v2 API calls. Omitting it raises an error. v1 had a default model fallback.","severity":"breaking","affected_versions":"v2 (ClientV2)"},{"fix":"Replace preamble='...' with messages=[{'role': 'system', 'content': '...'}, ...]","message":"preamble parameter removed in v2. System instructions must be passed as a system role message.","severity":"breaking","affected_versions":"v1 → v2 migration"},{"fix":"Manage conversation history client-side by passing full messages array each turn","message":"conversation_id removed in v2. Cohere no longer manages chat history server-side.","severity":"breaking","affected_versions":"v1 → v2 migration"},{"fix":"Implement web search as a tool_use pattern. See v2 migration guide.","message":"connectors (built-in RAG) removed in v2. Web search now requires passing a web search tool explicitly.","severity":"breaking","affected_versions":"v1 → v2 migration"},{"fix":"Update rerank calls to use v3.5 endpoint. Check SDK release notes for exact method changes.","message":"rerank v2 deprecated. Use rerank v3.5.","severity":"breaking","affected_versions":"current"},{"fix":"Check which client you instantiated (Client vs ClientV2) and use the matching response access pattern.","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.","severity":"gotcha","affected_versions":"all"},{"fix":"For num_generations=n, call co.chat() n times. Trim outputs on your side instead of stop_sequences.","message":"num_generations, stop_sequences, logit_bias, and truncate parameters from Generate are not supported in Chat. No direct equivalent for num_generations.","severity":"gotcha","affected_versions":"all v2"},{"fix":"Set export CO_API_KEY=your_key. ClientV2() reads this automatically.","message":"CO_API_KEY is the expected environment variable name for ClientV2. Not COHERE_API_KEY. Failing to set CO_API_KEY or using the wrong variable name can lead to explicit instantiation errors or authentication failures.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-11T18:16:57.173Z","next_check":"2026-03-28T00:00:00.000Z","problems":[{"fix":"pip install cohere","cause":"The 'cohere' Python package is not installed in your current environment.","error":"ModuleNotFoundError: No module named 'cohere'"},{"fix":"Ensure your CO_API_KEY environment variable is set with a valid, active Cohere API key, or pass the correct key directly to `cohere.ClientV2(api_key='YOUR_API_KEY')`.","cause":"The API key provided for authentication is either incorrect, expired, or malformed.","error":"cohere.core.api_error.ApiError: headers: {'content-type': 'application/json'}, status_code: 401, body: {'message': 'invalid api token'}"},{"fix":"Migrate to `cohere.ClientV2` and use the `chat` endpoint for generative tasks, or other specific V2 endpoints like `embed` or `rerank`. \n```python\nimport cohere\nco = cohere.ClientV2()\nresponse = co.chat(\n    model=\"command-r-plus\",\n    messages=[{\"role\": \"user\", \"content\": \"Hello world!\"}],\n)\nprint(response)\n```","cause":"You are attempting to use the deprecated `generate` endpoint with the older `cohere.Client` class, or calling `generate` directly on `ClientV2` which primarily uses `chat`.","error":"AttributeError: 'Client' object has no attribute 'generate'"},{"fix":"Always include the `model` parameter with a valid model name (e.g., 'command-r-plus', 'embed-english-v3.0') in your API calls. \n```python\nimport cohere\nco = cohere.ClientV2()\nresponse = co.embed(\n    model=\"embed-english-v3.0\",\n    texts=[\"Hello world\"],\n    input_type=\"search_document\",\n)\nprint(response)\n```","cause":"In Cohere V2 API calls, the 'model' parameter is now explicitly required for most endpoints like `chat`, `embed`, and `rerank`.","error":"cohere.core.api_error.ApiError: headers: {'content-type': 'application/json'}, status_code: 400, body: {'message': 'a model parameter is required for this endpoint'}"},{"fix":"If integrating with libraries that expect `cohere.AsyncClient` (like older versions of LangChain), you might need to adjust the integration layer or ensure you are using a compatible version of the wrapper library. The Cohere SDK itself primarily uses the `ClientV2` methods for async operations (e.g., `await co.chat_stream(...)`).","cause":"The top-level `cohere` module in the Cohere Python SDK does not directly expose an `AsyncClient` class as an attribute, although asynchronous operations are supported through methods on `ClientV2`.","error":"AttributeError: module 'cohere' has no attribute 'AsyncClient'"}],"ecosystem":"pypi","meta_description":null,"install_score":0,"install_tag":"stale","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-11","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]},"quickstart_checks":{"last_tested":"2026-05-11","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}