Skip to content

fix(bedrock): raise APIStatusError instead of ValueError for non-200 stream events#1486

Open
nileshpatil6 wants to merge 1 commit into
anthropics:mainfrom
nileshpatil6:fix/bedrock-stream-error-event
Open

fix(bedrock): raise APIStatusError instead of ValueError for non-200 stream events#1486
nileshpatil6 wants to merge 1 commit into
anthropics:mainfrom
nileshpatil6:fix/bedrock-stream-error-event

Conversation

@nileshpatil6
Copy link
Copy Markdown

Fixes #1477

Problem

AWSEventStreamDecoder._parse_message_from_event raises a raw ValueError when Bedrock returns a non-200 status code inside the event stream (e.g. internalServerException, throttlingException). This happens independently of the HTTP response status -- Bedrock signals these errors through the event stream protocol with the event's own status_code field.

The raw ValueError is uncatchable as a standard SDK error, gives no structured information to the caller, and surfaces as a confusing crash.

Fix

Replace the ValueError with a proper APIStatusError subclass constructed from the event's status_code and parsed body:

  • 429 -> RateLimitError
  • >= 500 -> InternalServerError
  • 400 -> BadRequestError
  • anything else -> APIStatusError

A synthetic httpx.Response is built from the event's status code so the exception contract (response, status_code, message) is fully satisfied.

Before / After

# Before -- uncatchable, no structured info
ValueError: Bad response code, expected 200: {'status_code': 400, 'headers': {':exception-type': 'internalServerException', ...}, 'body': b'...'}

# After -- catchable, structured
anthropic.InternalServerError: The system encountered an unexpected error during processing. Try your request again.

Users can now handle these by type:

from anthropic import InternalServerError, RateLimitError

async with client.messages.stream(...) as stream:
    try:
        async for chunk in stream:
            ...
    except RateLimitError:
        # retry
    except InternalServerError:
        # log and retry

…stream events

Bedrock can return error payloads mid-stream via the event stream protocol
(e.g. internalServerException, throttlingException with status 400/429/500).
_parse_message_from_event was raising a raw ValueError which is uncatchable
as a standard SDK error and gives no structured info to the caller.

Replace the ValueError with a proper APIStatusError subclass (RateLimitError,
InternalServerError, BadRequestError, or APIStatusError) built from the
event's status code and body. Users can now catch these by type and inspect
.status_code / .message as with any other SDK error.

Fixes anthropics#1477
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bedrock streaming SSE events errors are not handled gracefully

2 participants