fix(streaming): derive correct status code from SSE error type instead of HTTP 200#1269
Open
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
Open
fix(streaming): derive correct status code from SSE error type instead of HTTP 200#1269tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
Conversation
When a mid-stream SSE error arrives (e.g. overloaded_error), the HTTP response already has status 200 because the stream started successfully. _make_status_error dispatches on response.status_code, so it falls through to a generic APIStatusError instead of the specific subclass. Map the error type from the SSE body to the correct HTTP status code: - overloaded_error -> 529 (OverloadedError) - rate_limit_error -> 429 (RateLimitError) - invalid_request_error -> 400 (BadRequestError) - etc. This affects both sync (Stream) and async (AsyncStream) paths. Fixes anthropics#1258
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.
Description
Fixes incorrect error classification for mid-stream SSE errors, as reported in #1258.
Problem
When the API starts streaming (HTTP 200) then sends an SSE error event like
overloaded_error, the SDK creates anAPIStatusErrorwithstatus_code=200because_make_status_errordispatches onself.response.status_code.This means:
OverloadedError(529) is never raised for mid-stream overload errorsRateLimitError(429) is never raised for mid-stream rate limitsSolution
Add an
_SSE_ERROR_TYPE_TO_STATUSmapping that derives the correct HTTP status code from the error body type:When a mapped error type is found, create a synthetic
httpx.Responsewith the correct status code so_make_status_errorreturns the right error subclass.Changes
src/anthropic/_streaming.pyFixes #1258