fix: handle missing usage metadata on premature Anthropic stream termination#1898
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: handle missing usage metadata on premature Anthropic stream termination#1898giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Conversation
3 tasks
…ination When the Anthropic API stream terminates before sending the message_stop event (e.g. network timeout, connection reset), the code crashes with AttributeError because event.message.usage is None. The stream() method unconditionally accessed event.message.usage after the async iteration loop, assuming a complete stream. Two failure modes: 1. Empty stream: 'event' variable is never assigned (UnboundLocalError) 2. Premature termination: event.message or event.message.usage is None Fix: Initialize event=None before the loop, use safe attribute access via getattr() chain, and emit zero-usage metadata with a warning log when usage data is unavailable. Added two regression tests: - test_stream_premature_termination: stream ends without message.usage - test_stream_empty_no_events: completely empty stream Fixes strands-agents#1868
732a8d6 to
ba341eb
Compare
Contributor
Author
|
Friendly ping — handles missing usage metadata on premature Anthropic stream termination, preventing |
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.
Problem
When the Anthropic API stream terminates before sending the
message_stopevent (e.g., network timeout, connection reset, server error mid-stream),AnthropicModel.stream()crashes withAttributeError.Crash site (line 412):
Two failure modes:
eventvariable is never assigned by theasync forloop →UnboundLocalErroreventlacks a.messageattribute or.message.usageisNone→AttributeError: 'NoneType' object has no attribute 'model_dump'Root Cause
The code unconditionally accesses
event.message.usageafter the async iteration loop, assuming the Anthropic stream always completes normally with usage metadata. This assumption fails when:message_stopFix
event = Nonebefore the loopgetattr()chain:getattr(getattr(event, "message", None), "usage", None)The agent can still process whatever partial response was received before the stream terminated.
Tests
Added two regression tests:
test_stream_premature_termination: Stream ends with an event that has no.messageattributetest_stream_empty_no_events: Completely empty stream (zero events)All 43 existing tests continue to pass.
Fixes #1868