fix: delegate to MessageStream in _MessageStreamManager.__enter__ #2468
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.
fixes #2467
Problem
The
_MessageStreamManager.__enter__()wrapper was calling__api_request()which returns a low-levelanthropic.Streamobject instead of the high-levelanthropic.lib.streaming.MessageStreamobject.This caused
AttributeError: 'Stream' object has no attribute 'get_final_message'when users tried to call helper methods likeget_final_message(),get_final_text(), oruntil_done()on instrumented streams.Solution
Changed
__enter__()to delegate to the wrappedMessageStreamManager.__enter__()method, which returns the properMessageStreamobject with all helper methods intact.Before:
After:
Impact
This fix allows users to:
stream.get_final_message()after consuming the streamget_final_text()anduntil_done()Testing
The existing streaming tests in
test_instrumentor.pyverify that basic streaming functionality works. This fix maintains backward compatibility while adding support for the missing helper methods.Note
Return the real
MessageStreamfromMessageStreamManager.__enter__and pass through__exit__, enabling helper methods and proper cleanup; add tests and cassette to verify._MessageStreamManager.__enter__()to call wrappedMessageStreamManager.__enter__()and return_MessagesStreamwrapping the realMessageStream._MessagesStream.__exit__()to delegate to the underlying manager's__exit__for proper cleanup.MessageStreamtype.test_anthropic_instrumentation_stream_messageto assertstream.get_final_message()works.test_anthropic_instrumentation_stream_context_manager_exitto verify manager__exit__is called andget_final_message()works.tests/.../test_anthropic_instrumentation_stream_context_manager_exit.yaml.Written by Cursor Bugbot for commit 397c19c. This will update automatically on new commits. Configure here.