Skip to content

[WIP] feat: add semantic convention support for anthropic sdk#175

Open
123liuziming wants to merge 2 commits into
mainfrom
feat/anthropic
Open

[WIP] feat: add semantic convention support for anthropic sdk#175
123liuziming wants to merge 2 commits into
mainfrom
feat/anthropic

Conversation

@123liuziming

Copy link
Copy Markdown
Collaborator

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

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

  • Test A

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@CLAassistant

CLAassistant commented May 4, 2026

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 all sign our Contributor License Agreement before we can accept your contribution.
0 out of 2 committers have signed the CLA.

❌ musi
❌ Copilot


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

)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: 123liuziming <32130965+123liuziming@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label May 19, 2026

@ralf0131 ralf0131 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review by github-manager-bot

Summary

Adds a new instrumentation plugin for the Anthropic Python SDK (messages.create / AsyncMessages.create, including SSE streaming) following the OTel contrib pattern. Includes LICENSE, examples, and cassette-based tests covering basic/error/stop-reason/token-usage paths. Nice, clean structure overall.

Note: I see the PR is marked [WIP], so I've focused on design/architecture feedback rather than nitpicks — the findings below should hold even if individual lines change.

Findings

  • [Warning] patch.pyAnthropicStreamWrapper and AsyncAnthropicStreamWrapper duplicate ~120 lines (_process_event, _cleanup, _ContentBlockAccumulator usage, and all state fields) almost verbatim. This is a real maintenance hazard: when a token-usage or content-block edge case is fixed in one, the other is easy to forget. Consider extracting a shared _StreamTelemetryMixin/base that both sync and async wrappers inherit, with only the __next__/__anext__ plumbing differing.
  • [Info] Streaming span lifecycle: finalization (_cleanupstop_llm/fail_llm) only runs via __next__ hitting StopIteration, __exit__, or explicit close(). If a caller partially consumes a stream and discards the iterator without using it as a context manager / calling close(), the LLM span is never ended (leak). There's no __del__ safety net. Worth at least documenting that streaming must be consumed in a with block, or adding a best-effort __del__.
  • [Info] build_output_messages uses finish_reason=stop_reason or "error". If a stream completes normally but stop_reason wasn't captured by the time _cleanup runs, a successful response is labelled "error" — a false-positive error finish reason that would skew metrics. Consider distinguishing "absent" from "error".
  • [Info] License header in patch.py is the full Apache header (good), but it's inconsistent with the sibling pydantic-ai / deepagents plugins which truncate to two lines. Aligning helps if a license-header lint is ever enabled.

Suggestions

Something like:

class _StreamTelemetryMixin:
    def _process_event(self, event): ...   # shared
    def _cleanup(self, error=None): ...    # shared

class AnthropicStreamWrapper(_StreamTelemetryMixin): ...
class AsyncAnthropicStreamWrapper(_StreamTelemetryMixin): ...

Cross-repo Note

No protocol/API shared with loongsuite-pilot; no cross-repo change needed.


Automated review by github-manager-bot

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants