Skip to content

docstring

622a48f
Select commit
Loading
Failed to load commit list.
Merged

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

docstring
622a48f
Select commit
Loading
Failed to load commit list.
@sentry/warden / warden completed Apr 9, 2026 in 9m 19s

3 issues

Medium

Missing test coverage for hooks-based instrumentation path - `sentry_sdk/integrations/pydantic_ai/__init__.py:31-111`

The new register_hooks function and the hooks-based code path (lines 31-111) introduce significant new functionality for instrumenting pydantic-ai using first-class hooks, but there are no corresponding tests in the test file that verify this functionality when pydantic_ai.capabilities.Hooks is available. The test file doesn't contain any tests that assert the hooks are registered or verify behavior when the newer library version is used.

Span may leak if update_ai_client_span or span.__exit__ raises in on_response - `sentry_sdk/integrations/pydantic_ai/__init__.py:71-72`

In the on_response hook (lines 57-74), if update_ai_client_span() or span.__exit__() raises an exception, the span may not be properly closed. The on_error hook correctly wraps span.__exit__ in capture_internal_exceptions(), but on_response does not have similar protection. This inconsistency could lead to span leaks or scope corruption if an internal error occurs during span finalization.

Also found at:

  • sentry_sdk/integrations/pydantic_ai/__init__.py:51
Agent created before sentry_init() bypasses hook-based instrumentation - `tests/integrations/pydantic_ai/test_pydantic_ai.py:712-723`

In test_without_pii_tools, get_test_agent() is called at line 712 before sentry_init() at line 719. The PydanticAIIntegration.setup_once() patches Agent.__init__ to inject Sentry hooks (see register_hooks at line 111 of the integration). Since the agent is created before the SDK is initialized, the patched __init__ isn't used, and the agent lacks Sentry instrumentation. This means the test may pass vacuously (no chat spans created to check) rather than validating the actual behavior.

Also found at:

  • tests/integrations/pydantic_ai/test_pydantic_ai.py:1190-1201
4 skills analyzed
Skill Findings Duration Cost
code-review 1 5m 48s $1.55
find-bugs 2 8m 54s $2.37
skill-scanner 0 3m 16s $0.47
security-review 0 3m 2s $0.66

Duration: 21m · Tokens: 3.9M in / 50.3k out · Cost: $5.07 (+extraction: $0.01, +dedup: $0.02, +merge: $0.00, +fix_gate: $0.01)