fix(pydantic-ai): Use first-class hooks when available#5947
fix(pydantic-ai): Use first-class hooks when available#5947alexander-alderman-webb wants to merge 33 commits intomasterfrom
Conversation
…lying on SDK internals
…on the request object
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛Anthropic
Other
Internal Changes 🔧
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ 115 passed | Total: 115 | Pass Rate: 100% | Execution Time: 13.76s All tests are passing successfully. ❌ Patch coverage is 4.23%. Project has 15878 uncovered lines. Files with missing lines (2)
Generated by Codecov Action |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Autofix Details
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Streaming path sets input messages before model execution
- Moved
_set_input_messagesin the streaming wrapper to execute after the original stream context finishes so instruction mutations done during execution are captured.
- Moved
Or push these changes by commenting:
@cursor push 6153a56a6a
Preview (6153a56a6a)
diff --git a/sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py b/sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py
--- a/sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py
+++ b/sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py
@@ -91,13 +91,13 @@
# Create chat span for streaming request
with ai_client_span(None, model, model_settings) as span:
- if messages:
- _set_input_messages(span, messages)
-
# Call the original stream method
async with original_stream_method(self, ctx) as stream:
yield stream
+ if messages:
+ _set_input_messages(span, messages)
+
# After streaming completes, update span with response data
# The ModelRequestNode stores the final response in _result
model_response = NoneThis Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit ef7802a. Configure here.
| Test that tool execution creates execute_tool spans. | ||
| """ | ||
|
|
||
| test_agent = get_test_agent() |
There was a problem hiding this comment.
Tests create agents before hooks are registered
Medium Severity
Several tool-related tests call get_test_agent() before sentry_init(), so the agent is constructed before register_hooks patches Agent.__init__. These agents never receive the Hooks capability. When hooks are available, the fallback patches (_patch_graph_nodes, _patch_model_request) are skipped, so no gen_ai.chat spans are created. Assertions iterating over chat_spans (e.g., checking gen_ai.request.available_tools or gen_ai.response.streaming) pass vacuously because the list is empty. Affected tests include test_agent_with_tools, test_agent_with_tools_streaming, test_agent_with_tool_model_retry, test_agent_with_tool_validation_error, test_without_pii_tools, and test_include_prompts_false_with_tools.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit ef7802a. Configure here.



Description
Resolve tox failure by using the
pydantic-aihooks when available. See the failing run in #5945See changes to instruction setting in https://github.com/pydantic/pydantic-ai/pull/4123/changes#diff-71730070455237accebd709ba7fe41ce60edbd238af02e7d86a208cf8f10ab12. Per-run system instructions are now added inside
ModelRequestNode.runandModelRequestNode.stream, and are therefore not available before the functions run.Issues
Reminders
tox -e linters.feat:,fix:,ref:,meta:)