Skip to content

Conversation

@dsfaccini
Copy link
Contributor

@dsfaccini dsfaccini commented Oct 21, 2025

Fixes #3207

When running multiple agents sequentially with message_history, retry ModelRequest objects created by CallToolsNode were missing instructions. This caused Model._get_instructions() to search the message history and return the first agent's instructions instead of the current agent's.

The fix ensures all retry ModelRequest objects are initialized with instructions from the current agent via ctx.deps.get_instructions(run_context).

Changes:

  • pydantic_ai_slim/pydantic_ai/_agent_graph.py: Set instructions on empty retry requests (line 572-576) and tool retry requests (line 638-642)
  • tests/test_agent.py: Update test assertion to expect instructions on retry requests
  • tests/test_multi_agent_instructions.py: New test file with 3 test cases for multi-agent scenarios

I'll add some materials that can be useful for discussion (the code we used to test, the logs we added, the output of those logs, claude's conclussions and proposed solutions and the implemented one):

dsfaccini and others added 3 commits October 21, 2025 01:22
Add test case that reproduces issue pydantic#3207 where Agent2's instructions
are ignored when running sequentially with Agent1's message history.

The test verifies that each agent uses its own instructions rather than
inheriting instructions from previous agents in the workflow.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
…c#3207)

When running multiple agents sequentially with message_history, retry
requests created by CallToolsNode were missing instructions. This caused
Model._get_instructions() to fall back to searching message history and
incorrectly return the previous agent's instructions instead of the
current agent's.

Fixed by ensuring all retry ModelRequest objects created in CallToolsNode
are initialized with instructions from the current agent via
ctx.deps.get_instructions(run_context).

Changes:
- Set instructions on empty retry requests (line 572-576)
- Set instructions on tool retry requests (line 638-642)
- Update test assertion to expect instructions on retry requests

Fixes pydantic#3207

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
The try/except block in test_multi_agent_sequential_instructions_with_output_type
was never executed since the fix ensures Agent2 works correctly. Removing it
achieves 100% test coverage as required by CI.

Related to pydantic#3207
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please move this to test_agent.py


# Run Agent2 with Agent1's message history, capturing messages
# This is the scenario that triggers the bug in issue #3207
with capture_run_messages() as agent2_messages:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't need this, we can use result.{new,all}_messages()

instructions_in_agent2_requests = [req.instructions for req in agent2_requests if req.instructions is not None]

# Agent2 should use its own instructions, not Agent1's
agent2_instructions_found = 'Agent 2 instructions' in instructions_in_agent2_requests
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can directly assert this, not need for the LLM assertion message stuff below

result1 = agent1.run_sync('Hello')

# Run Agent2 with Agent1's message history
result2 = agent2.run_sync('Hello again', message_history=result1.new_messages())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wasn't this always working correctly because there's a user prompt?

result1 = agent1.run_sync('Hello')

# Run Agent2 WITH a user_prompt (workaround)
result2 = agent2.run_sync('Continue', message_history=result1.new_messages())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wasn't the scenario from the issue one where there's no user_prompt and there is an output type, so that we end up with a "retry" ModelRequest? Which is tested in the first test?

I'm not sure we're testing exactly what the user reported.

@DouweM DouweM marked this pull request as ready for review October 23, 2025 14:16
@DouweM DouweM changed the title Fix multi-agent instruction handling (#3207) Use correct agent's instructions when telling model to retry output Oct 23, 2025
@DouweM
Copy link
Collaborator

DouweM commented Oct 23, 2025

@dsfaccini Thanks David!

@DouweM DouweM merged commit 4d9f5d7 into pydantic:main Oct 23, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Instruction seems to be ignored in back to back agent calls with tool output

2 participants