Skip to content

fix(streaming): handle dict objects that lack to_dict() in stream accumulator#1272

Open
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python:fix/stream-dict-to-dict-error
Open

fix(streaming): handle dict objects that lack to_dict() in stream accumulator#1272
tao-to-python wants to merge 1 commit intoanthropics:mainfrom
tao-to-python:fix/stream-dict-to-dict-error

Conversation

@tao-to-python
Copy link

Description

Fixes intermittent AttributeError: 'dict' object has no attribute 'to_dict' during beta streaming, as reported in #1088.

Problem

When using client.beta.messages.stream() with certain betas (e.g. effort-2025-11-24, code-execution-2025-08-25), the stream accumulator intermittently receives raw dict objects instead of Pydantic models for message_start and content_block_start events.

Calling .to_dict() on a plain dict raises AttributeError.

Solution

Add hasattr checks before calling .to_dict(), falling back to dict() conversion:

message_data = event.message.to_dict() if hasattr(event.message, "to_dict") else dict(event.message)

Applied to both _messages.py (non-beta) and _beta_messages.py (beta) for consistency.

Changes

File Change
src/anthropic/lib/streaming/_beta_messages.py Guard to_dict() calls on message + content_block
src/anthropic/lib/streaming/_messages.py Guard to_dict() call on message

Fixes #1088

…umulator

When using beta streaming with certain betas (e.g. effort, code-execution),
the accumulator intermittently receives raw dicts instead of Pydantic
models for message_start and content_block_start events. Calling
.to_dict() on a dict raises AttributeError.

Add hasattr checks to fall back to dict() when to_dict is not available,
in both beta and non-beta streaming paths.

Fixes anthropics#1088
@tao-to-python tao-to-python requested a review from a team as a code owner March 20, 2026 02:27
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.

Intermittent AttributeError in beta.messages.stream() with effort parameter: 'dict' object has no attribute 'to_dict'

1 participant