fix(pydantic-ai): Use first-class hooks when available #5947
2 issues
code-review: Found 2 issues (1 medium, 1 low)
Medium
are_request_hooks_available incorrectly set to True when Hooks import fails - `sentry_sdk/integrations/pydantic_ai/__init__.py:66`
On line 66, when the Hooks import fails (meaning hooks are NOT available), the code sets are_request_hooks_available = True. This is logically incorrect - it should be False since hooks are unavailable. While the class attribute defaults to True (line 32), the exception handler should flip it to False. This causes unnecessary metadata initialization ({"_sentry_span": None}) in agent_run.py even when hooks aren't being used, which is wasteful and potentially confusing for debugging.
Low
Falsy check on metadata may overwrite user's empty dict - `sentry_sdk/integrations/pydantic_ai/patches/agent_run.py:113-114`
The condition if not metadata: treats both None and empty dict {} the same way. If a user explicitly passes metadata={}, the code overwrites it with {"_sentry_span": None} instead of preserving their (empty) dict. While this edge case is unlikely in practice, using if metadata is None: would be more precise and avoid potential confusion or data loss when users intend to pass an empty metadata dict that they plan to populate later.
Duration: 6m 7s · Tokens: 899.8k in / 15.2k out · Cost: $1.50 (+extraction: $0.00, +merge: $0.00, +fix_gate: $0.00)
Annotations
Check warning on line 66 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: code-review
are_request_hooks_available incorrectly set to True when Hooks import fails
On line 66, when the `Hooks` import fails (meaning hooks are NOT available), the code sets `are_request_hooks_available = True`. This is logically incorrect - it should be `False` since hooks are unavailable. While the class attribute defaults to `True` (line 32), the exception handler should flip it to `False`. This causes unnecessary metadata initialization (`{"_sentry_span": None}`) in `agent_run.py` even when hooks aren't being used, which is wasteful and potentially confusing for debugging.