Repository navigation
fix: guard stop_reason/stop_sequence/stop_details against None in accumulate_event - #1944
Open
priyadarshnisundararajan wants to merge 1 commit into
Conversation
…umulate_event A message_delta that omits the stop_* fields (all Optional on Delta) used to overwrite a previously accumulated stop_reason with None, while the neighbouring container field was already guarded. Only overwrite the accumulated values when the incoming delta actually carries them. Fixes anthropics#1940
This branch has not been deployed
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
In
accumulate_event'smessage_deltabranch,stop_reason,stop_sequence, andstop_detailswere assigned unconditionally fromevent.delta, while the neighbouringcontainerassignment was already guarded with anis not Nonecheck. Since all four fields areOptional[...] = NoneonDelta, a latermessage_deltathat omits them (e.g. a usage-only delta) would wipe a value an earlier delta had already set.This applies the same guard to the three
stop_*fields in both the regular accumulator (src/anthropic/lib/streaming/_messages.py) and the beta accumulator (src/anthropic/lib/streaming/_beta_messages.py).Why
The unconditional assignment contradicts the convention stated twenty lines below for usage fields ("optional ones are omitted when not applicable, in which case the message_start value must survive") and is visible to callers through
MessageStream.current_message_snapshot/get_final_message(). Code that branches onstop_reason(refusal handling,max_tokenscontinuation, tool-loop termination) can't distinguish the reset from "the stream never finished".Fixes #1940
Tests
RawMessageDeltaEvent.model_validateand the shippedaccumulate_event:stop_reasonnow staysend_turn(wasNonebefore).test_message_delta_omitted_stop_fields_keep_earlier_valuesintests/lib/streaming/test_messages.pyandtests/lib/streaming/test_beta_messages.py.