fix(bedrock): convert non-200 SSE error events to APIStatusError
Problem
When Bedrock returns a non-200 status code inside an SSE event frame (for example internalServerException with status_code: 400 or internalServerException with status_code: 500), AWSEventStreamDecoder._parse_message_from_event raises a raw ValueError:
ValueError: Bad response code, expected 200: {'status_code': 400, 'headers': {':exception-type': 'internalServerException', ...}, 'body': b'{"message":"The system encountered an unexpected error..."}'}
This ValueError is not part of the SDK error hierarchy, so user code that catches anthropic.APIError / anthropic.InternalServerError / etc. silently misses the error and has to special-case a raw Python built-in exception type.
Fixes #1477
Root cause
_parse_message_from_event used raise ValueError(...) directly instead of routing the error through the existing SSE error path.
Fix
Instead of raising ValueError, the method now returns a ServerSentEvent(event="error", data=<json>). The existing Stream.__stream__ and AsyncStream.__stream__ already handle event == "error" by calling self._client._make_status_error(...), which maps the exception to the correct APIStatusError subclass (InternalServerError, BadRequestError, etc.).
The error body is constructed from the Bedrock event headers (:exception-type) and the JSON body (message field), giving callers the full context they need.
What changed
src/anthropic/lib/bedrock/_stream_decoder.py: replaceraise ValueError(...)with a yieldedServerSentEvent(event="error", ...)containing the Bedrock exception type and message. Also changed the return type of_parse_message_from_eventfromstr | NonetoServerSentEvent | None(the callers already just checked truthiness and yielded the result), and updatediter_bytes/aiter_bytesto yield the returnedServerSentEventdirectly rather than constructing a new one.
Before / after
Before:
except ValueError:
# uncatchable via anthropic.APIError
After:
except anthropic.InternalServerError:
# properly caught, e.message contains Bedrock's error detail
Thanks for the detailed review @Zawwarsami16 -- all three points addressed in the latest commit:
1. Status-code-based exception mapping
The approach: _stream_decoder.py now embeds "_bedrock_status": <int> in the error SSE body JSON. Both BaseBedrockClient._make_status_error (_client.py) and BaseMantleClient._make_status_error (_mantle.py) now check body.get("_bedrock_status") first and use that status code for exception mapping instead of response.status_code (which is always 200 for Bedrock streams). So a frame with status 429 raises RateLimitError, 400 raises BadRequestError, etc.
2. Unit tests
Added tests/lib/test_bedrock_stream_decoder.py covering:
- Non-200 frames emit
ServerSentEvent(event="error")with_bedrock_statusembedded - 400/429/500 frame statuses map to
BadRequestError/RateLimitError/InternalServerError - Invalid UTF-8 body doesn't crash (uses
decode(errors="replace")) - Normal 200 path (no
_bedrock_statusin body) still usesresponse.status_code
3. str(raw_body) fallback
Changed to raw_body.decode(errors="replace") so the error message is readable text instead of b'...' repr.