fix(streaming): preserve BetaCompactionBlock type during streaming accumulation#1200
Open
bledden wants to merge 1 commit intoanthropics:mainfrom
Open
fix(streaming): preserve BetaCompactionBlock type during streaming accumulation#1200bledden wants to merge 1 commit intoanthropics:mainfrom
bledden wants to merge 1 commit intoanthropics:mainfrom
Conversation
…cumulation When the beta streaming accumulator builds the final message snapshot, it reconstructs each content block by converting it to a dict and passing it through construct_type with the ParsedBetaContentBlock discriminated union. On some Python versions, the generic ParsedBetaTextBlock[T] variant in that union breaks Pydantic's discriminator resolution, causing all non-text blocks (including BetaCompactionBlock) to be incorrectly deserialized as ParsedBetaTextBlock. The fix: only text blocks need reconstruction through construct_type (to wrap them in ParsedBetaTextBlock which adds the parsed_output field). Non-text blocks are already the correct type from the event and can be appended directly to the snapshot. Fixes anthropics#1175
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
When using the beta streaming API with compaction enabled, the final message from
get_final_message()incorrectly typesBetaCompactionBlockcontent blocks asParsedBetaTextBlock. The block hastype='compaction'butisinstance(block, BetaCompactionBlock)returns False, and parasitic fields liketext,citations, andparsed_outputappear as None.The issue is in the streaming accumulator's
content_block_starthandling. It converts every content block to a dict and reconstructs it through theParsedBetaContentBlockdiscriminated union viaconstruct_type(). On some Python versions (reported on 3.12), the genericParsedBetaTextBlock[ResponseFormatT]variant in that union breaks Pydantic's discriminator resolution, causing all blocks to fall through to the first union variant (ParsedBetaTextBlock).The fix: only text blocks need reconstruction through
construct_type(they're the only type that needs wrapping inParsedBetaTextBlockto add theparsed_outputfield). Non-text blocks likeBetaCompactionBlock,BetaToolUseBlock,BetaThinkingBlock, etc. are already the correct type from the streaming event and can be appended directly.Added a test fixture simulating a compaction streaming response and two tests (sync + async) that verify
BetaCompactionBlockinstances are preserved in the final message. All 14 beta streaming tests pass.Fixes #1175