{"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":"http","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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/oci/chat/cohere.py#L185-L221","documentation":"After a successful HTTP call to OCI's managed Cohere endpoint, LiteLLM validates the JSON body against the `CohereChatResult` Pydantic model. If the payload does not match (TypeError/ValidationError), it raises OCIError carrying the upstream HTTP status code with the message 'Response cannot be casted to CohereChatResult: {validation details}'. This means OCI returned a 200-shaped body whose schema drifted from what the adapter expects — a provider-side contract mismatch, not a caller input error.","triggerScenarios":"Non-streaming completion on an OCI Cohere model (e.g. cohere.command-r-plus via the oci/ prefix) where the response JSON is missing required fields (chatResponse.text, finishReason), renames them, or returns an error envelope with HTTP 200. Also happens when OCI changes its inference API shape or when the wrong apiFormat/model routing causes a generic-OCI payload to be parsed as Cohere.","commonSituations":"OCI ships a model/endpoint revision with a new response envelope; proxying through a gateway that rewrites the body; pinning litellm to an old adapter while the OCI API evolves; using a preview/ga switchover endpoint whose error bodies come back with status 200.","solutions":["Upgrade litellm to the latest patch release — the CohereChatResult model may already have been updated for the new OCI schema.","Log the raw response body next to the exception to identify the exact field mismatch (the {e} text names the offending field).","Verify you are calling a Cohere-family model through the Cohere path (model string and apiFormat COHERE), not mixing it with the GENERIC adapter.","If the mismatch persists, open a litellm GitHub issue with the sanitized response JSON so the type can be extended."],"exampleFix":"# before\nresp = litellm.completion(model='oci/cohere.command-r-plus', messages=msgs)  # raises OCIError\n\n# after\n# keep the call, but capture the raw shape for a bug report\nimport litellm, json\ntry:\n    resp = litellm.completion(model='oci/cohere.command-r-plus', messages=msgs)\nexcept Exception as e:\n    print(str(e))  # field-level ValidationError detail\n    raise","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm.llms.oci.common_utils import OCIError\n\ntry:\n    resp = litellm.completion(model='oci/cohere.command-r-plus', messages=msgs)\nexcept OCIError as e:\n    if 'CohereChatResult' in str(e):\n        logger.error('OCI Cohere response schema drift: %s', e)\n        # provider-side contract mismatch — retrying identical input won't help;\n        # fall back to another model/provider or surface to the user\n        raise","preventionTips":["Pin litellm to a recent patch and upgrade soon after OCI announces inference API changes.","Wrap provider adapters with error classification so schema-drift errors route to fallbacks rather than retries.","Log raw response bodies (redacted) for 200-status surprises to speed up bug reports."],"tags":["oci","cohere","response-parsing","schema-drift","pydantic"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}