fix: handle premature stream termination for Anthropic (#1868)#2047
fix: handle premature stream termination for Anthropic (#1868)#2047gautamsirdeshmukh 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! |
|
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. |
|
/strands review |
|
/strands are there any issues with this change? |
| yield event | ||
|
|
||
| mock_stream.__aiter__ = mock_aiter | ||
| mock_stream.get_final_message.return_value = unittest.mock.Mock( |
There was a problem hiding this comment.
Can we build this into the agenerator? That way we dont need to redefine this feature for multiple tests in this file?
There was a problem hiding this comment.
+1 on this. The mock stream setup pattern (creating an AsyncMock with __aiter__ and get_final_message) is repeated 3 times across test_stream, test_stream_early_termination, and test_stream_empty. A shared fixture or helper would reduce duplication and make it easier to add more streaming tests. Additionally, test_structured_output (line 892) needs the same update — it still uses the old agenerator pattern which doesn't have get_final_message().
There was a problem hiding this comment.
agenerator is just a plain async generator, it yields items but that's about it. We need to create a custom generator in order to attach/mock the get_final_message method as well. I do agree that we should have a helper method or something of that nature to reduce duplication
|
/strands review |
src/strands/models/anthropic.py
Outdated
| yield self.format_chunk({"type": "metadata", "usage": usage.model_dump()}) | ||
| try: | ||
| message_snapshot = await stream.get_final_message() | ||
| except Exception as e: |
There was a problem hiding this comment.
Can we make this error catch more targeted? Like AssertionError or something?
There was a problem hiding this comment.
+1. The Anthropic SDK's get_final_message() specifically does:
assert self.__final_message_snapshot is not NoneSo AssertionError is the right type to catch here. Catching broad Exception masks real issues — for example, test_structured_output currently passes despite using a mock without get_final_message() only because the AttributeError is silently swallowed by except Exception.
There was a problem hiding this comment.
Yes, scoping this catch to AttributeError, we should surface unexpected exceptions
|
Assessment: Request Changes Good fix for a real crash scenario. The approach of using Review Details
The core fix is solid and the thorough scenario testing in the PR description is appreciated. |
|
/strands review |
|
Assessment: Approve All previous review feedback has been addressed cleanly. The exception catch is now scoped to Review Summary
Solid fix with thorough testing. |
|
/strands review |
|
Assessment: Approve All prior review feedback has been addressed. No new issues found. Review Details
|
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.