Wrap mid-stream httpx.TransportError as APIConnectionError#1550
Draft
mengtingli-ant wants to merge 1 commit into
Draft
Wrap mid-stream httpx.TransportError as APIConnectionError#1550mengtingli-ant wants to merge 1 commit into
mengtingli-ant wants to merge 1 commit into
Conversation
Mid-stream transport drops during SSE iteration (RemoteProtocolError, ReadError, ConnectError, …) leak through as bare httpx exceptions because the SDK's wrapping in _base_client._request only covers the pre-body request — once the SSE 200 is sent and body iteration starts, _iter_events has no try/except. Customers' standard `except anthropic.APIConnectionError:` retry ladders therefore miss mid-stream drops and have to know to also catch `httpx.TransportError`. This wraps mid-stream TransportError (in both Stream and AsyncStream) as APIConnectionError with the original preserved as __cause__, matching the pattern at _base_client.py:1104. TimeoutException (a TransportError subclass) passes through unchanged so it doesn't get double-wrapped — APITimeoutError is already an APIConnectionError subclass. Found while reproducing DeepSearchQA via the public API: 35/45 terminal question failures were RemoteProtocolError that the standard retry ladder missed. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
What
Wrap mid-stream
httpx.TransportError(during SSE body iteration) asanthropic.APIConnectionError, in bothStream._iter_eventsandAsyncStream._iter_events.Why
Mid-stream transport drops (
RemoteProtocolError,ReadError,ConnectError) currently leak through as barehttpxexceptions because the SDK's wrapping in_base_client._requestonly covers the pre-body request. Once the SSE 200 is sent and body iteration starts, there's no try/except in_iter_events.This means customers' standard retry ladders:
…miss mid-stream drops. They have to know to also catch
httpx.TransportError, which nobody discovers without debugging.Found while reproducing DeepSearchQA via the public API: 35/45 terminal question failures were
RemoteProtocolError("peer closed connection without sending complete message body")that the standard retry ladder missed. Context: Slack thread.Changes
Stream._iter_events/AsyncStream._iter_events: wraphttpx.TransportErrorasAPIConnectionError(same pattern as_base_client.py:1104); letTimeoutExceptionpass through unchanged so it doesn't get double-wrapped (APITimeoutErroris already anAPIConnectionErrorsubclass).RemoteProtocolErroris wrapped (with__cause__preserved); mid-streamReadTimeoutpasses through.Not in this PR
The follow-up — auto-retry the full request inside
MessageStream.get_final_message()on mid-streamAPIConnectionError— is a larger behavior change. This PR just makes the exception catchable with the right type.🤖 Generated with Claude Code