Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,32 @@
"method": "stream",
"span_name": "anthropic.chat",
},
# # Beta API methods (regular Anthropic SDK)
# {
# "package": "anthropic.resources.beta.messages.messages",
# "object": "Messages",
# "method": "create",
# "span_name": "anthropic.chat",
# },
# {
# "package": "anthropic.resources.beta.messages.messages",
# "object": "Messages",
# "method": "stream",
# "span_name": "anthropic.chat",
# },
# # Beta API methods (Bedrock SDK)
# {
# "package": "anthropic.lib.bedrock._beta_messages",
# "object": "Messages",
# "method": "create",
# "span_name": "anthropic.chat",
# },
# {
# "package": "anthropic.lib.bedrock._beta_messages",
# "object": "Messages",
# "method": "stream",
# "span_name": "anthropic.chat",
# },
# Beta API methods (regular Anthropic SDK)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "Messages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "Messages",
"method": "stream",
"span_name": "anthropic.chat",
},
# Beta API methods (Bedrock SDK)
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "Messages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "Messages",
"method": "stream",
"span_name": "anthropic.chat",
},
]
Comment on lines +84 to 110
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add Beta AsyncMessages.stream to sync wrappers (async context manager semantics).

For non-beta, AsyncMessages.stream is wrapped via the sync wrapper because it returns an async context manager (used with "async with") and is not awaitable. The Beta API exposes the same semantics, so Beta AsyncMessages.stream should also be wrapped with the sync wrapper (WRAPPED_METHODS), mirroring the non-beta approach. This avoids awaiting a non-awaitable object in the async wrapper.

Apply this diff to include Beta AsyncMessages.stream entries in WRAPPED_METHODS:

@@
     # Beta API methods (regular Anthropic SDK)
     {
         "package": "anthropic.resources.beta.messages.messages",
         "object": "Messages",
         "method": "create",
         "span_name": "anthropic.chat",
     },
     {
         "package": "anthropic.resources.beta.messages.messages",
         "object": "Messages",
         "method": "stream",
         "span_name": "anthropic.chat",
     },
+    # Beta API async stream is an async context manager; wrap with sync wrapper (see non-beta)
+    {
+        "package": "anthropic.resources.beta.messages.messages",
+        "object": "AsyncMessages",
+        "method": "stream",
+        "span_name": "anthropic.chat",
+    },
     # Beta API methods (Bedrock SDK)
     {
         "package": "anthropic.lib.bedrock._beta_messages",
         "object": "Messages",
         "method": "create",
         "span_name": "anthropic.chat",
     },
     {
         "package": "anthropic.lib.bedrock._beta_messages",
         "object": "Messages",
         "method": "stream",
         "span_name": "anthropic.chat",
     },
+    {
+        "package": "anthropic.lib.bedrock._beta_messages",
+        "object": "AsyncMessages",
+        "method": "stream",
+        "span_name": "anthropic.chat",
+    },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Beta API methods (regular Anthropic SDK)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "Messages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "Messages",
"method": "stream",
"span_name": "anthropic.chat",
},
# Beta API methods (Bedrock SDK)
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "Messages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "Messages",
"method": "stream",
"span_name": "anthropic.chat",
},
]
# Beta API methods (regular Anthropic SDK)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "Messages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "Messages",
"method": "stream",
"span_name": "anthropic.chat",
},
# Beta API async stream is an async context manager; wrap with sync wrapper (see non-beta)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessages",
"method": "stream",
"span_name": "anthropic.chat",
},
# Beta API methods (Bedrock SDK)
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "Messages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "Messages",
"method": "stream",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessages",
"method": "stream",
"span_name": "anthropic.chat",
},
]
🤖 Prompt for AI Agents
In
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py
around lines 84-110, the WRAPPED_METHODS list is missing Beta
AsyncMessages.stream entries so the Beta async context-manager stream is
currently not wrapped by the sync wrapper and may be awaited incorrectly; add
entries mirroring the non-beta async stream entries for the Beta API (both the
beta messages package and the bedrock _beta_messages package) pointing to
AsyncMessages.stream so the sync wrapper treats the returned async context
manager correctly.


WRAPPED_AMETHODS = [
Expand All @@ -122,32 +122,32 @@
"method": "create",
"span_name": "anthropic.chat",
},
# # Beta API async methods (regular Anthropic SDK)
# {
# "package": "anthropic.resources.beta.messages.messages",
# "object": "AsyncMessages",
# "method": "create",
# "span_name": "anthropic.chat",
# },
# {
# "package": "anthropic.resources.beta.messages.messages",
# "object": "AsyncMessages",
# "method": "stream",
# "span_name": "anthropic.chat",
# },
# # Beta API async methods (Bedrock SDK)
# {
# "package": "anthropic.lib.bedrock._beta_messages",
# "object": "AsyncMessages",
# "method": "create",
# "span_name": "anthropic.chat",
# },
# {
# "package": "anthropic.lib.bedrock._beta_messages",
# "object": "AsyncMessages",
# "method": "stream",
# "span_name": "anthropic.chat",
# },
# Beta API async methods (regular Anthropic SDK)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessages",
"method": "stream",
"span_name": "anthropic.chat",
},
# Beta API async methods (Bedrock SDK)
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessages",
"method": "stream",
"span_name": "anthropic.chat",
},
]
Comment on lines +125 to 151
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Bug: Wrapping Beta AsyncMessages.stream in async wrapper will await a non-awaitable context manager.

AsyncMessages.stream returns an async context manager (used with "async with"), not an awaitable. Wrapping it in WRAPPED_AMETHODS leads to awaiting a non-awaitable object in _awrap, which will raise a TypeError at runtime. Mirror the non-beta handling: remove these from WRAPPED_AMETHODS and wrap them via the sync wrapper (see prior comment).

Apply this diff to remove Beta AsyncMessages.stream entries from WRAPPED_AMETHODS:

@@
     # Beta API async methods (regular Anthropic SDK)
     {
         "package": "anthropic.resources.beta.messages.messages",
         "object": "AsyncMessages",
         "method": "create",
         "span_name": "anthropic.chat",
     },
-    {
-        "package": "anthropic.resources.beta.messages.messages",
-        "object": "AsyncMessages",
-        "method": "stream",
-        "span_name": "anthropic.chat",
-    },
@@
     # Beta API async methods (Bedrock SDK)
     {
         "package": "anthropic.lib.bedrock._beta_messages",
         "object": "AsyncMessages",
         "method": "create",
         "span_name": "anthropic.chat",
     },
-    {
-        "package": "anthropic.lib.bedrock._beta_messages",
-        "object": "AsyncMessages",
-        "method": "stream",
-        "span_name": "anthropic.chat",
-    },

Run this quick check to confirm stream usage patterns in tests (expect "async with ...messages.stream(" occurrences):


🏁 Script executed:

#!/bin/bash
# Verify AsyncMessages.stream is used as an async context manager (not awaited)
rg -n -A2 -i $'async with .*\\.messages\\.stream\\(' tests || true
rg -n -A2 -i $'await .*\\.messages\\.stream\\(' tests || true

Length of output: 214


To verify stream usage across the repo, let's search everywhere (not just a missing tests/ folder):

This will confirm whether stream is ever awaited or only used in async with. If it’s never awaited, we should remove it from WRAPPED_AMETHODS as proposed.


🏁 Script executed:

#!/bin/bash
# Verify AsyncMessages.stream is used as an async context manager (not awaited)
rg -n -A2 -i 'async with .*messages\.stream\(' .
rg -n -A2 -i 'await .*messages\.stream\(' .

Length of output: 1124


Remove Beta AsyncMessages.stream from async wrapper
Tests confirm that AsyncMessages.stream is only ever used as an async context manager (async with … .stream(…)) and is never awaited. Wrapping it in the async‐await wrapper will attempt to await a non‐awaitable, causing a TypeError at runtime.

Please update WRAPPED_AMETHODS in
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py
to remove the two "method": "stream" entries under both the regular and Bedrock Beta sections, for example:

@@ -128,11 +128,6 @@ WRAPPED_AMETHODS = [
     # Beta API async methods (regular Anthropic SDK)
     {
         "package": "anthropic.resources.beta.messages.messages",
         "object": "AsyncMessages",
         "method": "create",
         "span_name": "anthropic.chat",
     },
-    {
-        "package": "anthropic.resources.beta.messages.messages",
-        "object": "AsyncMessages",
-        "method": "stream",
-        "span_name": "anthropic.chat",
-    },
@@ -134,11 +129,6 @@ WRAPPED_AMETHODS = [
     # Beta API async methods (Bedrock SDK)
     {
         "package": "anthropic.lib.bedrock._beta_messages",
         "object": "AsyncMessages",
         "method": "create",
         "span_name": "anthropic.chat",
     },
-    {
-        "package": "anthropic.lib.bedrock._beta_messages",
-        "object": "AsyncMessages",
-        "method": "stream",
-        "span_name": "anthropic.chat",
-    },
 ]

After removal, if you need to instrument stream, mirror the non-Beta handling by wrapping it via the sync wrapper.

Tagging as since this change is necessary to prevent runtime errors.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Beta API async methods (regular Anthropic SDK)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessages",
"method": "stream",
"span_name": "anthropic.chat",
},
# Beta API async methods (Bedrock SDK)
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessages",
"method": "create",
"span_name": "anthropic.chat",
},
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessages",
"method": "stream",
"span_name": "anthropic.chat",
},
]
# Beta API async methods (regular Anthropic SDK)
{
"package": "anthropic.resources.beta.messages.messages",
"object": "AsyncMessages",
"method": "create",
"span_name": "anthropic.chat",
},
# Beta API async methods (Bedrock SDK)
{
"package": "anthropic.lib.bedrock._beta_messages",
"object": "AsyncMessages",
"method": "create",
"span_name": "anthropic.chat",
},
]
🤖 Prompt for AI Agents
In
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py
around lines 125 to 151, the Beta async mapping includes two entries with
"method": "stream" which are only used as async context managers and not
awaitable; remove the two {"object": "AsyncMessages", "method": "stream",
"span_name": "anthropic.chat"} entries (one in the regular Beta section and one
in the Bedrock Beta section) so the async wrapper will not attempt to await a
non-awaitable, and if you need to instrument stream leave or add it under the
non-Beta/sync wrapper handling instead.



Expand Down
Loading
Loading