The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Fix/issue 16759 streaming error validation (#17242)
Wei-Chiet Kud43c07765342
diff --git a/litellm/llms/openai/responses/transformation.py b/litellm/llms/openai/responses/transformation.pyindex f75213b0688..4c9d3828383 100644--- a/litellm/llms/openai/responses/transformation.py+++ b/litellm/llms/openai/responses/transformation.py@@ -238,6 +238,24 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig):event_pydantic_model = OpenAIResponsesAPIConfig.get_event_model_class(event_type=event_type)+ # Defensive: Some OpenAI-compatible providers may send `error.code: null`.+ # Pydantic will raise a ValidationError when it expects a string but gets None.+ # Coalesce a None `error.code` to a stable default string so streaming+ # iteration does not crash (see issue report). This keeps behavior similar+ # to previous fixes (coalesce before validation) and lets higher-level+ # handlers still receive an `ErrorEvent` object.+ try:+ error_obj = parsed_chunk.get("error")+ if isinstance(error_obj, dict) and error_obj.get("code") is None:+ # Preserve other fields, but ensure `code` is a non-null string+ parsed_chunk = dict(parsed_chunk)+ parsed_chunk["error"] = dict(error_obj)+ parsed_chunk["error"]["code"] = "unknown_error"+ except Exception:+ # If anything unexpected happens here, fall back to attempting+ # instantiation and let higher-level handlers manage errors.+ verbose_logger.debug("Failed to coalesce error.code in parsed_chunk")+return event_pydantic_model(**parsed_chunk)@staticmethoddiff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.pyindex fa5231a2a2f..074378fd562 100644--- a/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py+++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py@@ -390,6 +390,33 @@ class TestOpenAIResponsesAPIConfig:assert result["partial_images"] == partial_images_valueassert result["stream"] is True+ def test_transform_streaming_response_coalesces_null_error_code(self):+ """Ensure that when a streaming error event contains error.code=None,+ transform_streaming_response coalesces it to 'unknown_error' and returns+ an ErrorEvent instance without raising a ValidationError.+ """+ from litellm.types.llms.openai import ErrorEvent++ parsed_chunk = {+ "type": "error",+ "sequence_number": 1,+ "error": {+ "type": "invalid_request_error",+ "code": None,+ "message": "Something went wrong",+ "param": None,+ },+ }++ event = self.config.transform_streaming_response(+ model=self.model, parsed_chunk=parsed_chunk, logging_obj=self.logging_obj+ )++ # Validate returned type and coalesced code+ assert isinstance(event, ErrorEvent)+ assert event.error.code == "unknown_error"+ assert event.error.message == "Something went wrong"+class TestAzureResponsesAPIConfig:def setup_method(self):