fix: handle premature stream termination for Anthropic (#1868)#2047
Open
gautamsirdeshmukh wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: handle premature stream termination for Anthropic (#1868)#2047gautamsirdeshmukh wants to merge 1 commit intostrands-agents:mainfrom
gautamsirdeshmukh wants to merge 1 commit intostrands-agents:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Author
|
Looking into why 1-3 integ tests continue to time out intermittently (this change to the Anthropic stream method has zero connection to the failed cases for multi-agent executions, concurrency for tests may need to be optimized). Edit: Looks like #2044 is seeing the same test failure. Certainly unrelated to either change, as expected. |
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
The Anthropic provider's stream method tries to read
event.message.usagefrom the last iterated stream event to extract token usage metadata. However, if the stream terminates prematurely and the last stream event's.messageattribute has not yet been populated with usage, this line crashes with an AttributeError.Solution
Instead of checking
event.message.usagefor the last stream event, we now call Anthropic SDK'sstream.get_final_message()method, which returns a "message snapshot" accumulated from all received events rather than relying on the last event's state. This call is wrapped in a try/except/else block, so that if it fails (which is only possible when zero events were received) we log a warning instead of crashing.Possible Concerns
One may worry that merely logging a warning when
get_final_message()call fails could lead to undercounted token usage. However, this method only fails when the stream yields zero events - in which case, there is no usage data to report anyway. If one or more events were received, the Anthropic SDK guarantees that the snapshot contains usage data (initialized by the mandatorymessage_startevent), andget_final_message()will succeed.Related Issues
#1868
Documentation PR
N/A
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepare(added a few tests to cover premature termination + empty stream cases)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.