fix(anthropic): instrument beta stream managers (fixes #4388) - #4393
fix(anthropic): instrument beta stream managers (fixes #4388)#4393Anai-Guo wants to merge 1 commit into
Conversation
client.beta.messages.stream(...) returns a Beta(Async)MessageStreamManager from anthropic.lib.streaming._beta_messages, but is_stream_manager only recognized the non-beta MessageStreamManager/AsyncMessageStreamManager, so beta streams were left uninstrumented. The async routing also compared the class name only against "AsyncMessageStreamManager", which would misroute a beta async stream to the sync wrapper. Recognize the beta manager classes in is_stream_manager and add is_async_stream_manager so both routing sites cover the beta async variant.
|
Anai-Guo seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe Anthropic instrumentation adds reusable detection for standard and beta stream managers. Sync and async response wrappers use the detection helper. Tests cover synchronous and asynchronous manager variants, including beta managers. ChangesAnthropic stream-manager detection
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Fixes #4388.
client.beta.messages.stream(...)(both sync and async, on the regular and Bedrock SDKs) returns aBetaMessageStreamManager/BetaAsyncMessageStreamManagerfromanthropic.lib.streaming._beta_messages. The betastreammethods are already listed inWRAPPED_METHODS, butis_stream_manageronly recognized the non-betaMessageStreamManager/AsyncMessageStreamManager, so the wrapped beta calls fell through theis_stream_manager(response)branch and were left uninstrumented.In addition, the async routing compared
response.__class__.__name__ == "AsyncMessageStreamManager", so even if a beta async manager reached that branch it would be misrouted to the syncWrappedMessageStreamManager.Changes
is_stream_managernow also imports andisinstance-checksBetaMessageStreamManager/BetaAsyncMessageStreamManager(with the same class-name fallback extended for older SDKs that lack the beta module).is_async_stream_manager()which matches bothAsyncMessageStreamManagerandBetaAsyncMessageStreamManager, and used it at both stream-manager routing sites so beta async streams route toWrappedAsyncMessageStreamManager.tests/test_stream_manager_detection.py: a cassette-free unit test asserting all four manager classes are detected and that only the async variants (including beta) are treated as async.Verification
Ran the detection logic against
anthropic==0.120.2: all four manager classes are now recognized byis_stream_manager, andis_async_stream_managerreturnsTrueonly for the two async variants.ruff check(E/F/W, line-length 120) passes.🤖 Generated with Claude Code
Summary by CodeRabbit