Skip to content

set metadata in agent.__init__

396f322
Select commit
Loading
Failed to load commit list.
Merged

fix(pydantic-ai): Use first-class hooks when available #5947

set metadata in agent.__init__
396f322
Select commit
Loading
Failed to load commit list.
@sentry/warden / warden: code-review completed Apr 9, 2026 in 5m 19s

1 issue

code-review: Found 1 issue (1 medium)

Medium

Potential race condition when storing span in shared metadata - `sentry_sdk/integrations/pydantic_ai/__init__.py:51-52`

The _sentry_span is stored in ctx.metadata which, according to the docstring (line 126-127), is initialized from Agent.metadata as a shared reference. If multiple concurrent run() calls share the same metadata dictionary, the _sentry_span key could be overwritten by concurrent requests, causing spans to be mismatched between on_request and on_response/on_error handlers. This could result in spans being closed with the wrong response data or orphaned spans.


Duration: 5m 19s · Tokens: 1.3M in / 14.8k out · Cost: $1.71

Annotations

Check warning on line 52 in sentry_sdk/integrations/pydantic_ai/__init__.py

See this annotation in the file changed.

@sentry-warden sentry-warden / warden: code-review

Potential race condition when storing span in shared metadata

The `_sentry_span` is stored in `ctx.metadata` which, according to the docstring (line 126-127), is initialized from `Agent.metadata` as a shared reference. If multiple concurrent `run()` calls share the same metadata dictionary, the `_sentry_span` key could be overwritten by concurrent requests, causing spans to be mismatched between `on_request` and `on_response`/`on_error` handlers. This could result in spans being closed with the wrong response data or orphaned spans.