fix(anthropic): Only finish relevant spans in .create() patches by alexander-alderman-webb · Pull Request #5716 · getsentry/sentry-python · GitHub
Skip to content

fix(anthropic): Only finish relevant spans in .create() patches - #5716

Merged
alexander-alderman-webb merged 9 commits into
masterfrom
webb/anthropic/remove-get-current-span
Apr 9, 2026
Merged

alexander-alderman-webb merged 9 commits into
masterfrom
webb/anthropic/remove-get-current-span

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Description

Anthropic spans are only created in _sentry_patched_create() and _sentry_patched_create_async().

Exit spans in these functions when the anthropic library function raises an exception.

Resolves an edge case where _sentry_patched_create() or _sentry_patched_create_async() exits early and does not create a span and get_current_span() therefore returns a non-anthropic span.

Issues

Reminders

@github-actions

github-actions Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 8.36s

All tests are passing successfully.

❌ Patch coverage is 20.00%. Project has 14846 uncovered lines.

Files with missing lines (1)
File Patch % Lines
anthropic.py 5.81% ⚠️ 405 Missing

Generated by Codecov Action

@alexander-alderman-webb
alexander-alderman-webb marked this pull request as ready for review April 2, 2026 08:55
@alexander-alderman-webb
alexander-alderman-webb requested a review from a team as a code owner April 2, 2026 08:55
Comment thread sentry_sdk/integrations/anthropic.py

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment thread sentry_sdk/integrations/anthropic.py

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

Is there any test coverage that we can add here to confirm this behaviour? Otherwise LGTM

@alexander-alderman-webb

Copy link
Copy Markdown
Contributor Author

Is there any test coverage that we can add here to confirm this behaviour? Otherwise LGTM

In my view a regression test has limited use in this case because an integration should not be touching spans created outside the integration (unless it's for an agreed-upon reason, i.e., not duplicating spans between openai-agents and openai).

For the future, here's a script that verifies that demonstrates the edge case I'm addressing below. The messages argument is missing from the call to create() and the outer transaction is closed prematurely since it's status is INTERNAL_ERROR.

import sentry_sdk
from sentry_sdk.tracing import SPANSTATUS
from sentry_sdk.integrations.anthropic import AnthropicIntegration
from anthropic import Anthropic

sentry_sdk.init(
    dsn="",
    integrations=[AnthropicIntegration()],
    traces_sample_rate=1.0,
)

def main():
    client = Anthropic(api_key="dummy")
    with sentry_sdk.start_transaction(name="parent", op="test") as transaction:
        transaction.set_status(SPANSTATUS.INTERNAL_ERROR)
        try:
            client.messages.create(max_tokens=1024, model="model", stream=False)
        except Exception as e:
            print(f"Expected exception: {e}")

if __name__ == "__main__":
    main()

@ericapisani

Copy link
Copy Markdown
Member

Base automatically changed from webb/anthropic/separate-sync-and-async to master April 9, 2026 07:32
@alexander-alderman-webb
alexander-alderman-webb merged commit 45abbe3 into master Apr 9, 2026
158 checks passed
@alexander-alderman-webb
alexander-alderman-webb deleted the webb/anthropic/remove-get-current-span branch April 9, 2026 07:46
mgaligniana pushed a commit to mgaligniana/sentry-python that referenced this pull request Aug 9, 2026
…entry#5716)

Anthropic spans are only created in `_sentry_patched_create()` and `_sentry_patched_create_async()`.

Exit spans in these functions when the anthropic library function raises an exception.

Resolves an edge case where `_sentry_patched_create()` or `_sentry_patched_create_async()` exits early without creating a span, causing `get_current_span()` to return a non-anthropic span.
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.

2 participants