Skip to content

fix(anthropic): normalize ParsedTextBlock to avoid Pydantic serialization warnings#2058

Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH:fix/anthropic-parsed-text-block
Open

fix(anthropic): normalize ParsedTextBlock to avoid Pydantic serialization warnings#2058
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH:fix/anthropic-parsed-text-block

Conversation

@Zelys-DFKH
Copy link
Copy Markdown

@Zelys-DFKH Zelys-DFKH commented Apr 4, 2026

Description

Anthropic SDK >= 0.84.0 returns `ParsedTextBlock` objects in `content_block_start` events. `ParsedTextBlock` is a `TextBlock` subclass with an extra `parsed_output` field that is not part of the discriminated union on `Message.content`. Calling `model_dump()` on those events triggers `PydanticSerializationUnexpectedValue` warnings on every agent invocation (6+ warnings per call), cluttering stderr even though serialization succeeds and results are correct.

The fix adds a `_normalize_event()` static method that converts `ParsedTextBlock` back to a plain `TextBlock` before `model_dump()` is called. This preserves all content data (text, type, citations) and removes the spurious `parsed_output` field that causes the discriminated-union mismatch.

Key details:

  • The import of `ParsedTextBlock` is guarded with `try/except ImportError` for SDK versions that do not ship it
  • Non-`content_block_start` events pass through unchanged
  • Events with a plain `TextBlock` also pass through unchanged

Related Issues

Fixes #1865

Documentation PR

N/A

Type of Change

Bug fix

Testing

  • Added three unit tests for `_normalize_event()` covering: ParsedTextBlock conversion, plain TextBlock passthrough, and non-`content_block_start` passthrough
  • All 45 existing tests pass

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…tion warnings

Anthropic SDK >= 0.84.0 returns ParsedTextBlock objects in content_block_start
events. ParsedTextBlock is a TextBlock subclass with an extra parsed_output field
that is not part of the discriminated union on Message.content. Calling
model_dump() on those events triggers PydanticSerializationUnexpectedValue
warnings on every agent invocation.

Add _normalize_event() to convert ParsedTextBlock back to a plain TextBlock
before model_dump(). The conversion preserves all content data (text, type,
citations) and is guarded with try/except ImportError for SDK versions that
do not ship ParsedTextBlock.

Fixes strands-agents#1865
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] Strands Agent AnthropicModel emits Pydantic serialization warnings with Anthropic SDK v0.84.0

1 participant