Skip to content

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]

[Bug Fix]: ErrorEvent ValidationError when OpenAI Responses API returns nested error structure (#15804)

Ishaan Jaffd91efa7a7b83
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))
+
+
+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