Skip to content

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
tao-to-python:fix/sse-error-status-code
Open

fix(streaming): derive correct status code from SSE error type instead of HTTP 200#1269
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python:fix/sse-error-status-code

Conversation

@tao-to-python
Copy link

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 an APIStatusError with status_code=200 because _make_status_error dispatches on self.response.status_code.

This means:

  • OverloadedError (529) is never raised for mid-stream overload errors
  • RateLimitError (429) is never raised for mid-stream rate limits
  • Retry logic that catches specific error types doesn't work

Solution

Add an _SSE_ERROR_TYPE_TO_STATUS mapping that derives the correct HTTP status code from the error body type:

_SSE_ERROR_TYPE_TO_STATUS = {
    "overloaded_error": 529,
    "rate_limit_error": 429,
    "invalid_request_error": 400,
    ...
}

When a mapped error type is found, create a synthetic httpx.Response with the correct status code so _make_status_error returns the right error subclass.

Changes

File Change
src/anthropic/_streaming.py Add error type → status code mapping for sync + async paths

Fixes #1258

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
@tao-to-python tao-to-python requested a review from a team as a code owner March 20, 2026 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mid-stream SSE errors get status_code=200 instead of the actual error code

1 participant