fix(streaming): resolve unchecked content block index and type safety issues - #1797
Open
vjaudir8123-debug wants to merge 1 commit into
Open
fix(streaming): resolve unchecked content block index and type safety issues#1797vjaudir8123-debug wants to merge 1 commit into
vjaudir8123-debug wants to merge 1 commit into
Conversation
… issues 1. Solves the `TODO: check index` by padding the content block list to the exact event index. Previously, `content_block_start` would blindly append, meaning out-of-order or skipped indices would cause data corruption or `IndexError`. 2. Replaces `ParsedMessage.construct()` with `construct_type(type_=ParsedMessage)` on `message_start`. Pydantic's `construct()` bypasses validation, which left the `usage` block as a raw dict. This would later crash the stream with `AttributeError: 'dict' object has no attribute 'output_tokens'` on subsequent usage deltas. Co-authored-by: Claude <noreply@anthropic.com>
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
This PR addresses two critical bugs in the streaming message parser (
_messages.pyand_beta_messages.py) that can cause runtime crashes or data corruption.1. Missing Content Block Index Validation
The parser previously had a
# TODO: check indexcomment and blindly appended new blocks oncontent_block_start. If events arrived out of order or an index was skipped, the subsequentcontent_block_deltawould either overwrite the wrong block or crash with anIndexError. This PR correctly pads the list up toevent.indexbefore inserting the block.2.
construct()Bypassing Type ValidationThe snapshot initialization used Pydantic
sconstruct(**event.message.to_dict()). Becauseconstruct()skips validation and deep coercion, nested fields likeusagewere left as raw Python dictionaries rather than Pydantic objects. When a subsequent usage delta arrived,current_snapshot.usage.output_tokens = ...would crash withAttributeError: 'dict' object has no attribute 'output_tokens'. This PR fixes it by usingconstruct_type(type_=...)`, ensuring correct instantiation of nested models.