Pause again when a deferred pause whose step also ran other tools is resumed without `deferred_tool_results`

Merged
AD
17d ago
+962-60

Part of #7537 (first of a stack; the follow-up PR closes it).

[!WARNING]
Compatibility impact: Resuming a stored history from a deferred-tool pause with no new prompt and no deferred_tool_results, where the paused step also ran other tools, used to send the history to the model and continue. It now returns DeferredToolRequests for the waiting calls again, without a model request.

Why this can ship in a minor release: Continuing silently skipped the approval and left the history unable to accept the real result later. The new behavior matches what the same resume already does when every call in the step was deferred, and returns an output type the caller already declared and handled on the first run.

Migration: Pass deferred_tool_results for the waiting calls to continue, or call pydantic_ai.messages.repair_messages() on the history first to abandon them.

Durable execution: Because the same history now produces different model calls, a Temporal workflow that is in flight across the upgrade and replays such a resume may fail with a nondeterminism error. Let in-flight workflows finish before upgrading.

Why we make these changes

A run that pauses for approval after a step that also executed other tools stores [ModelResponse(calls a, b), ModelRequest(result a)]. Resuming that history with no prompt and no deferred_tool_results sent it to the model with a synthesized "interrupted" result for b, and the run continued. The approval for b was skipped, and a later deferred_tool_results for b failed with "Tool call results were provided, but the message history does not contain any unprocessed tool calls." This is one way Adam lost an approval in #7537.

When every call in the step was deferred, the history ends on the response, and a resume goes back through CallToolsNode, so the deferred calls defer again. This PR gives the mixed shape the same treatment: the resume goes back through CallToolsNode for the response and skips the calls the trailing request already answered. Douwe described this as the intended design when we discussed the issue.

The run marks the pause explicitly. When it ends with DeferredToolRequests, the request it appends with the step's other results, and any content it records at the pause, carry {'__pydantic_ai__': {'deferred_pause': True}} in their metadata. A plain resume treats the history as a pause when every request after the last response carries that mark and some call of the response is still unanswered (answers are counted by tool name and call ID, and a legacy tool-bound RetryPromptPart counts). Anything else after the response keeps main's behavior: a UI adapter's new message, or a new prompt whose model request failed.

The mark lives in the reserved __pydantic_ai__ namespace, which sanitize_messages already drops from client messages, so a client can't claim a pause to get a call executed. It drops this mark silently. When cleaning merges two requests, the merged request keeps the mark only if both had it, so repair_messages(..., repair_last_response=False) keeps a pause. An earlier version inferred the pause from run_id, but a run_id only says which run wrote a request, not that the run paused (see the decision log).

New public surface

none

User-visible behavior

  agent.run(message_history=paused_history)
- → continues; `b` is answered with a synthesized interrupted return and its approval is skipped
+ → DeferredToolRequests(approvals=[b]) again; no model request; history unchanged
  agent.run(message_history=paused_history, deferred_tool_results=...)
  → unchanged: runs `b` with its result
  agent.run('Never mind.', message_history=paused_history)
  → unchanged

  paused_result.all_messages()[-1].metadata
- None
+ {'__pydantic_ai__': {'deferred_pause': True}}

Verification

  • tests/test_transcript_repair.py::test_resuming_a_mixed_deferred_pause_pauses_again: the plain resume returns the same pause, makes no model request, doesn't rerun the answered call, and the approval still works afterwards. Fails on main.
  • test_resuming_a_mixed_deferred_pause_runs_a_call_that_no_longer_defers: a waiting call that runs on the resume sends its result after the earlier one. Fails on main.
  • test_resuming_a_mixed_deferred_pause_with_a_repeated_call_id_runs_only_the_waiting_call: answered calls are skipped by count, so a repeated ID doesn't skip the waiting call. Fails on main.
  • test_resuming_after_content_recorded_at_an_all_deferred_pause_pauses_again: a marked request after an all-deferred response is part of the pause. Fails on main.
  • test_content_queued_at_a_second_pause_keeps_the_pause: content a later resumed run records when it pauses again keeps the pause, though another run wrote the response.
  • test_resuming_after_a_new_prompt_failed_at_a_mixed_deferred_pause_retries_the_prompt: the history captured from a failed new prompt isn't a pause; resuming it sends the prompt. Fails with run_id-based detection.
  • test_a_paused_history_repaired_without_closing_its_last_response_keeps_the_pause: repair_messages merges the marked requests and the pause holds. Fails with run_id-based detection.
  • test_resuming_a_mixed_deferred_pause_from_run_stream_pauses_again: run_stream() writes its own tool-results request, which now carries the mark too, so a plain resume of its history pauses again. Fails without the mark.
  • test_approving_after_a_resume_ran_another_call_skips_every_answered_call: results in any request after the response are skipped when approving.
  • tests/test_sanitize_messages.py::test_sanitize_messages_drops_a_deferred_pause_record_silently and tests/test_ui.py::test_run_stream_does_not_execute_a_call_from_a_forged_deferred_pause: a client-forged mark is dropped and the call isn't executed.
  • test_new_message_after_a_mixed_deferred_pause_continues_the_run and test_new_message_after_unanswered_call_in_history_is_repaired: a new message from elsewhere after the pause still continues with a synthesized return, as on main.
  • test_resuming_an_interrupted_step_with_an_unanswered_call_does_not_run_it: an interrupted step isn't treated as a pause.
  • test_resuming_a_pause_with_several_recorded_requests_keeps_pausing: several marked requests, merged or kept apart by their instructions, still pause on a first and a second resume.
  • test_a_legacy_retry_prompt_answers_its_call_at_a_pause: a RetryPromptPart answering the other call means nothing is waiting, so the run continues. Fails when only ToolReturnPart counts.
  • test_deferred_run_history_not_silently_repaired from #6319 is unchanged from main.
  • Targeted deferred/approval/resume tests in test_agent.py, test_tools.py, test_streaming.py, test_capability_pending_messages.py, test_vercel_ai.py, test_ag_ui.py, test_conversation.py and test_ui.py pass; nine snapshots there now show the mark.

What changes for existing users

A plain resume of a mixed paused history pauses again instead of skipping the approval. Requests a run writes at a deferred pause carry the mark in metadata; the Vercel AI adapter already leaves the reserved namespace out of what it sends to clients.

Notes for review

  • Douwe: your #6319 test is untouched here; a new prompt on this history still continues as on main. Per your answer on #13054, #13054 makes it raise and updates the test.
  • CallToolsNode gains a private _answered_calls init field, following ModelRequestNode(_resume_suspended=...). Answered calls are left out only where they're passed to process_tool_calls, so the end_strategy='early' check still sees the whole response, as on the first pass.
PR decisions

Entry format

## YYYY-MM-DD · <short title> · iter <N or "-">
- Decision: <one line>
- Why: <one line>
- Source: <link to comment/thread/issue/commit — mandatory>
- Session: <harness session id — written by the script when the harness sets one>
- Supersedes: <earlier entry title, if applicable>

No prose. No multi-paragraph rationale. The Source link carries the full context.

One exception — modality. When the Decision restates a modal claim (always / never / only if / must / unless), quote that clause verbatim instead of paraphrasing it. Brevity everywhere else, but "always X unless Y" compressed to "always X" is a different instruction, and the next agent has no way to notice the qualifier went missing.

When to append

  • You made a pick between two valid implementations and the reviewer later asked about it
  • A thread resolution chose path A over B
  • A maintainer answered an escalation and that answer settled an ambiguity
  • You deviated from the plan while coding (.claude/plans/<branch>.md, or PLAN.md on a plan-only PR)
  • A CI finding forced a design change

When NOT to append

  • Routine implementation work already captured by the diff
  • Decisions already spelled out in the issue or in issue-brief.md
  • Research notes (those go in local-notes/)

2026-11-10 · Raise instead of re-pausing on a plain resume of a mixed deferred pause · iter -

  • Decision: A plain resume and a new prompt both raise UserError; the pause and stored history stay untouched
  • Why: Re-pausing would re-run CallToolsNode on a copied response (workspace_ref stamping and compaction read the response), or relax the all-results guard on deferred_tool_results. Raising keeps the pause and names the fix. The test from #6319 that let a new prompt through is updated: docs/message-history.md:400 already promised the raise, and Douwe on #7537 (2026-10-29) asked that a pause not be silently skipped. Session 6d7fbd3d-5c31-4680-8a58-6ff4f94cd7c1.
  • Source: https://github.com/pydantic/pydantic-ai/issues/7537
  • Session: 6d7fbd3d-5c31-4680-8a58-6ff4f94cd7c1

2026-11-10 · pre-push review 1 of 3 at 64c5044b2b1f596347403dd7c698bf7456e0afc5 · iter -

2026-11-10 · pre-push review 1 of 3 at 64c5044b2b1f596347403dd7c698bf7456e0afc5 · iter -

2026-11-10 · pre-push review 2 of 3 at fb323eb37 · iter -

2026-11-10 · pre-push review 2 of 3 at fb323eb3701c2bf4d4b6699cfa3ddc465debc477 · iter -

2026-11-10 · pre-push review 3 of 3 at 0a97338fa · iter -

2026-11-10 · pre-push review 3 of 3 at 0a97338fa51bb89b70e35633ad330e3b3a928184 · iter -

2026-11-10 · douwebot on 848d80ea6: guard broader than documented · iter -

  • Decision: Narrowed in ef13a6291/2c1c7cc73: raise only when the trailing request answers at least one call of the final response (the mixed-pause signature). A trailing request with no results (UI adapter new-message shape) keeps main's send-time repair; regression test test_new_message_after_unanswered_call_in_history_is_repaired fails without the narrowing. Docs bullet already described only the narrowed shape.
  • Why: Avoid an undocumented behavior change for UI adapter users
  • Source: https://github.com/pydantic/pydantic-ai/pull/13051#pullrequestreview-5846302574
  • Session: 6d7fbd3d-5c31-4680-8a58-6ff4f94cd7c1

2026-11-12 · Pause again instead of raising on a plain resume of a mixed deferred pause · iter -

  • Decision: Supersedes the 2026-11-10 raise decision. In 07b20119f a plain resume (no prompt, no deferred_tool_results) of [ModelResponse(a, b), ModelRequest(result a)] goes through CallToolsNode for the response, skipping calls the trailing request answered, so a deferred b pauses again with no model request and the history unchanged. A new prompt on that history keeps main's behavior; test_deferred_run_history_not_silently_repaired is back to its main version. Troubleshooting entry for the removed UserError dropped.
  • Why: Maintainer call with Douwe 2026-11-10: resuming this shape was designed to go back through CallToolsNode so deferred calls defer again; returning DeferredToolRequests again is less breaking than a new exception, and the author chose to leave the new-prompt case as on main
  • Source: https://github.com/pydantic/pydantic-ai/pull/13051
  • Session: 6d7fbd3d-5c31-4680-8a58-6ff4f94cd7c1

2026-11-12 · Recognize the pause from the stored history · iter -

  • Decision: In ca89e274d the pause is detected on ctx.state.message_history before cleaning: every request after the last complete response answers one of its calls or shares its run_id, at least one call is unanswered (by count), and none is interrupted. Answered calls are skipped by count at the process_tool_calls call site.
  • Why: gpt-6.1-sol review found a set-based skip dropped repeated IDs, filtering before the early-output gate changed its outcome, a UI message after the pause was treated as a resume, and recorded pause content (run_id-stamped) was not
  • Source: https://github.com/pydantic/pydantic-ai/pull/13051
  • Session: 6d7fbd3d-5c31-4680-8a58-6ff4f94cd7c1

2026-11-12 · Keep a resumed pause's history as stored · iter -

  • Decision: In 6b316c08d a resumed pause runs on the stored history instead of the cleaned one, the response is found by searching back, and legacy RetryPromptPart counts as an answer (via _is_tool_result_part)
  • Why: gpt-6.1-sol second pass: merged requests lost the run_id so a second resume continued; requests with different instructions stayed separate and len-2 hit a request; a retry prompt answering a call was treated as unanswered
  • Source: https://github.com/pydantic/pydantic-ai/pull/13051
  • Session: 6d7fbd3d-5c31-4680-8a58-6ff4f94cd7c1

2026-11-12 · Mark the pause in reserved metadata instead of inferring it from run_id · iter -

  • Decision: In aceadbe3f the run writes __pydantic_ai__.deferred_pause on the request it appends at a DeferredToolRequests pause and on content recorded there; a plain resume is a pause only when every request after the last response has it. Merging keeps it only when both requests have it; sanitize_messages drops it silently; the sanitizer's run_id clearing is reverted. Supersedes "Recognize the pause from the stored history" and "Keep a resumed pause's history as stored".
  • Why: gpt-6.1-sol third pass: run_id proves authorship, not a pause. A new prompt whose model request failed carries one, and repair_messages merging drops it.
  • Source: https://github.com/pydantic/pydantic-ai/pull/13051
  • Session: 6d7fbd3d-5c31-4680-8a58-6ff4f94cd7c1

2026-11-12 · Mark run_stream()'s tool-results request too · iter -

  • Decision: In b6e5b4be2 _handle_final_result and run_stream()'s on_complete both build the request through final_tool_results_request, which marks it when the run ends with DeferredToolRequests.
  • Why: gpt-6.1-sol fourth pass: run_stream() appended its own copy of the request without the mark, so a plain resume of its history asked the model instead of pausing.
  • Source: https://github.com/pydantic/pydantic-ai/pull/13051
  • Session: 6d7fbd3d-5c31-4680-8a58-6ff4f94cd7c1

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.