Skip to content

🐛 Bug Report: AnthropicInstrumentor is not supporting get_final_message #3371

@MiloParigi

Description

@MiloParigi

Which component is this bug for?

Anthropic Instrumentation

📜 Description

The current implementation (0.46.2) of opentelemetry-instrumentation-anthropic does not support the get_final_message method from the Anthropic SDK

When trying to use the get_final_message property of an async_generator object, the property is not present in the async_generator object: AttributeError: 'async_generator' object has no attribute 'get_final_message'

👟 Reproduction steps

In a new, clean environment (I'm using Python 3.13.5 personally)

requirements.txt

anthropic==0.66.0
opentelemetry-instrumentation-anthropic==0.46.2

main.py

import asyncio
from anthropic import AsyncAnthropic
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor

# AnthropicInstrumentor().instrument() # Uncommenting this will make the call fail

async def main() -> None:
    """
    This comes directly from the Github README of the Anthropic SDK
    
    https://github.com/anthropics/anthropic-sdk-python?tab=readme-ov-file#streaming-helpers
    """
    client = AsyncAnthropic(api_key="sk-XXX")

    async with client.messages.stream(
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": "Say hello there!",
            }
        ],
        model="claude-sonnet-4-20250514",
    ) as stream:
        message = await stream.get_final_message()
        print(message.to_json())

asyncio.run(main())

Then launch main.py, once with the telemetry commented out (it works), once without (it fails)

👍 Expected behavior

The call should not fail and traces should be sent

👎 Actual Behavior with Screenshots

It fails with an error

Image

🤖 Python Version

3.13.5

📃 Provide any additional context for the Bug.

I think it might not be just the get_final_message, but some/all stream helpers, because the same issue arise when using the example from the SDK with text_stream: Anthropic SDK - Streaming Helpers

Looking at the SDK docs on helpers, we might want to add some unit-tests for the following methods:

  • async_generator.close()
  • async_generator.until_done()
  • async_generator.get_final_message()
  • async_generator.get_final_text()
  • async_generator.text_stream()

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

Are you willing to submit PR?

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions