{"record":{"id":"6a9548daef5035f0","repo":"BerriAI/litellm","slug":"raw-response-text-6a9548","errorCode":null,"errorMessage":"raw_response.text","messagePattern":"raw_response\\.text","errorType":"exception","errorClass":"CohereError","httpStatus":null,"severity":"error","filePath":"litellm/llms/cohere/chat/transformation.py","lineNumber":236,"sourceCode":"    def transform_response(\n        self,\n        model: str,\n        raw_response: httpx.Response,\n        model_response: ModelResponse,\n        logging_obj: LiteLLMLoggingObj,\n        request_data: dict,\n        messages: list[AllMessageValues],\n        optional_params: dict,\n        litellm_params: dict,\n        encoding: Any,\n        api_key: str | None = None,\n        json_mode: bool | None = None,\n    ) -> ModelResponse:\n        try:\n            raw_response_json: Final = raw_response.json()\n            model_response.choices[0].message.content = raw_response_json[\"text\"]\n        except Exception:\n            raise CohereError(message=raw_response.text, status_code=raw_response.status_code)\n\n        ## ADD CITATIONS\n        if \"citations\" in raw_response_json:\n            setattr(model_response, \"citations\", raw_response_json[\"citations\"])\n\n        ## Tool calling response\n        cohere_tools_response: Final = raw_response_json.get(\"tool_calls\", None)\n        if cohere_tools_response is not None and cohere_tools_response != []:\n            # convert cohere_tools_response to OpenAI response format\n            tool_calls: Final = []\n            for tool in cohere_tools_response:\n                function_name = tool.get(\"name\", \"\")\n                generation_id = tool.get(\"generation_id\", \"\")\n                parameters = tool.get(\"parameters\", {})\n                tool_call = {\n                    \"id\": f\"call_{generation_id}\",\n                    \"type\": \"function\",\n                    \"function\": {","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/cohere/chat/transformation.py#L218-L254","documentation":"In Cohere v1 chat transformation, transform_response tries raw_response.json() and then accesses the 'text' field for the message content. If either fails — non-JSON body or a JSON body missing 'text' (e.g. an error payload) — it raises CohereError with the raw response text and the real HTTP status code. This covers both malformed bodies and API error payloads that bypassed status checks.","triggerScenarios":"Calling cohere/* v1 chat models (command, command-light, command-nightly) when the response body lacks a 'text' field: error JSON like {\"message\": \"...\"} returned with 200/4xx, HTML from a proxy, or a truncated body.","commonSituations":"Cohere returning an error object without 'text' (invalid model name, bad parameters); api_base customized to a gateway returning unexpected JSON; deprecated v1 endpoint shape changes. Note Cohere has deprecated v1 in favor of v2 models — using a v2-only model through the v1 path triggers this.","solutions":["Inspect error.message (raw body) and error.status_code: Cohere error payloads state the actual problem (e.g. invalid model, invalid API key).","Switch to a current model (command-r-plus / command-a) via the v2 path — the v1 'text' shape is legacy.","If api_base was overridden, remove it or point it at the genuine Cohere endpoint.","Retry once for transient truncation; if persistent, enable verbose logging to capture the full body."],"exampleFix":"# before (v1 path, may miss 'text')\nresp = litellm.completion(model=\"cohere/command-nightly\", messages=[...])\n\n# after (v2-capable model)\nresp = litellm.completion(model=\"cohere/command-r-plus\", messages=[...])","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm.exceptions import CohereError\n\ntry:\n    resp = litellm.completion(model=\"cohere/command-r-plus\", messages=msgs)\nexcept CohereError as e:\n    if e.status_code == 200 or \"text\" not in (e.message or \"\"):\n        # schema/shape issue: prefer v2 models instead of retrying blind\n        raise RuntimeError(f\"Unexpected Cohere v1 body: {e.message}\") from e\n    raise","preventionTips":["Prefer current v2-capable models (command-r, command-r-plus, command-a) over legacy v1 command models.","Do not override api_base for Cohere unless the target faithfully proxies the Cohere shape.","Pin your litellm version and test after Cohere API deprecation announcements."],"tags":["cohere","json-parsing","response-shape","deprecation"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}