Skip to content

fix(bedrock): preserve SSE event type in stream decoder instead of hardcoding "completion"#1651

Open
MaitreyeeDeshmukh wants to merge 1 commit into
anthropics:mainfrom
MaitreyeeDeshmukh:fix/bedrock-stream-decoder-drops-sse-event-type
Open

fix(bedrock): preserve SSE event type in stream decoder instead of hardcoding "completion"#1651
MaitreyeeDeshmukh wants to merge 1 commit into
anthropics:mainfrom
MaitreyeeDeshmukh:fix/bedrock-stream-decoder-drops-sse-event-type

Conversation

@MaitreyeeDeshmukh

Copy link
Copy Markdown

Summary

AWSEventStreamDecoder.iter_bytes() and aiter_bytes() in src/anthropic/lib/bedrock/_stream_decoder.py hardcode event="completion" for every ServerSentEvent they yield, discarding the real event type from the decoded chunk.

This causes downstream issues in _streaming.py's __stream__ method, which relies on sse.event to backfill data["type"] when it is missing. Chunks without a type field (such as Bedrock's amazon-bedrock-invocationMetrics trailer) fall back to construct_type against the stream-event union, which picks the first union member — yielding RawMessageStartEvent(message=None), an object that violates the SDK's own type contract.

Fix

Parse the "type" field from the decoded JSON bytes and pass it as the event argument to ServerSentEvent. Falls back to "completion" if the bytes cannot be parsed as JSON, preserving existing behavior for malformed chunks.

Changed file: src/anthropic/lib/bedrock/_stream_decoder.py — 3 lines changed in each of iter_bytes and aiter_bytes.

Testing

Added two regression tests to tests/lib/test_bedrock.py:

  • test_stream_decoder_preserves_sse_event_type (sync)
  • test_stream_decoder_preserves_sse_event_type_async (async)

Both assert that a chunk with "type": "message_start" yields a ServerSentEvent with event == "message_start", not "completion". All 14 tests pass across Python 3.9, 3.14 with both Pydantic v1 and v2.

Closes #1647

@MaitreyeeDeshmukh MaitreyeeDeshmukh requested a review from a team as a code owner June 4, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bedrock stream decoder drops SSE event types; type-less chunks construct as RawMessageStartEvent(message=None)

1 participant