Skip to content

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

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

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

@tomer-ant

Copy link
Copy Markdown
Contributor

Thanks, but this is fixed in v0.110.0 (ed0af5b). The Bedrock stream decoder now forwards the payload's type as the SSE event type instead of hardcoding completion.

@tomer-ant tomer-ant closed this Aug 17, 2026
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)

2 participants