fix(pydantic-ai): Use first-class hooks when available #5947
2 issues
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
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 0 | 6m 55s | $1.70 |
| find-bugs | 2 | 8m 32s | $3.03 |
| skill-scanner | 0 | 3m 22s | $0.88 |
| security-review | 0 | 3m | $0.92 |
Duration: 21m 49s · Tokens: 4.0M in / 58.1k out · Cost: $6.57 (+extraction: $0.01, +merge: $0.00, +dedup: $0.01)