fix(bedrock): handle EOF consistently, fix stale events, and surface exceptions#303
Open
rishabh-emergent wants to merge 1 commit intoanthropics:mainfrom
Open
Conversation
…exceptions Three fixes for the Bedrock EventStream decoder: 1. Filter io.EOF on normal stream end (fixes anthropics#110) The SSE decoder returns nil from scn.Err() on normal EOF, but the EventStream decoder exposes io.EOF as an error via Err(). This inconsistency forces consumers to special-case Bedrock EOF handling. Now EOF is filtered at the source, matching SSE decoder behavior. 2. Prevent stale event on non-chunk event types When eventType != "chunk", e.evt was not updated but Next() returned true, causing consumers to re-process the previous event. Now non-chunk events are skipped via recursive Next() call. 3. Surface ExceptionMessageType errors (fixes anthropics#71) Go case statements don't fall through. The ExceptionMessageType case had its own handler that never reached the ErrorMessageType handler, causing Bedrock exceptions to be silently dropped. Merged both cases into a single handler.
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.
Summary
Three fixes for the Bedrock EventStream decoder in
bedrock/bedrock.go.Bug 1: EOF exposed as error (fixes #110)
The SSE decoder (
eventStreamDecoder) returnsnilfromscn.Err()on normal stream end. The EventStream decoder (eventstreamDecoder) exposesio.EOFas an error viaErr(). This forces consumers to special-case Bedrock EOF handling.Bug 2: Stale event on non-chunk event types
When
eventType != "chunk",e.evtis not updated butNext()returnstrue. Consumers re-process the previous event's data.Bug 3: ExceptionMessageType silently dropped (fixes #71)
Go
casestatements don't fall through.ExceptionMessageTypehad its own handler that never reachedErrorMessageType, causing Bedrock exceptions to be silently dropped.Impact
These bugs cause silent data loss when streaming from Bedrock:
We hit all three in production (391 errors/day) when Bedrock streams were prematurely terminated — the EOF was silently swallowed, and the incomplete response caused downstream failures.
Tests
All existing tests pass:
go test ./bedrock/... -v