BerriAI/litellm · error · OCIError
Response cannot be casted to CohereChatResult: {e}
Error message
Response cannot be casted to CohereChatResult: {e} What it means
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.
Source
Thrown at litellm/llms/oci/chat/cohere.py:203
description=function_def.get("description", ""),
parameterDefinitions=parameter_definitions,
)
)
return cohere_tools
def handle_cohere_response(
json_response: dict,
model: str,
model_response: ModelResponse,
raw_response: httpx.Response,
) -> ModelResponse:
"""Parse a non-streaming Cohere OCI response into a LiteLLM ModelResponse."""
try:
cohere_response: Final = CohereChatResult(**json_response)
except (TypeError, ValidationError) as e:
raise OCIError(
message=f"Response cannot be casted to CohereChatResult: {e}",
status_code=raw_response.status_code,
)
model_response.model = model
model_response.created = int(datetime.datetime.now().timestamp())
response_text: Final = cohere_response.chatResponse.text
finish_reason: Final = _normalize_oci_finish_reason(cohere_response.chatResponse.finishReason)
tool_calls: list[dict[str, Any]] | None = None
if cohere_response.chatResponse.toolCalls:
tool_calls = [
{
"id": _synthesize_oci_tool_call_id(i, tc.name, json.dumps(tc.parameters, sort_keys=True)),
"type": "function",
"function": {
"name": tc.name,View on GitHub (pinned to 6c2dcb801b)
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.
Example fix
# before
resp = litellm.completion(model='oci/cohere.command-r-plus', messages=msgs) # raises OCIError
# after
# keep the call, but capture the raw shape for a bug report
import litellm, json
try:
resp = litellm.completion(model='oci/cohere.command-r-plus', messages=msgs)
except Exception as e:
print(str(e)) # field-level ValidationError detail
raise Defensive patterns
Strategy: try-catch
Try / catch
from litellm.llms.oci.common_utils import OCIError
try:
resp = litellm.completion(model='oci/cohere.command-r-plus', messages=msgs)
except OCIError as e:
if 'CohereChatResult' in str(e):
logger.error('OCI Cohere response schema drift: %s', e)
# provider-side contract mismatch — retrying identical input won't help;
# fall back to another model/provider or surface to the user
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Chunk cannot be parsed as CohereStreamChunk: {e}
- No results found in the response={raw_response_json}
- Failed to parse OCI embed response as JSON: {e}
- OCI embed response does not match expected schema: {e}
- Failed to parse Braintrust API response: {str(e)}
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/e2c7c9f81e0337c1.
Report an issue: GitHub.