{"record":{"id":"88cb2e264514d7b5","repo":"BerriAI/litellm","slug":"cohere-api-key-is-required-please-set-cohere-api","errorCode":null,"errorMessage":"Cohere API key is required. Please set 'COHERE_API_KEY' or 'CO_API_KEY' or 'litellm.cohere_key'","messagePattern":"Cohere API key is required\\. Please set 'COHERE_API_KEY' or 'CO_API_KEY' or 'litellm\\.cohere_key'","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/cohere/rerank/transformation.py","lineNumber":89,"sourceCode":"                top_n=top_n,\n                rank_fields=rank_fields,\n                return_documents=return_documents,\n                max_chunks_per_doc=max_chunks_per_doc,\n            )\n        )\n\n    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        api_key: str | None = None,\n        optional_params: dict | None = None,\n    ) -> dict:\n        if api_key is None:\n            api_key = get_secret_str(\"COHERE_API_KEY\") or get_secret_str(\"CO_API_KEY\") or litellm.cohere_key\n\n        if api_key is None:\n            raise ValueError(\n                \"Cohere API key is required. Please set 'COHERE_API_KEY' or 'CO_API_KEY' or 'litellm.cohere_key'\"\n            )\n\n        default_headers: Final = {\n            \"Authorization\": f\"Bearer {api_key}\",\n            \"accept\": \"application/json\",\n            \"content-type\": \"application/json\",\n        }\n\n        # If 'Authorization' is provided in headers, it overrides the default.\n        if \"Authorization\" in headers:\n            default_headers[\"Authorization\"] = headers[\"Authorization\"]\n\n        # Merge other headers, overriding any default ones except Authorization\n        return {**default_headers, **headers}\n\n    def transform_rerank_request(\n        self,","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/cohere/rerank/transformation.py#L71-L107","documentation":"Credential guard in Cohere rerank validate_environment: no API key was resolved from the api_key argument, COHERE_API_KEY/CO_API_KEY secrets, or litellm.cohere_key, so the Authorization header cannot be built.","triggerScenarios":"Thrown at litellm/llms/cohere/rerank/transformation.py:89 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Set the COHERE_API_KEY environment variable before making the call, e.g. export COHERE_API_KEY=<your-key>.","Alternatively pass the credential directly via the api_key parameter in the litellm call.","You can also set CO_API_KEY or assign litellm.cohere_key before the call."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}