Skip to content

[opentelemetry-instrumentation-genai-anthropic] Instrument beta.messages create, stream, and parse (#550) - #781

Open
benkawecki wants to merge 10 commits into
open-telemetry:mainfrom
benkawecki:anthropic-beta-messages-create
Open

benkawecki wants to merge 10 commits into
open-telemetry:mainfrom
benkawecki:anthropic-beta-messages-create

Conversation

@benkawecki

@benkawecki benkawecki commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes 550

This work builds from PR 661

Instruments anthropic.resources.beta.messages.Messages and AsyncMessages (create, stream, and parse), including raw and streaming response helpers.

Anthropic beta messages provide access to new API features (such as prompt caching, computer use, and MCP tools) while producing standard chat inference spans and metrics.

Type of change

  • New feature (non-breaking change which adds functionality)

How has this been tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. List any relevant details for your test
configuration.

  • uv run tox -e py312-test-instrumentation-genai-anthropic-conformance
  • uv run tox -e py312-test-instrumentation-genai-anthropic-latest
  • uv run tox -e py312-test-instrumentation-genai-anthropic-oldest

Checklist

See CONTRIBUTING.md
for the style guide, changelog guidance, and more.

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests added
  • Documentation updated

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-10-17 01:44 UTC

Respond to 1 review item (e.g. link a commit, explain why not, ask a follow-up):

  • Top-level threads: 1
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Redact exposed workspace identifiers and correct the beta raw-response coverage and duplicate conformance registration.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 5 Medium severity · 1 Low severity

Open (6)
What changed in this PR

Adds instrumentation for Anthropic beta Messages APIs, including synchronous/asynchronous create, stream, parse, raw responses, and content extraction.

Changes:

  • Adds beta-aware wrappers, parsing, streaming, and telemetry extraction.
  • Adds unit, conformance, and cassette-backed tests.
  • Updates documentation and changelog.
File Summary
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​test_sync_messages.py Tests synchronous beta messages.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​test_parse_messages.py Tests beta parse behavior.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​test_messages_extractors.py Tests beta content extraction.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​test_instrumentor.py Tests instrumentation lifecycle.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​test_conformance.py Registers conformance scenarios.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​test_async_wrappers.py Tests asynchronous stream handling.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​test_async_messages.py Tests asynchronous beta messages.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​conformance/​tool_calling_beta.py Adds beta tool-calling coverage.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​conformance/​inference_streaming_beta.py Adds beta streaming coverage.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​conformance/​inference_raw_response_beta.py Adds beta raw-response coverage.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​conformance/​inference_beta.py Adds beta inference coverage.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​conformance/​inference_beta_server_tool_calling.py Adds beta server tool-calling coverage.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​tool_calling_beta_conformance.yaml Records beta tool-calling interactions.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_messages_beta_create_with_raw_response.yaml Records synchronous beta raw responses.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_messages_beta_create_streaming.yaml Records synchronous beta streaming.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_messages_beta_create_streaming_with_raw_response.yaml Records synchronous beta raw streaming.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_beta_messages_with_raw_response.yaml Records synchronous beta raw responses.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_beta_messages_stream.yaml Records synchronous beta streams.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_beta_messages_parse_basic.yaml Records synchronous beta parsing.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_beta_messages_create_with_content.yaml Records beta content creation.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_beta_messages_create_streaming.yaml Records synchronous beta streaming.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_beta_messages_create_basic.yaml Records basic synchronous beta creation.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_sync_beta_messages_create_api_error.yaml Records synchronous beta API errors.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_async_beta_messages_with_raw_response.yaml Records asynchronous beta raw responses.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_async_beta_messages_stream.yaml Records asynchronous beta streams.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_async_beta_messages_parse_basic.yaml Records asynchronous beta parsing.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_async_beta_messages_create_with_content.yaml Records asynchronous beta content creation.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_async_beta_messages_create_streaming.yaml Records asynchronous beta streaming.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_async_beta_messages_create_basic.yaml Records basic asynchronous beta creation.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​test_async_beta_messages_create_api_error.yaml Records asynchronous beta API errors.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​inference_beta_streaming_conformance.yaml Records beta streaming conformance.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​inference_beta_server_tool_calling_conformance.yaml Records beta server tool-calling conformance.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​tests/​cassettes/​inference_beta_conformance.yaml Records beta inference conformance.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​src/​opentelemetry/​instrumentation/​genai/​anthropic/​wrappers.py Handles beta stream accumulation.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​src/​opentelemetry/​instrumentation/​genai/​anthropic/​utils.py Converts beta content blocks.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​src/​opentelemetry/​instrumentation/​genai/​anthropic/​patch.py Instruments beta calls and streams.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​src/​opentelemetry/​instrumentation/​genai/​anthropic/​messages_extractors.py Extracts beta request and response data.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​src/​opentelemetry/​instrumentation/​genai/​anthropic/​_raw_response.py Supports beta raw responses.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​src/​opentelemetry/​instrumentation/​genai/​anthropic/​__init__.py Registers beta instrumentation.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​README.rst Documents beta API support.
instrumentation/​opentelemetry-instrumentation-genai-anthropic/​.changelog/​661.added Adds the feature changelog entry.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@benkawecki

Copy link
Copy Markdown
Contributor Author

A couple notes / questions for maintainers.

  1. I took over @lmolkova PR 661 code for this. There were some hard conflicts between that branch and main. I ended up squashing the existing commits and applying those on top of main. This does however remove the commit messages and contribute history from @lmolkova which I don't want to do unless thats okay with others. My intent wasn't to claim that work, but rather the to avoid a really nasty rebase. If people think preserving that is more important I can go back, do the fully rebase and then cherry pick my work ontop.
  2. I found a failing test when running uv run tox -e py314-test-instrumentation-genai-anthropic-oldest. It appears this fails on the current main branch as well. Please let me know if I should open an issue or attempt to resolve the issue in this PR.
  3. The current anthropic model for the conformance tests (claude-sonnet-4-20250514) is deprecated and no longer works. I updated to sonnet 4.6 for my tests. I can updating the existing tests and re-run the VCR capture in this PR if people think thats prudent.

@benkawecki
benkawecki force-pushed the anthropic-beta-messages-create branch from 1693211 to a9c1ce3 Compare September 23, 2026 20:22
@aabmass

aabmass commented Sep 23, 2026

Copy link
Copy Markdown
Member
  1. This does however remove the commit messages and contribute history from @lmolkova which I don't want to do unless thats okay with others. My intent wasn't to claim that work, but rather the to avoid a really nasty rebase.

She's OOO but I don't think it's a problem. Can you add her email as a coauthor on https://docs.github.com/en/pull-requests/how-tos/commit-changes/creating-a-commit-with-multiple-authors?

2. It appears this fails on the current main branch as well. Please let me know if I should open an issue or attempt to resolve the issue in this PR.

If it's a consistent failure, sending a separate PR would be awesome :)

3. I can updating the existing tests and re-run the VCR capture in this PR if people think thats prudent.

My 2c, that would be great but consider doing in a separate PR. Thanks!

benkawecki and others added 4 commits September 24, 2026 14:03
…sages create, stream, and parse

Adds the beta.messages instrumentation (create/stream/parse spans,
raw-response support, server-tool-calling and MCP content classification,
conformance scenarios)

Co-authored-by: Liudmila Molkova <neskazu@gmail.com>
Assisted-by: Claude Sonnet 5
…se and tool-calling conformance scenarios

Adds InferenceBetaRawResponseScenario, InferenceBetaRawResponseStreamingScenario,
and ToolCallingBetaScenario, closing the beta.messages coverage gap against the
existing non-beta conformance scenarios. InferenceBetaStreamingScenario moves out
of inference_beta.py into its own inference_streaming_beta.py, matching the
inference.py/inference_streaming.py split already used for the non-beta scenarios
@benkawecki
benkawecki force-pushed the anthropic-beta-messages-create branch from d7f4340 to db81210 Compare September 24, 2026 18:05
@eternalcuriouslearner

Copy link
Copy Markdown
Contributor

@benkawecki Sorry for the delay, looking into this.

@opentelemetry-pr-dashboard

This comment has been minimized.

@benkawecki

Copy link
Copy Markdown
Contributor Author

@benkawecki Sorry for the delay, looking into this.

Thanks! lmk when you get a chance to take a look.

@dskoda1

dskoda1 commented Oct 2, 2026

Copy link
Copy Markdown

looking forward to this one 🙏

@lmolkova lmolkova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@lmolkova

Copy link
Copy Markdown
Member

@benkawecki could you please resolve conflicts and it should be good to go

@benkawecki

Copy link
Copy Markdown
Contributor Author

@lmolkova conflicts resolved.

@lmolkova

Copy link
Copy Markdown
Member

@benkawecki sorry, some conflicts again 😞

@lmolkova

Copy link
Copy Markdown
Member

@benkawecki could you please fix tests? there were some changes on main wrt capturing lists and some methods were removed. see #873 for the details

args: tuple[Any, ...],
kwargs: dict[str, Any],
) -> MessagesStreamManagerWrapper[Any]:
_materialize_tools(kwargs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why? are these not applicable here? can't input / output messages be iterators / generators here that we consume prematurely?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I missed this when I did my merge. Removed it as it loos like #873 moved to a different pattern. Let me know if my interpretation of that was not correct.

@benkawecki

Copy link
Copy Markdown
Contributor Author

@lmolkova fixed CI issues.

@eternalcuriouslearner

Copy link
Copy Markdown
Contributor

@benkawecki are we capturing compaction span attribute here?

@benkawecki

Copy link
Copy Markdown
Contributor Author

@benkawecki are we capturing compaction span attribute here?

I don't think so. I see you have a PR to add compaction attributes. Maybe we should add compaction as a separate follow-on PR to this one?

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[anthropic] Add beta.messages.create span

6 participants