Skip to content

fix: map SSE error types to correct HTTP status codes in streaming#1263

Open
Jah-yee wants to merge 1 commit intoanthropics:mainfrom
Jah-yee:fix/sse-error-status-code
Open

fix: map SSE error types to correct HTTP status codes in streaming#1263
Jah-yee wants to merge 1 commit intoanthropics:mainfrom
Jah-yee:fix/sse-error-status-code

Conversation

@Jah-yee
Copy link

@Jah-yee Jah-yee commented Mar 19, 2026

Summary

When the API returns HTTP 200 (streaming started fine) but then sends an SSE error event like overloaded_error, the SDK was creating an APIStatusError with status_code=200 instead of the correct error code (e.g., 529 for overloaded).

This happened because _streaming.py passed the original HTTP response (status 200) to _make_status_error, which then selected the error subclass based on that 200 status code.

Fix

Added a mapping from SSE error types to HTTP status codes, and create a mock response with the correct status code when handling SSE errors in both sync and async streaming.

Changes

  • Added _SSE_ERROR_TYPE_TO_STATUS mapping in _streaming.py
  • Added _get_status_code_from_sse_error() helper function
  • Modified error handling in both Stream and AsyncStream to use the mapped status code

Testing

This fix ensures that:

  • overloaded_errorOverloadedError(status_code=529)
  • rate_limit_errorRateLimitError(status_code=429)
  • api_errorInternalServerError(status_code=500)
  • etc.

Fixes: #1258

@Jah-yee Jah-yee requested a review from a team as a code owner March 19, 2026 09:15
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