fix: suppress PydanticSerializationUnexpectedValue warnings for Anthropic streaming#1790
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…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>
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
Fixes #1746
When using Anthropic SDK >= 0.83.0,
message_stopevents containParsedTextBlockobjects (a generic BaseModel subclass ofTextBlock) in theMessage.contentfield. Pydantic's serializer cannot matchParsedTextBlockagainst theContentBlockTypedDict union variants, producing noisy warnings for each failed match attempt:Root Cause
ParsedTextBlockis a generic Pydantic BaseModel (ParsedTextBlock[TypeVar]) that inherits fromTextBlock. Whenmodel_dump()is called on the streaming event, Pydantic tries each variant in theContentBlockunion and warns for every non-match. The serialization still produces correct output —ParsedTextBlockserializes identically toTextBlock— but the warnings confuse users.Fix
Added
warnings=Falsetoevent.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_reasonfrommessage_stop,deltafromcontent_block_delta), so the serialization of unusedParsedTextBlockcontent is irrelevant to the output.Tests
All 42 Anthropic model tests pass.