BerriAI/litellm · error · ValueError

Cohere API key is required. Please set 'COHERE_API_KEY' or…

Error message

Cohere API key is required. Please set 'COHERE_API_KEY' or 'CO_API_KEY' or 'litellm.cohere_key'

What it means

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.

Solutions

  1. Set the COHERE_API_KEY environment variable before making the call, e.g. export COHERE_API_KEY=<your-key>.
  2. Alternatively pass the credential directly via the api_key parameter in the litellm call.
  3. You can also set CO_API_KEY or assign litellm.cohere_key before the call.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at litellm/llms/cohere/rerank/transformation.py:89 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/88cb2e264514d7b5. Report an issue: GitHub.

Appendix: source

Thrown at litellm/llms/cohere/rerank/transformation.py:89

                top_n=top_n,
                rank_fields=rank_fields,
                return_documents=return_documents,
                max_chunks_per_doc=max_chunks_per_doc,
            )
        )

    def validate_environment(
        self,
        headers: dict,
        model: str,
        api_key: str | None = None,
        optional_params: dict | None = None,
    ) -> dict:
        if api_key is None:
            api_key = get_secret_str("COHERE_API_KEY") or get_secret_str("CO_API_KEY") or litellm.cohere_key

        if api_key is None:
            raise ValueError(
                "Cohere API key is required. Please set 'COHERE_API_KEY' or 'CO_API_KEY' or 'litellm.cohere_key'"
            )

        default_headers: Final = {
            "Authorization": f"Bearer {api_key}",
            "accept": "application/json",
            "content-type": "application/json",
        }

        # If 'Authorization' is provided in headers, it overrides the default.
        if "Authorization" in headers:
            default_headers["Authorization"] = headers["Authorization"]

        # Merge other headers, overriding any default ones except Authorization
        return {**default_headers, **headers}

    def transform_rerank_request(
        self,

View on GitHub (pinned to 77b7c6c40c)