Skip to content

fix(anthropic): surface response.failed in /v1/messages Responses bridge streaming - #39708

Closed
pengzh1 wants to merge 2 commits into
BerriAI:mainfrom
pengzh1:fix/issue-39703
Closed

pengzh1 wants to merge 2 commits into
BerriAI:mainfrom
pengzh1:fix/issue-39703

Conversation

@pengzh1

@pengzh1 pengzh1 commented Sep 4, 2026

Copy link
Copy Markdown

What changed

When /v1/messages streams through the Responses bridge, a provider-side failure that arrives inside an already-200 stream (response.failed, e.g. exhausted quota) never reached the client: it shared the response.completed branch and was relabelled as a successful empty turn (stop_reason: "end_turn", usage 0/0), and the MidStreamFallbackError raised by the Responses stream iterator was swallowed by a blanket except in __anext__, leaving a lone message_start and an unterminated stream.

Now response.failed and mid-stream MidStreamFallbackError are surfaced as the Anthropic streaming spec's terminal error event, carrying the upstream error type/message (status-mapped per the Anthropic error taxonomy). Successful response.completed / response.incomplete turns are unchanged.

Fixes #39703

Verification

  • New regression tests in tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_streaming_iterator.py: on the unfixed source all 4 fail (fabricated end_turn / swallowed raise), with the fix all pass — 173 passed in the directory, 939 passed across experimental_pass_through/ + anthropic/messages/.
  • ruff format --check / ruff check / ruff check --config ruff-tests.toml clean on touched files.
  • basedpyright per-rule delta vs base on the changed file: no rule increased (102 → 72 errors total).

@greptile-apps

greptile-apps Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR makes the Anthropic Messages Responses bridge emit terminal error events for failed Responses streams instead of reporting a successful empty turn or silently ending

  • Separates response.failed from successful completion handling and extracts upstream error details
  • Converts MidStreamFallbackError into an Anthropic error frame using status-based error classification
  • Adds regression coverage for object and dictionary errors, missing error details, successful completion, and mid-stream exceptions

Confidence Score: 4/5

The PR appears safe to merge after addressing the non-blocking source-style issue in the new failure conversion branch

The reachable Responses iterator raises one MidStreamFallbackError and then terminates, so the new handler emits one terminal error frame as intended; only maintainability conventions remain

Files Needing Attention: litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py

Important Files Changed

Filename Overview
litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py Correctly surfaces the reachable mid-stream failure path, with non-blocking repository-style violations in the new parsing branch
tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_streaming_iterator.py Adds focused regression tests covering failed stream conversion and preserving successful completion behavior

Reviews (1): Last reviewed commit: "fix(anthropic): surface response.failed ..." | Re-trigger Greptile

Comment on lines +240 to +266
# ---- response failed -> terminal error event ----
if event_type == "response.failed":
event_obj = cast("object", event)
failed_response: object | None
if _is_json_object(event_obj):
failed_response = event_obj.get("response")
else:
failed_response = getattr(event_obj, "response", None)

failed_error: object | None = None
if failed_response is not None:
if _is_json_object(failed_response):
failed_error = failed_response.get("error")
else:
failed_error = getattr(failed_response, "error", None)

error_type: str | None = None
error_message: str | None = None
if failed_error is not None:
if _is_json_object(failed_error):
raw_type: object | None = failed_error.get("type") or failed_error.get("code")
raw_message: object | None = failed_error.get("message")
else:
raw_type = getattr(failed_error, "type", None) or getattr(failed_error, "code", None)
raw_message = getattr(failed_error, "message", None)
if isinstance(raw_type, str):
error_type = raw_type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Mutable failure parsing

The new failure branches add narration and rebind parsing variables, increasing maintenance cost and violating the repository's immutable-code convention

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rebinding is intentional: the failure payload can arrive as either an attr-style object or a dict, and the two shapes parse through different paths before converging on the error event (the file's guard-ok/mutable-ok annotations cover this contract). A pure-helper refactor would add indirection without changing behavior — happy to do it if maintainers prefer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s fair. The rebinding is serving as shape normalization: mappings and attribute-style objects need different extraction paths before converging on the terminal error event. Given the existing guard/mutable-ok annotations and no demonstrated benefit from adding a helper, I’m withdrawing this comment; no refactor is needed.

@codspeed

codspeed Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing pengzh1:fix/issue-39703 (7dbd808) with litellm_internal_staging (e5da593)

Open in CodSpeed

@codecov

codecov Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...s_through/responses_adapters/streaming_iterator.py 97.43% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@pengzh1
pengzh1 requested a review from a team September 5, 2026 05:06
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


pengzh1 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

"type": "error",
"error": { # mutable-ok: nested Anthropic error object
"type": error_type or "api_error",
"message": error_message or "Upstream response failed",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low: Upstream error details bypass response redaction

A caller who triggers an in-stream failure receives the raw upstream message, including any credential-shaped values, filesystem paths, or internal hostnames present in it. Because this wrapper converts failures into SSE frames, the message bypasses redact_internal_details_from_client_message() in the proxy exception path; apply that redaction centrally in _make_error_event so both response.failed and MidStreamFallbackError are covered.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for flagging. This mirrors the non-streaming pass-through path, which forwards the upstream error body as-is, and the streaming layer has no redaction hook to apply selectively. If maintainers want redaction here it is a semantic change across both paths, so deferring that to review rather than unilaterally diverging the two.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detail. I can't automatically confirm this is safe to dismiss, so I'm leaving the thread open for a maintainer to make the call.

@veria-ai

veria-ai Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

PR overview

This PR updates the Anthropic Responses bridge so streaming /v1/messages requests surface upstream response.failed events to callers.

One security issue remains open in the streaming error path. In-stream failures can expose unredacted upstream error text to callers, potentially revealing credentials, filesystem paths, or internal hostnames when those details are present. No issues have yet been addressed.

Open issues (1)

Fixed/addressed: 0 · PR risk: 3/10

@pengzh1
pengzh1 force-pushed the fix/issue-39703 branch 2 times, most recently from 648d8e8 to 421f058 Compare September 6, 2026 08:08
pengzh1 added 2 commits September 10, 2026 18:08
…dge streaming

response.failed inside an already-200 stream used to share the
response.completed branch and was relabelled as a successful empty turn
(stop_reason end_turn), and the MidStreamFallbackError raised by the
Responses iterator in 1.97+ was swallowed by a blanket except, leaving an
unterminated stream. Emit the Anthropic streaming spec's terminal error
event carrying the upstream error instead.

Fixes BerriAI#39703
@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:48
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 14:44
@github-actions

Copy link
Copy Markdown
Contributor

#39703 was fixed by #43126 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants