fix: delegate to MessageStream in _MessageStreamManager.__enter__#2468
Open
youest wants to merge 3 commits intoArize-ai:mainfrom
Open
fix: delegate to MessageStream in _MessageStreamManager.__enter__#2468youest wants to merge 3 commits intoArize-ai:mainfrom
youest wants to merge 3 commits intoArize-ai:mainfrom
Conversation
Resolves Arize-ai#2467 The wrapper was calling __api_request() which returns a low-level Stream object instead of the high-level MessageStream object with helper methods like get_final_message(). This fix delegates to the wrapped MessageStreamManager's __enter__ method to get the proper MessageStream object.
Contributor
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
Add test case to verify that get_final_message() can be called on instrumented MessageStream objects after consuming the stream. This validates the fix for issue Arize-ai#2467.
...inference-instrumentation-anthropic/src/openinference/instrumentation/anthropic/_wrappers.py
Outdated
Show resolved
Hide resolved
Author
|
I have read the CLA Document and I hereby sign the CLA |
…anup Fixes context manager protocol violation where __exit__() was not called on the wrapped MessageStreamManager, causing stream.close() to never execute. - Added _manager parameter to _MessagesStream.__init__() - Implemented __exit__() that delegates to manager's __exit__() - Added test to verify __exit__() is properly called - All 22 tests pass 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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.
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.