[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
This commit is contained in:
Ishaan Jaff 2025-10-22 14:18:46 -07:00 • committed by GitHub
parent 2ab2d15efc
commit d91efa7a7b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 3 deletions

View file

@ -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

View file

@ -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