Skip to content

fix(pydantic-ai): Set system instructions after instructions are set on the request object#5947

Draft
alexander-alderman-webb wants to merge 21 commits intomasterfrom
webb/pydantic-ai/move-instruction-fetching
Draft

fix(pydantic-ai): Set system instructions after instructions are set on the request object#5947
alexander-alderman-webb wants to merge 21 commits intomasterfrom
webb/pydantic-ai/move-instruction-fetching

Conversation

@alexander-alderman-webb
Copy link
Copy Markdown
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Apr 7, 2026

Description

Add the test_invoke_agent_streaming_with_instructions test to ensure system instructions are set on the streaming path. The test is identical to test_invoke_agent_with_instructions except that agent.run_stream() is used instead of agent.run().

Remove the messages parameter from ai_client_span() and manually call _set_input_messages() on AI Client Spans.

Move functions required for _set_input_messages() from pydantic_ai.spans.* modules to pydantic_ai.utils.

Issues

Reminders

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (ai) Redact base64 data URLs in image_url content blocks by ericapisani in #5953
  • (integrations) Instrument pyreqwest tracing by servusdei2018 in #5682

Bug Fixes 🐛

  • (pydantic-ai) Set system instructions after instructions are set on the request object by alexander-alderman-webb in #5947

Internal Changes 🔧

  • (openai) Split token counting by API for easier deprecation by ericapisani in #5930
  • (opentelemetry) Ignore mypy error by alexander-alderman-webb in #5927
  • Fix license metadata in setup.py by sl0thentr0py in #5934
  • Update validate-pr workflow by stephanie-anderson in #5931

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 6.83s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 3.23%. Project has 14900 uncovered lines.
✅ Project coverage is 29.8%. Comparing base (base) to head (head).

Files with missing lines (2)
File Patch % Lines
agent_run.py 0.00% ⚠️ 100 Missing
__init__.py 8.96% ⚠️ 61 Missing
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       +45

Generated by Codecov Action

@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review April 7, 2026 10:46
@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner April 7, 2026 10:46
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

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_messages in the streaming wrapper to execute after the original stream context finishes so instruction mutations done during execution are captured.

Create PR

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 = None

This 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.

@alexander-alderman-webb alexander-alderman-webb marked this pull request as draft April 7, 2026 11:23
@alexander-alderman-webb alexander-alderman-webb changed the base branch from webb/pydantic-ai/remove-test to master April 8, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant