fix(models/anthropic): eliminate ParsedTextBlock Pydantic serialization warnings#2821
Open
FadelT wants to merge 1 commit into
Open
fix(models/anthropic): eliminate ParsedTextBlock Pydantic serialization warnings#2821FadelT wants to merge 1 commit into
FadelT wants to merge 1 commit into
Conversation
…on warnings Fixes strands-agents#1865 When the Anthropic SDK (>=0.84.0) returns ParsedTextBlock objects in streaming responses, calling event.model_dump() triggers Pydantic serialization warnings on stderr. These warnings appear for message_start, content_block_start, content_block_delta, and content_block_stop events. This change extracts a _build_event_dict() helper that constructs event dicts by directly accessing typed attributes instead of using model_dump(). This approach follows the same pattern as the existing fix for message_stop events (issue strands-agents#1746). Changes: - Add _build_event_dict() static method to build dicts without Pydantic serialization - Refactor stream() to use the helper for all event types - Update tests to properly mock event attributes - Add comprehensive warning-free streaming tests for text and tool-use Verified with anthropic SDK 0.109.2 - zero Pydantic warnings emitted. All 59 unit tests passing.
6ca03a9 to
a1c0579
Compare
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.
Problem
When using
strands-agents >= v1.29.0withanthropic SDK >= v0.84.0, every agent invocation usingAnthropicModelproduces 6+ Pydantic serialization warnings on stderr. The Anthropic SDK returnsParsedTextBlockobjects in streaming responses, but callingevent.model_dump()triggers serialization warnings becauseParsedTextBlockdoesn't match the discriminated union schema.Impact:
anthropic >= 0.84.0Closes #1865
Solution
Extract a
_build_event_dict()helper method that constructs event dicts by directly accessing typed attributes instead of callingmodel_dump(). This approach:message_stopfix from [BUG] PydanticSerializationUnexpectedValue warnings when Anthropic SDK returns ParsedTextBlock in message content #1746message_start,content_block_start,content_block_delta,content_block_stop,message_stopWhy this approach:
The Anthropic SDK's
ParsedTextBlockobjects have all required attributes accessible directly. Building dicts from these typed attributes bypasses Pydantic's serialization layer entirely, eliminating the warnings without changing SDK contracts.Verification
Real SDK Testing (anthropic==0.109.2)
Tested with actual Anthropic API calls using
claude-sonnet-4-6:Before fix: Multiple Pydantic warnings on stderr
After fix: Zero warnings ✓
Test coverage:
Unit Tests
test_stream_text_no_pydantic_warningstest_stream_tool_use_no_pydantic_warningsChanges
src/strands/models/anthropic.py_build_event_dict()static method - constructs dicts without Pydantic serializationstream()to use helper for all event types (reduces cognitive complexity)tests/strands/models/test_anthropic.pytest_structured_outputmocks to match new patternChecklist
References