Keep a run paused for `DeferredToolRequests` or `PendingModelResponse` when content is queued, and deliver that content to the resumed run

Closed
AD
14d ago
+743-104

Why we make these changes

Content queued when a run pauses (DeferredToolRequests or PendingModelResponse) made the end-of-run drain redirect the run into another model request, so the pause was dropped and a required approval was skipped. #11752 (unreleased) kept the pause only when the paused step's own request ends the history; every other case fell through to the redirect, including the common BackgroundTools case raised in its review. Now a pause always stands, and queued content reaches the resumed run once:

  • When the paused step's request ends the history, the content is appended after it, as in #11752.
  • Otherwise appending would change how the history resumes (a trailing request turns a pending approval into an 'interrupted' result, and abandons a suspended provider job), so the content is kept in the paused message's reserved __pydantic_ai__ metadata and queued again when the next run starts. UI adapters already strip that namespace from client messages.
  • BackgroundTools no longer makes this decision itself, and tells the model when a pause cancelled a still-running call instead of leaving the "result will arrive automatically" promise (from #11741).

New public surface

none

User-visible behavior

  pydantic_ai/run.py :: AgentRun._run_node_with_hooks()
  └─ capabilities/_pending_messages.py :: drain_pending_messages_at_end(End(DeferredToolRequests | PendingModelResponse))
-    └─ ModelRequestNode(request=queued)    -> model called again, approval skipped
+    └─ _keep_at_pause()                    -> pause returned; content appended or carried
  pydantic_ai/agent/__init__.py :: Agent._prepare_run(message_history)
+ └─ _enqueue.py :: take_carried_over()     -> carried content queued for the next model request

Verification

Tests:

All new tests fail on main. Targeted suites (pending messages, BackgroundTools, SubAgents, batch resume, append-only history, UI adapters, test_agent) pass; Pyright and Ruff are clean.

Before merge: the cancellation notice is new model-facing text, so it still needs the live probes agent_docs/message-history-and-caching.md asks for.

What changes for existing users

A run that pauses with content queued now stays paused instead of continuing without the approval, and its EnqueuedMessagesEvent reaches on_event listeners at the pause. Related follow-up: #11955 (run_stream() gaps that also exist on main).

PR decisions

2026-10-30 · Never redirect a pause · iter -

2026-10-30 · Carry queued content in reserved metadata of the last message · iter -

  • Decision: Content that cannot be appended without changing how the history resumes is stored in pydantic_ai metadata of the paused last message and re-queued at the next run's start
  • Why: Measured: appending changes resume decisions; reordering rewrites seen history; leaving it on pending_messages loses BackgroundTools results
  • Source: https://github.com/pydantic/pydantic-ai/pull/11752#discussion_r4415406598

2026-10-30 · Keep #11752 append path · iter -

  • Decision: Append after the step's own request when it ends the history and nothing queued is a ModelResponse; carry otherwise
  • Why: Appended content survives UI client round trips, which strip reserved metadata
  • Source: https://github.com/pydantic/pydantic-ai/pull/11752

2026-10-30 · Announce at the pause · iter -

2026-10-30 · pre-push review 1 of 3 at c8123494ac8ed0bcde6433044efe7efa308f92b1 · iter -

2026-10-30 · Keep BackgroundTools cancellation notice in this PR · iter -

2026-10-30 · run_stream gaps go to a follow-up issue · iter -

2026-10-30 · pre-push review 1 of 3 at c8123494ac8ed0bcde6433044efe7efa308f92b1 · iter -

2026-10-30 · pre-push review 2 of 3 at 9cc51f07034efa58213fa69d98be50a659dcd54e · iter -

2026-10-30 · pre-push review 2 of 3 at 9cc51f07034efa58213fa69d98be50a659dcd54e · iter -

2026-10-30 · defer: run_stream gaps at a pause · iter -

2026-10-30 · exempt delta after review 2 at 72be3f1f13892b0ed88bf0408beb5d8ef65ec357 · iter -

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.