fix(bedrock): raise APIStatusError instead of ValueError for non-200 stream events#1486
Open
nileshpatil6 wants to merge 1 commit into
Open
fix(bedrock): raise APIStatusError instead of ValueError for non-200 stream events#1486nileshpatil6 wants to merge 1 commit into
nileshpatil6 wants to merge 1 commit into
Conversation
…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
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.
Fixes #1477
Problem
AWSEventStreamDecoder._parse_message_from_eventraises a rawValueErrorwhen 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 ownstatus_codefield.The raw
ValueErroris uncatchable as a standard SDK error, gives no structured information to the caller, and surfaces as a confusing crash.Fix
Replace the
ValueErrorwith a properAPIStatusErrorsubclass constructed from the event'sstatus_codeand parsed body:429->RateLimitError>= 500->InternalServerError400->BadRequestErrorAPIStatusErrorA synthetic
httpx.Responseis built from the event's status code so the exception contract (response,status_code,message) is fully satisfied.Before / After
Users can now handle these by type: