fix(pydantic-ai): Use first-class hooks when available #5947
2 issues
find-bugs: Found 2 issues (2 medium)
Medium
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
Duration: 8m 54s · Tokens: 1.8M in / 22.7k out · Cost: $2.38 (+extraction: $0.01, +merge: $0.00, +fix_gate: $0.01)
Annotations
Check warning on line 72 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: find-bugs
Span may leak if update_ai_client_span or span.__exit__ raises in on_response
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.
Check warning on line 51 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: find-bugs
[TLC-PT8] Span may leak if update_ai_client_span or span.__exit__ raises in on_response (additional location)
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.
Check warning on line 723 in tests/integrations/pydantic_ai/test_pydantic_ai.py
sentry-warden / warden: find-bugs
Agent created before sentry_init() bypasses hook-based instrumentation
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.
Check warning on line 1201 in tests/integrations/pydantic_ai/test_pydantic_ai.py
sentry-warden / warden: find-bugs
[3XL-5Y3] Agent created before sentry_init() bypasses hook-based instrumentation (additional location)
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.