Skip to content

fix(models/anthropic): eliminate ParsedTextBlock Pydantic serialization warnings#2821

Open
FadelT wants to merge 1 commit into
strands-agents:mainfrom
FadelT:fix/parsed-text-block-serialization-warnings
Open

fix(models/anthropic): eliminate ParsedTextBlock Pydantic serialization warnings#2821
FadelT wants to merge 1 commit into
strands-agents:mainfrom
FadelT:fix/parsed-text-block-serialization-warnings

Conversation

@FadelT

@FadelT FadelT commented Jun 16, 2026

Copy link
Copy Markdown

Problem

When using strands-agents >= v1.29.0 with anthropic SDK >= v0.84.0, every agent invocation using AnthropicModel produces 6+ Pydantic serialization warnings on stderr. The Anthropic SDK returns ParsedTextBlock objects in streaming responses, but calling event.model_dump() triggers serialization warnings because ParsedTextBlock doesn't match the discriminated union schema.

Impact:

  • Affects all Strands users with anthropic >= 0.84.0
  • Produces noisy stderr output (6+ warnings per agent call)
  • Particularly problematic for demos and getting-started experiences
  • Creates confusion - developers may think their code is broken

Closes #1865

Solution

Extract a _build_event_dict() helper method that constructs event dicts by directly accessing typed attributes instead of calling model_dump(). This approach:

  1. Follows proven pattern - Same technique as the existing message_stop fix from [BUG] PydanticSerializationUnexpectedValue warnings when Anthropic SDK returns ParsedTextBlock in message content #1746
  2. Reduces complexity - Extracts nested conditional logic into a focused helper
  3. Maintains compatibility - No changes to public API or behavior
  4. Complete coverage - Handles all event types: message_start, content_block_start, content_block_delta, content_block_stop, message_stop

Why this approach:
The Anthropic SDK's ParsedTextBlock objects 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:

  • ✅ Text streaming - no warnings
  • ✅ Tool-use streaming - no warnings
  • ✅ All event types exercised

Unit Tests

  • ✅ 59/59 tests passing
  • ✅ Added 2 comprehensive warning-detection tests:
    • test_stream_text_no_pydantic_warnings
    • test_stream_tool_use_no_pydantic_warnings
  • ✅ Updated existing test mocks to match new implementation

Changes

src/strands/models/anthropic.py

  • Add _build_event_dict() static method - constructs dicts without Pydantic serialization
  • Refactor stream() to use helper for all event types (reduces cognitive complexity)

tests/strands/models/test_anthropic.py

  • Fix mock objects to use proper attribute assignment (not constructor kwargs)
  • Add comprehensive warning-free streaming tests for text and tool-use
  • Update test_structured_output mocks to match new pattern

Checklist

References

@github-actions github-actions Bot added size/m area-model Related to models or model providers python Pull requests that update python code bug Something isn't working labels Jun 16, 2026
…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.
@FadelT FadelT force-pushed the fix/parsed-text-block-serialization-warnings branch from 6ca03a9 to a1c0579 Compare June 16, 2026 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-model Related to models or model providers bug Something isn't working python Pull requests that update python code size/m

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Strands Agent AnthropicModel emits Pydantic serialization warnings with Anthropic SDK v0.84.0

1 participant