Skip to content

fix(anthropic): instrument beta stream managers (fixes #4388) - #4393

Open
Anai-Guo wants to merge 1 commit into
traceloop:mainfrom
Anai-Guo:fix/anthropic-beta-async-stream
Open

fix(anthropic): instrument beta stream managers (fixes #4388)#4393
Anai-Guo wants to merge 1 commit into
traceloop:mainfrom
Anai-Guo:fix/anthropic-beta-async-stream

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented Jul 31, 2026

Copy link
Copy Markdown

Summary

Fixes #4388.

client.beta.messages.stream(...) (both sync and async, on the regular and Bedrock SDKs) returns a BetaMessageStreamManager / BetaAsyncMessageStreamManager from anthropic.lib.streaming._beta_messages. The beta stream methods are already listed in WRAPPED_METHODS, but is_stream_manager only recognized the non-beta MessageStreamManager / AsyncMessageStreamManager, so the wrapped beta calls fell through the is_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 sync WrappedMessageStreamManager.

Changes

  • is_stream_manager now also imports and isinstance-checks BetaMessageStreamManager / BetaAsyncMessageStreamManager (with the same class-name fallback extended for older SDKs that lack the beta module).
  • Added is_async_stream_manager() which matches both AsyncMessageStreamManager and BetaAsyncMessageStreamManager, and used it at both stream-manager routing sites so beta async streams route to WrappedAsyncMessageStreamManager.
  • Added 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 by is_stream_manager, and is_async_stream_manager returns True only for the two async variants. ruff check (E/F/W, line-length 120) passes.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved Anthropic streaming detection across standard and beta synchronous and asynchronous variants.
    • Ensured asynchronous streams are handled correctly, including environments where beta types are unavailable.
  • Tests
    • Added coverage for standard and beta stream managers.
    • Added validation that unsupported objects are rejected.

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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


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.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 368a9313-f33b-4e53-b1eb-475bde055a50

📥 Commits

Reviewing files that changed from the base of the PR and between 93429cf and 62af8a7.

📒 Files selected for processing (2)
  • packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py
  • packages/opentelemetry-instrumentation-anthropic/tests/test_stream_manager_detection.py

📝 Walkthrough

Walkthrough

The 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.

Changes

Anthropic stream-manager detection

Layer / File(s) Summary
Stream-manager type detection
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py
Detection now includes beta message and asynchronous stream managers, with class-name fallback checks.
Response wrapping and validation
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py, packages/opentelemetry-instrumentation-anthropic/tests/test_stream_manager_detection.py
Sync and async wrappers use is_async_stream_manager. Tests cover supported managers, plain objects, and beta asynchronous classification.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: dvirski

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the Anthropic beta stream manager instrumentation fix.
Linked Issues check ✅ Passed The changes satisfy [#4388] by detecting and instrumenting beta synchronous and asynchronous stream managers.
Out of Scope Changes check ✅ Passed The detection logic and tests directly support the linked issue and stated instrumentation objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: Anthropic instrumentation fails to instrument beta async streams

2 participants