{"record":{"id":"e3f3159332a52b35","repo":"BerriAI/litellm","slug":"cohereexception-original-exception-message","errorCode":null,"errorMessage":"CohereException - {original_exception.message}","messagePattern":"CohereException - (.+?)","errorType":"exception","errorClass":"AuthenticationError","httpStatus":401,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":1334,"sourceCode":"            message=f\"Cloudflare Exception - {original_exception.message}\",\n            llm_provider=\"cloudflare\",\n            model=model,\n            response=getattr(original_exception, \"response\", None),\n        )\n\n\ndef _map_cohere_exception(\n    *,\n    model: str,\n    original_exception: _ProviderHTTPException,\n    custom_llm_provider: str,\n    error_str: str,\n    exception_type: str,\n    exception_provider: str,\n    extra_information: str,\n) -> None:\n    if \"invalid api token\" in error_str or \"No API key provided.\" in error_str:\n        raise AuthenticationError(\n            message=f\"CohereException - {original_exception.message}\",\n            llm_provider=\"cohere\",\n            model=model,\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"invalid type: parameter\" in error_str:\n        raise BadRequestError(\n            message=f\"CohereException - {original_exception.message}\",\n            llm_provider=\"cohere\",\n            model=model,\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"too many tokens\" in error_str:\n        raise ContextWindowExceededError(\n            message=f\"CohereException - {original_exception.message}\",\n            model=model,\n            llm_provider=\"cohere\",\n            response=getattr(original_exception, \"response\", None),","sourceCodeStart":1316,"sourceCodeEnd":1352,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/exception_mapping_utils.py#L1316-L1352","documentation":"_map_cohere_exception maps error strings 'invalid api token' or 'No API key provided.' to litellm.AuthenticationError ('CohereException - <original message>'). This is Cohere's platform rejecting (or not receiving) your API key on api.cohere.com. litellm sends the key from COHERE_API_KEY or the api_key parameter, so the failure is almost always a missing/typo'd environment variable or an invalidated trial key.","triggerScenarios":"model='cohere/command-r-plus' (or command-a, command-r) without COHERE_API_KEY set; a trial key that expired when the trial ended; a revoked or regenerated key still cached in env; passing api_key=None because os.getenv was misspelled.","commonSituations":"Local works but deployed container lacks COHERE_API_KEY; free-trial keys expiring after evaluation; CI pipelines masking the variable incorrectly (empty string ≠ unset, both fail here); using a key from a different Cohere environment (EU vs US base URLs).","solutions":["Set the key: export COHERE_API_KEY=<key> from dashboard.cohere.com > API keys, or pass api_key explicitly.","Verify it works directly: curl -H \"Authorization: Bearer $COHERE_API_KEY\" 'https://api.cohere.com/v1/models'.","If the trial expired, create a new key on a paid/production account and update the secret store.","For EU endpoints set the correct api_base alongside the key."],"exampleFix":"# before\nresp = litellm.completion(model=\"cohere/command-r-plus\", messages=msgs)  # AuthenticationError: No API key provided.\n\n# after\nimport litellm, os\nresp = litellm.completion(\n    model=\"cohere/command-r-plus\",\n    messages=msgs,\n    api_key=os.environ[\"COHERE_API_KEY\"],\n)","handlingStrategy":"validation","validationCode":"import os\n\ndef require_cohere_key() -> str:\n    key = os.environ.get(\"COHERE_API_KEY\")\n    if not key:\n        raise RuntimeError(\"COHERE_API_KEY is not set — cohere calls will fail with AuthenticationError\")\n    return key","typeGuard":"import litellm\n\ndef is_authentication_error(e: BaseException) -> bool:\n    return isinstance(e, litellm.AuthenticationError)","tryCatchPattern":"try:\n    resp = litellm.completion(model=\"cohere/command-r-plus\", messages=msgs)\nexcept litellm.AuthenticationError as e:\n    logger.error(\"cohere rejected API key: %s\", e)\n    notify_ops(\"COHERE_API_KEY invalid or expired\")\n    raise","preventionTips":["Check COHERE_API_KEY presence in a startup probe and alert when missing.","Treat empty-string env values as missing (CI masking often produces them).","Rotate keys on a schedule and update the secret store atomically with the change.","Verify key validity against api.cohere.com/v1/models during deployment smoke tests."],"tags":["litellm","cohere","authentication","api-key"],"backgroundTag":"invalid-api-key","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}