xtekky/gpt4free · error · RuntimeError

Cohere API Error: {data['error']}

Error message

Cohere API Error: {data['error']}

What it means

RuntimeError raised by Cohere.raise_error when the parsed response JSON contains an "error" key, i.e. Cohere's API accepted the request shape but rejected it semantically (invalid model, malformed messages, bad parameters, or an API-level failure). The raw error payload from Cohere is embedded verbatim in the message for diagnosis.

Source

Thrown at g4f/Provider/needs_auth/Cohere.py:153

                                            "billed_units"
                                        ),
                                    )

    @classmethod
    def get_headers(
        cls, stream: bool, api_key: str = None, headers: dict = None
    ) -> dict:
        return {
            "Accept": "text/event-stream" if stream else "application/json",
            "Content-Type": "application/json",
            **({"Authorization": f"Bearer {api_key}"} if api_key is not None else {}),
            **({} if headers is None else headers),
        }

    @classmethod
    def raise_error(cls, data: dict):
        if "error" in data:
            raise RuntimeError(f"Cohere API Error: {data['error']}")

View on GitHub (pinned to 973504e177)

Solutions

  1. Read the embedded Cohere error payload — it states the exact cause (bad model, invalid parameter, etc.) and fix accordingly.
  2. Verify the model string against Cohere's current model list.
  3. Check that your API key's plan/tier permits the requested model and features.
  4. If the payload mentions overload/try-again, retry after a short backoff.

Example fix

# before
resp = await client.chat.completions.create(model="command-r-03-2024", provider=Cohere, messages=msgs, api_key=key)
# RuntimeError: Cohere API Error: {'message': 'model not found'}

# after
resp = await client.chat.completions.create(model="command-r", provider=Cohere, messages=msgs, api_key=key)
Defensive patterns

Strategy: try-catch

Try / catch

try:
    resp = await client.chat.completions.create(model=m, provider=Cohere, messages=msgs, api_key=key)
except RuntimeError as e:
    if "Cohere API Error" in str(e):
        log_and_handle_cohere_payload(str(e))
    else:
        raise

Prevention

When it happens

Trigger: Any Cohere API call whose JSON response body contains an error object — e.g. requesting a model your key cannot access, sending messages in an unsupported format, or exceeding Cohere-side limits that are reported in the body rather than the HTTP status.

Common situations: Using a model name deprecated/removed by Cohere; trial keys hitting feature-gated endpoints; passing chat-history formats Cohere rejects (e.g. invalid roles); Cohere incident returning error JSON with HTTP 200.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/cb24355b2c97c4f9. Report an issue: GitHub.