Skip to content

fix(responses): preserve top-level fields on in-stream error events - #45142

Closed
zichen0116 wants to merge 1 commit into
BerriAI:mainfrom
zichen0116:fix-responses-top-level-error-event
Closed

zichen0116 wants to merge 1 commit into
BerriAI:mainfrom
zichen0116:fix-responses-top-level-error-event

Conversation

@zichen0116

Copy link
Copy Markdown

TLDR

Problem this solves:

  • A Responses API stream ending with an OpenAI-shaped in-stream error event loses the provider code and message
  • The caller sees a generic retryable 500, so routers walk the fallback chain on caller faults

How it solves it:

  • When an error event has no nested error object, read the top-level code, message and param fields
  • invalid_prompt now raises BadRequestError 400 directly instead of a fallback-eligible 500

User Flow

Before: a caller fault mid-stream surfaces as a retryable 500 with the provider reason lost

  1. They stream POST /v1/responses and the provider ends the stream with an error event carrying code: invalid_prompt
  2. The client gets a 500 MidStreamFallbackError saying only "Response API in-stream error"
  3. Their router treats it as retryable and walks the fallback chain

After: the same event surfaces the provider reason as a 400 with no fallback

  1. They stream the same POST /v1/responses and the provider ends it with the same error event
  2. The client gets a 400 BadRequestError carrying invalid_prompt and the provider message
  3. No fallback is triggered for this caller fault

Relevant issues

Fixes #45122

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

Repro is the issue's own stub-server script: a local HTTP server streams response.created then the OpenAI-shaped error event (code: invalid_prompt) through litellm.aresponses(model="openai/gpt-4.1", stream=True), no provider key needed

Before (f0031e9)

  1. Run the repro script against the base commit
  2. Observed: MidStreamFallbackError InternalServerError 500 ... Response API in-stream error

After (18f86a2)

  1. Run the same repro script against the fixed commit
  2. Observed: BadRequestError BadRequestError 400 ... Invalid prompt: your prompt was flagged.

Mutation check on the new unit test: it fails on the base commit (raises MidStreamFallbackError) and passes with the fix; the full tests/unit/responses/test_streaming_iterator_error_events.py file passes (60 passed), and ruff check plus format are clean on both changed files

Type

🐛 Bug Fix

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@CLAassistant

CLAassistant commented Oct 7, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Changes error handling in the streaming response iterator.

This PR appears safe to merge

What we checked:

  • Caller errors could still trigger fallback: The parser keeps the event’s fields through model_construct. The mapper recognizes invalid_prompt as 400, and the iterator raises non-retryable errors directly

Summary

Preserves top-level error fields when a Responses stream event has no nested error

  • invalid_prompt reaches the existing mapper as a 400 instead of a generic 500
  • Nested errors and response.failed keep their existing paths
  • Adds a focused regression test without weakening the null-error test

Reviews (1) · Last reviewed commit: "fix(responses): preserve top-level field..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing zichen0116:fix-responses-top-level-error-event (9b93415) with main (44bc165)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (8f85de7) during the generation of this report, so 44bc165 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Oct 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

Thanks for this. Superseded by #46438, now merged, which reads the same top-level code and message fields, so closing this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Responses in-stream error event in OpenAI's top-level shape loses its code and becomes a fallback-eligible 500

3 participants