fix: handle error SSE events from Bedrock without AttributeError#1572
Open
pintaste wants to merge 1 commit into
Open
fix: handle error SSE events from Bedrock without AttributeError#1572pintaste wants to merge 1 commit into
pintaste wants to merge 1 commit into
Conversation
Bedrock delivers all event-stream frames over HTTP 200, including error
payloads such as rate-limit or model-not-found responses. Previously,
`AWSEventStreamDecoder` emitted every decoded frame as
`ServerSentEvent(event="completion")`, so a payload like
{"type": "error", "error": {"type": "rate_limit_error", ...}}
was passed to `_process_response_data` and cast to the stream event
union type. The cast produced a `RawMessageStartEvent` whose required
`message` field was `None`, causing downstream code to raise
`AttributeError: 'NoneType' object has no attribute 'model'` (or
`'usage'`, etc.) instead of a clean `APIStatusError`.
Fix: add `_sse_event_type()` which peeks at the decoded JSON and returns
`"error"` when `type == "error"`. The `Stream.__stream__` /
`AsyncStream.__stream__` methods in `_streaming.py` already contain a
handler for `sse.event == "error"` that raises `_make_status_error`, so
this routes the payload through the correct path without any additional
changes to the streaming core.
Fixes anthropics#1472
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.
Problem
Bedrock delivers all event-stream frames over HTTP 200, including error payloads (rate-limit, model-not-found, etc.).
AWSEventStreamDecoderpreviously emitted every decoded frame asServerSentEvent(event=\"completion\"), so a Bedrock error payload such as:{"type": "error", "error": {"type": "rate_limit_error", "message": "Rate limited", "details": null}, "request_id": "req_..."}was routed to
_process_response_dataand cast to the stream event union type. The cast produced aRawMessageStartEventwhose requiredmessagefield wasNone, causing:This was intermittently reproducible with
global.anthropic.claude-opus-4-7cross-region inference profiles on Bedrock.Fix
Add
_sse_event_type()toAWSEventStreamDecoderwhich inspects the decoded JSON and returns"error"whentype == "error". The existingStream.__stream__/AsyncStream.__stream__methods in_streaming.pyalready contain a handler forsse.event == \"error\"that raises_make_status_error, so this routes the payload through the correct path and surfaces a cleanAPIStatusErrorinstead of anAttributeError.No changes to the streaming core are required; the fix is entirely contained in
src/anthropic/lib/bedrock/_stream_decoder.py.Fixes #1472