fix(oci/chat): catch pydantic ValidationError when parsing OCI responses

Pydantic v2 raises ValidationError (not TypeError) when field validation
fails, so malformed OCI completion responses or stream chunks would
propagate unhandled out of handle_generic_response,
handle_generic_stream_chunk, and handle_cohere_stream_chunk. Widen the
except clauses to also catch ValidationError so callers get a clean
OCIError.
This commit is contained in:
mateo-berri 2026-05-21 06:09:53 +00:00
parent 1c6cd61311
commit e3ee32e1d9
No known key found for this signature in database
2 changed files with 6 additions and 3 deletions

View file

@ -10,6 +10,8 @@ import datetime
import json
from typing import Any, Dict, List, Optional
from pydantic import ValidationError
from litellm.llms.oci.chat.generic import _synthesize_oci_tool_call_id
from litellm.llms.oci.common_utils import (
OCI_JSON_TO_PYTHON_TYPES,
@ -273,7 +275,7 @@ def handle_cohere_stream_chunk(dict_chunk: dict) -> ModelResponseStream:
"""Parse a single Cohere SSE chunk into a LiteLLM ModelResponseStream."""
try:
typed_chunk = CohereStreamChunk(**dict_chunk)
except TypeError as e:
except (TypeError, ValidationError) as e:
raise OCIError(
status_code=500,
message=f"Chunk cannot be parsed as CohereStreamChunk: {str(e)}",

View file

@ -11,6 +11,7 @@ import hashlib
from typing import Dict, List, Optional, Union
import httpx
from pydantic import ValidationError
from litellm.llms.oci.common_utils import (
OCIError,
@ -315,7 +316,7 @@ def handle_generic_response(
"""Parse a non-streaming GENERIC OCI response into a LiteLLM ModelResponse."""
try:
completion_response = OCICompletionResponse(**json_data)
except TypeError as e:
except (TypeError, ValidationError) as e:
raise OCIError(
message=f"Response cannot be casted to OCICompletionResponse: {str(e)}",
status_code=raw_response.status_code,
@ -383,7 +384,7 @@ def handle_generic_stream_chunk(dict_chunk: dict) -> ModelResponseStream:
try:
typed_chunk = OCIStreamChunk(**dict_chunk)
except TypeError as e:
except (TypeError, ValidationError) as e:
raise OCIError(
status_code=500,
message=f"Chunk cannot be parsed as OCIStreamChunk: {str(e)}",