{"record":{"id":"e2c7c9f81e0337c1","repo":"BerriAI/litellm","slug":"response-cannot-be-casted-to-coherechatresult-e","errorCode":null,"errorMessage":"Response cannot be casted to CohereChatResult: {e}","messagePattern":"Response cannot be casted to CohereChatResult: (.+?)","errorType":"exception","errorClass":"OCIError","httpStatus":null,"severity":"error","filePath":"litellm/llms/oci/chat/cohere.py","lineNumber":203,"sourceCode":"                description=function_def.get(\"description\", \"\"),\n                parameterDefinitions=parameter_definitions,\n            )\n        )\n\n    return cohere_tools\n\n\ndef handle_cohere_response(\n    json_response: dict,\n    model: str,\n    model_response: ModelResponse,\n    raw_response: httpx.Response,\n) -> ModelResponse:\n    \"\"\"Parse a non-streaming Cohere OCI response into a LiteLLM ModelResponse.\"\"\"\n    try:\n        cohere_response: Final = CohereChatResult(**json_response)\n    except (TypeError, ValidationError) as e:\n        raise OCIError(\n            message=f\"Response cannot be casted to CohereChatResult: {e}\",\n            status_code=raw_response.status_code,\n        )\n\n    model_response.model = model\n    model_response.created = int(datetime.datetime.now().timestamp())\n\n    response_text: Final = cohere_response.chatResponse.text\n    finish_reason: Final = _normalize_oci_finish_reason(cohere_response.chatResponse.finishReason)\n\n    tool_calls: list[dict[str, Any]] | None = None\n    if cohere_response.chatResponse.toolCalls:\n        tool_calls = [\n            {\n                \"id\": _synthesize_oci_tool_call_id(i, tc.name, json.dumps(tc.parameters, sort_keys=True)),\n                \"type\": \"function\",\n                \"function\": {\n                    \"name\": tc.name,","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/oci/chat/cohere.py#L185-L221","documentation":"After a successful HTTP call to OCI's Cohere chat endpoint, LiteLLM validates the JSON body against the pydantic model CohereChatResult. If the payload has missing/wrongly-typed fields (TypeError or pydantic ValidationError), it raises OCIError with the upstream HTTP status code. This almost always means the OCI response shape does not match what this LiteLLM version expects — upstream API drift, not a bad request you sent.","triggerScenarios":"Non-streaming completion via model=\"oci/cohere.<variant>\" (e.g. cohere.command-r-plus-08-2024 served through OCI Generative AI) where the returned JSON fails CohereChatResult validation — e.g.OCI changed field names/casing, returned an error-shaped 200 body, or a model returned an undocumented structure.","commonSituations":"OCI updates their Cohere response contract (region rollout differences); pinning an old litellm against a new OCI API, or new litellm against an OCI Dedicated AI cluster running legacy firmware; model not enabled in the compartment returning a structured error inside a 200; isStreaming=false hitting a handler path only exercised for specific cohere versions.","solutions":["Upgrade LiteLLM: pip install -U litellm — parser models track OCI schema changes and fixes land regularly","Capture the raw response to inspect the mismatch: temporarily wrap with litellm client kwargs or use a proxy to log the upstream body, then compare fields against the error text (it embeds the pydantic error)","Try another OCI region or the same model on the OCID-serving endpoint to rule out a region-specific rollout","Retry once — transient OCI gateway error pages (HTML/JSON error in 200) also trigger this","If it persists, report with the sanitized response body to the litellm GitHub issue tracker; downgrade to the last working litellm version as a stopgap"],"exampleFix":"# before\nresp = litellm.completion(model=\"oci/cohere.command-r-plus-08-2024\", messages=msgs)\n# OCIError: Response cannot be casted to CohereChatResult: ...\n\n# after: pin/upgrade a litellm release matching current OCI schema, and guard the call\nimport litellm\nfrom litellm.exceptions import OCIError\ntry:\n    resp = litellm.completion(model=\"oci/cohere.command-r-plus-08-2024\", messages=msgs)\nexcept OCIError:\n    resp = litellm.completion(model=\"oci/generic.model\", messages=msgs)  # GENERIC handler path","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm.exceptions import OCIError\n\ntry:\n    resp = litellm.completion(model=MODEL, messages=msgs)\nexcept OCIError as e:\n    if \"cannot be casted to CohereChatResult\" in str(e):\n        resp = litellm.completion(model=FALLBACK_MODEL, messages=msgs)  # e.g. oci/generic.* or another vendor\n    else:\n        raise","preventionTips":["Subscribe to litellm releases and upgrade promptly — OCI schema fixes ship fast","Wrap per-vendor calls with a model fallback (Router with fallbacks) so parser drift degrades instead of failing","Enable response logging once when onboarding a new OCI model to catch shape changes in staging first"],"tags":["oci","cohere","pydantic","schema-drift","upstream-api"],"backgroundTag":"response-schema-validation-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}