Skip to content

fix: suppress PydanticSerializationUnexpectedValue warnings for Anthropic streaming#1790

Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/parsed-text-block-serialization-warnings
Open

fix: suppress PydanticSerializationUnexpectedValue warnings for Anthropic streaming#1790
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/parsed-text-block-serialization-warnings

Conversation

@giulio-leone
Copy link

Summary

Fixes #1746

When using Anthropic SDK >= 0.83.0, message_stop events contain ParsedTextBlock objects (a generic BaseModel subclass of TextBlock) in the Message.content field. Pydantic's serializer cannot match ParsedTextBlock against the ContentBlock TypedDict union variants, producing noisy warnings for each failed match attempt:

PydanticSerializationUnexpectedValue(Expected `ThinkingBlock` - serialized value may not be as expected ...)
PydanticSerializationUnexpectedValue(Expected `ToolUseBlock` - serialized value may not be as expected ...)
...

Root Cause

ParsedTextBlock is a generic Pydantic BaseModel (ParsedTextBlock[TypeVar]) that inherits from TextBlock. When model_dump() is called on the streaming event, Pydantic tries each variant in the ContentBlock union and warns for every non-match. The serialization still produces correct output — ParsedTextBlock serializes identically to TextBlock — but the warnings confuse users.

Fix

Added warnings=False to event.model_dump() call in the streaming loop. This suppresses the non-critical serialization warnings while preserving correct serialization behavior.

The format_chunk() method only extracts specific fields from each event type (e.g., stop_reason from message_stop, delta from content_block_delta), so the serialization of unused ParsedTextBlock content is irrelevant to the output.

Tests

All 42 Anthropic model tests pass.

…opic streaming events

When using Anthropic SDK >= 0.83.0, message_stop events contain
ParsedTextBlock objects (a generic BaseModel subclass of TextBlock)
in the Message.content field. Pydantic's serializer cannot match
ParsedTextBlock against the ContentBlock TypedDict union variants,
producing noisy warnings for each failed match attempt.

The serialization still produces correct output — ParsedTextBlock
serializes identically to TextBlock — but the warnings confuse users.

Added warnings=False to event.model_dump() to suppress these
non-critical serialization warnings during streaming.

Fixes strands-agents#1746

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] PydanticSerializationUnexpectedValue warnings when Anthropic SDK returns ParsedTextBlock in message content

1 participant