BerriAI/litellm · error · CohereError
raw_response.text
Error message
raw_response.text
What it means
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.
Source
Thrown at litellm/llms/cohere/chat/transformation.py:236
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()
model_response.choices[0].message.content = raw_response_json["text"]
except Exception:
raise CohereError(message=raw_response.text, status_code=raw_response.status_code)
## ADD CITATIONS
if "citations" in raw_response_json:
setattr(model_response, "citations", raw_response_json["citations"])
## Tool calling response
cohere_tools_response: Final = raw_response_json.get("tool_calls", None)
if cohere_tools_response is not None and cohere_tools_response != []:
# convert cohere_tools_response to OpenAI response format
tool_calls: Final = []
for tool in cohere_tools_response:
function_name = tool.get("name", "")
generation_id = tool.get("generation_id", "")
parameters = tool.get("parameters", {})
tool_call = {
"id": f"call_{generation_id}",
"type": "function",
"function": {View on GitHub (pinned to 6c2dcb801b)
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.
Example fix
# before (v1 path, may miss 'text') resp = litellm.completion(model="cohere/command-nightly", messages=[...]) # after (v2-capable model) resp = litellm.completion(model="cohere/command-r-plus", messages=[...])
Defensive patterns
Strategy: try-catch
Try / catch
from litellm.exceptions import CohereError
try:
resp = litellm.completion(model="cohere/command-r-plus", messages=msgs)
except CohereError as e:
if e.status_code == 200 or "text" not in (e.message or ""):
# schema/shape issue: prefer v2 models instead of retrying blind
raise RuntimeError(f"Unexpected Cohere v1 body: {e.message}") from e
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- raw_response.text
- Failed to decode JSON from chunk: {chunk}
- Error parsing chunk: {e}, Received chunk: {chunk}
- {service_name} returned non-dict JSON ({type(result).__name_
- CohereException - {original_exception.message}
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/6a9548daef5035f0.
Report an issue: GitHub.