{"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":"critical","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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/exception_mapping_utils.py#L1316-L1352","documentation":"LiteLLM's Cohere mapper raises AuthenticationError when the Cohere error string contains 'invalid api token' or 'No API key provided.'. Your Cohere credentials failed: the API key is absent, malformed, revoked, or from the wrong environment (trial vs production). The original provider response is attached for header-level inspection.","triggerScenarios":"Calling completion(model='command-r-plus', api_key=...) with a missing/None API key (COHERE_API_KEY unset), a key copied with extra characters, a revoked key, or using a Cohere platform key against an enterprise/private endpoint (and vice versa).","commonSituations":"COHERE_API_KEY not exported in the deployment environment; key rotated in the Cohere dashboard but stale in env/secrets; mixing up Cohere's trial and production keys which hit different rate pools; pointing api_base at a private-link deployment while sending a public-platform key.","solutions":["Set a valid key: export COHERE_API_KEY=... or pass api_key explicitly; verify it has no whitespace/quotes","Regenerate the key in the Cohere dashboard (API keys section) if revoked or unclear","Match the key to the endpoint: production keys for the standard API, correct credentials for private/enterprise api_base","Confirm the key's environment label (trial vs production) matches where you send traffic"],"exampleFix":"# before\nresp = completion(model=\"command-r-plus\", messages=[...])\n# CohereException - No API key provided.\n\n# after\nimport os\nfrom litellm import completion\nresp = completion(\n    model=\"command-r-plus\",\n    messages=[...],\n    api_key=os.environ[\"COHERE_API_KEY\"].strip(),\n)","handlingStrategy":"validation","validationCode":"import os\n\ndef cohere_key_ok() -> bool:\n    key = (os.environ.get(\"COHERE_API_KEY\") or \"\").strip()\n    return len(key) >= 20 and not key.startswith((\"Bearer\", \" \"))\n\nassert cohere_key_ok(), \"COHERE_API_KEY missing/invalid — set a dashboard-issued key\"","typeGuard":null,"tryCatchPattern":"import litellm\n\ntry:\n    resp = litellm.completion(model=\"command-r-plus\", messages=msgs)\nexcept litellm.AuthenticationError as e:\n    if \"invalid api token\" in str(e) or \"No API key provided\" in str(e):\n        raise PermissionError(\"Cohere credential missing/revoked — rotate COHERE_API_KEY\") from e\n    raise","preventionTips":["Fail fast at startup if COHERE_API_KEY is unset or suspiciously short","Store the key in a secrets manager with rotation hooks","Match key environment (trial vs production) to your traffic"],"tags":["litellm","cohere","authentication","api-key"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}