fix(streaming): align non-beta accumulator with beta implementation#1586
Open
xodn348 wants to merge 1 commit into
Open
fix(streaming): align non-beta accumulator with beta implementation#1586xodn348 wants to merge 1 commit into
xodn348 wants to merge 1 commit into
Conversation
- Use to_dict() instead of model_dump() in content_block_start handler to match the beta version and the message_start handler in the same file. to_dict() uses API-compatible key names (use_api_names=True) and excludes unset fields by default, which is the correct behavior for construct_type(). - Use builtins.type() instead of bare type() in the error message to prevent potential shadowing, consistent with the beta implementation. - Update messages_stream.py shebang from rye to uv, consistent with all other examples and CONTRIBUTING.md guidelines.
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.
What
Fixes three inconsistencies between
_messages.py(non-beta) and_beta_messages.py(beta) in the streaming accumulator:model_dump()→to_dict()in thecontent_block_starthandler —to_dict()defaults touse_api_names=Trueandexclude_unset=True, which is correct forconstruct_type()and consistent with both themessage_starthandler in the same file (line 452) and the beta version.type(event)→builtins.type(event)in the error path — prevents potential shadowing of thetypebuiltin, matching the beta implementation.rye→uvshebang inexamples/messages_stream.py— the only example still referencingryeafter the project migrated touv(per CONTRIBUTING.md line 52).Why
The beta streaming accumulator appears to have received these fixes independently, but the non-beta version was not updated to match. The
model_dump()vsto_dict()difference is the most significant: if a content block type ever introduces field aliasing,model_dump()would pass Python-style keys whileconstruct_type()expects API-style keys, causing silent field drops.Verification