BerriAI/litellm · error · CohereError

raw_response.text

Error message

raw_response.text

What it means

In the Cohere v2 chat transformation, the first parse step is raw_response.json(). If the body is not valid JSON (empty body, HTML from a gateway, truncated stream), a CohereError is raised with the raw text as message and the response's real status code. This is a decode failure, not a schema failure — schema failures happen one step later (errorIndex 1430).

Source

Thrown at litellm/llms/cohere/chat/v2_transformation.py:199

    def transform_response(
        self,
        model: str,
        raw_response: httpx.Response,
        model_response: ModelResponse,
        logging_obj: LiteLLMLoggingObj,
        request_data: dict,
        messages: list[AllMessageValues],
        optional_params: dict,
        litellm_params: dict,
        encoding: Any,
        api_key: str | None = None,
        json_mode: bool | None = None,
    ) -> ModelResponse:
        try:
            raw_response_json: Final = raw_response.json()
        except Exception:
            raise CohereError(message=raw_response.text, status_code=raw_response.status_code)

        try:
            cohere_v2_chat_response: Final = CohereV2ChatResponse(**raw_response_json)
        except Exception:
            raise CohereError(message=raw_response.text, status_code=422)

        cohere_content: Final = cohere_v2_chat_response["message"].get("content", None)
        if cohere_content is not None:
            model_response.choices[0].message.content = "".join(
                [content.get("text", "") for content in cohere_content if content is not None]
            )

        ## ADD CITATIONS AS ANNOTATIONS
        annotations: list[ChatCompletionAnnotation] | None = None
        citations = None

        if "message" in cohere_v2_chat_response and "citations" in cohere_v2_chat_response["message"]:
            citations = cohere_v2_chat_response["message"]["citations"]

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Check error.status_code: non-200 means a real API failure whose body is in error.message; 200 + non-JSON means a proxy/gateway intercepted the request.
  2. Verify api_base points at https://api.cohere.com (or compat/v1) if customized.
  3. Retry transient failures with backoff.
  4. Reproduce with curl from the same environment to confirm the raw body.
Defensive patterns

Strategy: try-catch

Try / catch

from litellm.exceptions import CohereError

try:
    resp = litellm.completion(model="cohere/command-a", messages=msgs)
except CohereError as e:
    if e.status_code == 200:
        # 200 + unparseable body => middlebox interference, not Cohere
        log_proxy_suspicion(e.message)
    raise

Prevention

When it happens

Trigger: Cohere v2 chat call (command-r, command-r-plus, command-a via /v2/chat) returning a non-JSON body: 200-with-HTML from a misrouted proxy, an empty 5xx body, or a response corrupted in transit; also a custom api_base serving non-JSON.

Common situations: Corporate proxies intercepting TLS and serving block pages; wrong api_base; transient gateway errors. The status_code in the CohereError is the genuine HTTP status, so a 200 + non-JSON body strongly implies a middlebox.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/176a992dc3806c4d. Report an issue: GitHub.