From e3ee32e1d9d3c5373ac8be331fbd6769fd285958 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 21 May 2026 06:09:53 +0000 Subject: [PATCH] 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. --- litellm/llms/oci/chat/cohere.py | 4 +++- litellm/llms/oci/chat/generic.py | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/litellm/llms/oci/chat/cohere.py b/litellm/llms/oci/chat/cohere.py index dc611c9906c..7805f17c74c 100644 --- a/litellm/llms/oci/chat/cohere.py +++ b/litellm/llms/oci/chat/cohere.py @@ -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)}", diff --git a/litellm/llms/oci/chat/generic.py b/litellm/llms/oci/chat/generic.py index 8ded6c7a546..92b050004dc 100644 --- a/litellm/llms/oci/chat/generic.py +++ b/litellm/llms/oci/chat/generic.py @@ -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)}",