fix: wrap mid-stream httpx.TransportError as APIConnectionError#1552
Open
blut-agent wants to merge 2 commits into
Open
fix: wrap mid-stream httpx.TransportError as APIConnectionError#1552blut-agent wants to merge 2 commits into
blut-agent wants to merge 2 commits into
Conversation
File upload helpers were sharing mutable containers nested inside file tuples, such as per-file headers dictionaries. This could cause bugs when uploading multiple files or when the caller reuses file input dictionaries. - Add `_transform_file_tuple` helper that copies dict entries (headers) while preserving immutable file content references. - Use the helper in both `_transform_file` and `_async_transform_file`. - Add regression test for file tuple with mutable headers. Fixes anthropics#1548
Wrap mid-stream httpx.TransportError (during SSE body iteration) as
anthropic.APIConnectionError, in both Stream._iter_events and
AsyncStream._iter_events.
Mid-stream transport drops (RemoteProtocolError, ReadError, ConnectError)
currently 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, there's no try/except in _iter_events.
This means customers' standard retry ladders:
except anthropic.APIConnectionError:
retry()
…miss mid-stream drops. They have to know to also catch httpx.TransportError,
which nobody discovers without debugging.
Changes:
- Stream._iter_events / AsyncStream._iter_events: wrap httpx.TransportError
as APIConnectionError (same pattern as _base_client.py:1104); let
TimeoutException pass through unchanged so it doesn't get double-wrapped
(APITimeoutError is already an APIConnectionError subclass).
- 2 tests: mid-stream RemoteProtocolError is wrapped (with __cause__ preserved);
mid-stream ReadTimeout passes through.
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.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.