fix(pydantic-ai): Set system instructions after instructions are set on the request object#5947
fix(pydantic-ai): Set system instructions after instructions are set on the request object#5947alexander-alderman-webb wants to merge 21 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 🐛
Internal Changes 🔧
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ 13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 6.83s 📊 Comparison with Base Branch
✨ No test changes detected All tests are passing successfully. ❌ Patch coverage is 3.23%. Project has 14900 uncovered lines. Files with missing lines (2)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 25.09% 29.80% +4.71%
==========================================
Files 191 191 —
Lines 21165 21224 +59
Branches 6946 6966 +20
==========================================
+ Hits 5310 6324 +1014
- Misses 15855 14900 -955
- Partials 432 477 +45Generated by Codecov Action |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
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.
Reviewed by Cursor Bugbot for commit 49dc6f4. Configure here.


Description
Add the
test_invoke_agent_streaming_with_instructionstest to ensure system instructions are set on the streaming path. The test is identical totest_invoke_agent_with_instructionsexcept thatagent.run_stream()is used instead ofagent.run().Remove the
messagesparameter fromai_client_span()and manually call_set_input_messages()on AI Client Spans.Move functions required for
_set_input_messages()frompydantic_ai.spans.*modules topydantic_ai.utils.Issues
Reminders
tox -e linters.feat:,fix:,ref:,meta:)