fix: guard ParsedMessageStopEvent.message type to suppress Pydantic warnings#1714
Open
lizradway wants to merge 1 commit into
Open
Conversation
Use TYPE_CHECKING guard on ParsedMessageStopEvent.message field to prevent Pydantic from building a serializer schema that expects ParsedTextBlock[TypeVar] in the content union. At runtime, the unparameterized ParsedMessage avoids the type mismatch that causes PydanticSerializationUnexpectedValue warnings on every streamed response. This follows the same pattern already used for ParsedContentBlockStopEvent.content_block in the same file.
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.
model_dump()onParsedMessageStopEventemitsPydanticSerializationUnexpectedValuewarnings on every streamed response because the unresolved generic in message:ParsedMessage[ResponseFormatT]confuses Pydantic’s serializer at runtime (see Pydantic ref)The fix is the same
TYPE_CHECKINGguard already used forParsedContentBlockStopEvent.content_blockright below, it was just missed here:It looks like this inconsistency was introduced in ad56677 and only shows up at serialization time rather than typechecking or validation, likely explaining why this was not caught
Reproduced on every SDK version from 0.84.0 through 0.113.0 with pydantic 2.9–2.13.
Testing
Verified with the following across multiple SDK/pydantic version combinations:
Also confirmed:
ruff check/ruff formatpasspyright/mypypass with 0 errorstests/test_streaming.py— 23 tests passRelated: #1175, #1422
Resolves: #1715