From d91efa7a7b832bcffea135802a94a4ac14b37296 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 22 Oct 2025 14:18:46 -0700 Subject: [PATCH] [Bug Fix]: ErrorEvent ValidationError when OpenAI Responses API returns nested error structure (#15804) * add ErrorEventError nested field * test_openai_responses_api_token_limit_error * test_openai_responses_api_token_limit_error --- litellm/types/llms/openai.py | 13 +++++++--- .../test_openai_responses_api.py | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/litellm/types/llms/openai.py b/litellm/types/llms/openai.py index b2d170514af..a9333e0061d 100644 --- a/litellm/types/llms/openai.py +++ b/litellm/types/llms/openai.py @@ -1408,13 +1408,20 @@ class ImageGenerationPartialImageEvent(BaseLiteLLMOpenAIResponseObject): b64_json: str -class ErrorEvent(BaseLiteLLMOpenAIResponseObject): - type: Literal[ResponsesAPIStreamEvents.ERROR] - code: Optional[str] +class ErrorEventError(BaseLiteLLMOpenAIResponseObject): + """Nested error object within ErrorEvent""" + type: str # e.g., 'invalid_request_error' + code: str # e.g., 'context_length_exceeded' message: str param: Optional[str] +class ErrorEvent(BaseLiteLLMOpenAIResponseObject): + type: Literal[ResponsesAPIStreamEvents.ERROR] + sequence_number: int + error: ErrorEventError + + class GenericEvent(BaseLiteLLMOpenAIResponseObject): type: str diff --git a/tests/llm_responses_api_testing/test_openai_responses_api.py b/tests/llm_responses_api_testing/test_openai_responses_api.py index b3947b26789..70624b02d14 100644 --- a/tests/llm_responses_api_testing/test_openai_responses_api.py +++ b/tests/llm_responses_api_testing/test_openai_responses_api.py @@ -1508,3 +1508,29 @@ async def test_basic_openai_responses_with_websearch(stream): print("chunk=", json.dumps(chunk, indent=4, default=str)) else: print("response=", json.dumps(response, indent=4, default=str)) + + +@pytest.mark.asyncio +async def test_openai_responses_api_token_limit_error(): + """ + Relevant issue: https://github.com/BerriAI/litellm/issues/15785 + + + When this fails you'll see: + "pydantic_core._pydantic_core.ValidationError: 3 validation errors for ErrorEvent" + in the console. + """ + litellm._turn_on_debug() + + # Generate text with >400k tokens to trigger token limit error + oversized_text = "This is a test sentence. " * 50000 # ~400k tokens + + # This will raise ValidationError instead of showing the real error + response = await litellm.aresponses( + model="gpt-5-mini", + input=oversized_text, + stream=True + ) + + async for event in response: + print(event) # Never reaches here - ValidationError is raised \ No newline at end of file