fix(pydantic-ai): Use first-class hooks when available #5947
2 issues
find-bugs: Found 2 issues (2 medium)
Medium
User metadata lost when passing empty dict to Agent.run() - `sentry_sdk/integrations/pydantic_ai/__init__.py:40-42`
The condition if not metadata: at line 115-116 (in agent_run.py) treats an empty dict as falsy, replacing metadata={} with {"_sentry_span": None}. If a user passes metadata={} expecting to receive hook modifications into that object, their reference is discarded. This is a change in the hunk at init.py that relies on agent_run.py metadata initialization - the hooks at lines 40-41 check isinstance(run_context_metadata, dict) which would now receive the Sentry-created dict instead of the user's original empty dict.
Test creates agent before sentry_init, potentially masking hooks instrumentation issues - `tests/integrations/pydantic_ai/test_pydantic_ai.py:316`
In test_agent_with_tools, the test agent is created at line 316 before sentry_init() is called at line 323. When the new hooks-based instrumentation is available (pydantic_ai.capabilities.Hooks), agents must be created AFTER sentry_init for the hooks to be registered via the patched Agent.__init__. Since the agent is created before sentry_init, it won't have Sentry hooks in its capabilities, meaning chat spans may not be created when hooks are available. This could cause the test to pass when hooks are unavailable (fallback patches) but fail or produce incomplete instrumentation when hooks are available.
Also found at:
tests/integrations/pydantic_ai/test_pydantic_ai.py:712
Duration: 8m 32s · Tokens: 2.0M in / 26.1k out · Cost: $3.04 (+extraction: $0.01, +merge: $0.00)
Annotations
Check warning on line 42 in sentry_sdk/integrations/pydantic_ai/__init__.py
sentry-warden / warden: find-bugs
User metadata lost when passing empty dict to Agent.run()
The condition `if not metadata:` at line 115-116 (in agent_run.py) treats an empty dict as falsy, replacing `metadata={}` with `{"_sentry_span": None}`. If a user passes `metadata={}` expecting to receive hook modifications into that object, their reference is discarded. This is a change in the hunk at __init__.py that relies on agent_run.py metadata initialization - the hooks at lines 40-41 check `isinstance(run_context_metadata, dict)` which would now receive the Sentry-created dict instead of the user's original empty dict.
Check warning on line 316 in tests/integrations/pydantic_ai/test_pydantic_ai.py
sentry-warden / warden: find-bugs
Test creates agent before sentry_init, potentially masking hooks instrumentation issues
In `test_agent_with_tools`, the test agent is created at line 316 before `sentry_init()` is called at line 323. When the new hooks-based instrumentation is available (pydantic_ai.capabilities.Hooks), agents must be created AFTER sentry_init for the hooks to be registered via the patched `Agent.__init__`. Since the agent is created before sentry_init, it won't have Sentry hooks in its capabilities, meaning chat spans may not be created when hooks are available. This could cause the test to pass when hooks are unavailable (fallback patches) but fail or produce incomplete instrumentation when hooks are available.
Check warning on line 712 in tests/integrations/pydantic_ai/test_pydantic_ai.py
sentry-warden / warden: find-bugs
[RZB-DBD] Test creates agent before sentry_init, potentially masking hooks instrumentation issues (additional location)
In `test_agent_with_tools`, the test agent is created at line 316 before `sentry_init()` is called at line 323. When the new hooks-based instrumentation is available (pydantic_ai.capabilities.Hooks), agents must be created AFTER sentry_init for the hooks to be registered via the patched `Agent.__init__`. Since the agent is created before sentry_init, it won't have Sentry hooks in its capabilities, meaning chat spans may not be created when hooks are available. This could cause the test to pass when hooks are unavailable (fallback patches) but fail or produce incomplete instrumentation when hooks are available.