Repository navigation
fix(streaming): tolerate message_start without usage - #2037
Open
Sanath91009 wants to merge 1 commit into
Open
Sanath91009 wants to merge 1 commit into
Sanath91009 wants to merge 1 commit into
Conversation
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 breaks
Streaming a message whose
message_startevent carries nousageobject crashes the accumulatorpartway through the stream:
construct_typefills the missing field withNone, andaccumulate_eventthen writes straightthrough it on the first
message_delta:The failure is easy to miss in testing because the usual fixtures always include usage in
message_start. The streaming docs' own thinking example does not, and any intermediary that stripsor reshapes the first event produces the same shape.
Reproduced offline with an
httpx2.MockTransportreplaying that event order, no API key:Cause
src/anthropic/lib/streaming/_messages.pyand the beta equivalent, inaccumulate_event: themessage_deltabranch assumescurrent_snapshot.usageis always populated bymessage_start. Itis not, and the type only promises
Usage | None.Fix
Seed the snapshot's usage from the first delta that carries one, and let a stream that never carries
usage finish with
usage=Noneinstead of raising:event.usageis present and the snapshot has no usage yet, build one from the delta and attachit before writing the cumulative counters
still leaves the
message_startvalue intactApplied identically to
_messages.pyand_beta_messages.py, which duplicate this accumulator.Tests
Four regression tests per module (eight total) in
tests/lib/streaming/, plus two recorded responsefixtures:
message_startwithout usage, delta with usage: accumulator seeds it and finishes with correct countsmessage_startwithout usage, no delta ever supplies usage: stream finishes withusage=None, no raisesync and async clients both covered
with the fix
83 passed (test_messages.py + test_beta_messages.py)
fix reverted, same files
8 failed, 75 passed
No other test changed behaviour.
ruff checkandruff format --checkpass on every touched file.Compatibility
No public API change.
usageis already typedUsage | None; this makes the runtime match the typeinstead of assuming it away. Users who stream from endpoints that always send usage see no
difference.