Repository navigation
fix(anthropic): surface mid-stream errors on the sync /v1/messages adapter - #43703
Open
revanth-045 wants to merge 1 commit into
Open
revanth-045 wants to merge 1 commit into
revanth-045 wants to merge 1 commit into
Conversation
…apter BerriAI#43601 taught the async adapter to convert a mid-stream provider failure into a terminal Anthropic `error` event instead of dropping the socket. The sync path, returned by `transformation.py` whenever `is_async` is False, never got the same treatment and was worse off: `__next__` caught every exception and raised `StopIteration`, so the SSE stream simply ended as though the response had completed. The client got a silently truncated answer with no error at all. Let `__next__` propagate, as `__anext__` already does, and give `anthropic_sse_wrapper` the same guard as `async_anthropic_sse_wrapper` so the failure is emitted as an Anthropic `error` event. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
revanth-045 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. |
Contributor
|
Contributor
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AnthropicStreamWrapperhas two SSE wrappers, and only the async one handles a mid-stream provider failure:transformation.pyreturns the sync one wheneveris_asyncis False, so it is a live path.The sync case is actually worse than the pre-#43601 async behaviour.
__next__ends with:So an upstream failure (the
BedrockErroron a ConverseStream that ends without a terminalmessageStop, per the existing regression tests) is swallowed and converted into a clean end-of-stream. The client sees a silently truncated answer and no error event — not even the "Connection closed mid-response" that the async fix was written for.__anext__has no such catch, which is why the async guard works.Fix
Two lines, mirroring the async path:
__next__propagates the exception instead of converting it toStopIteration(the log line stays).anthropic_sse_wrappergets the sametry/exceptasasync_anthropic_sse_wrapper, emitting_mid_stream_error_sse_event(e).On the
__next__change:AnthropicStreamWrapperis constructed in exactly one place (transformation.py:333) and is only ever consumed through one of the two SSE wrappers, so the propagated exception is caught immediately by the guard added here and never reaches a caller that previously saw a clean stop.Tests
Two cases added to the existing
test_streaming_iterator_mid_stream_error.py, as sync mirrors of the async ones already there: aBedrockErrorafter partial content ends the stream with a terminal Anthropicerrorevent carrying the upstream message, and draining the wrapper does not re-raise. Both fail onmain.tests/unit/llms/anthropic/pass_through/adapters371 passed;ruff checkandruff format --checkclean on the source file. (TheUP035/UP045findings in that test file are pre-existing onmainand untouched.)🤖 Generated with Claude Code