Keep a run paused for `DeferredToolRequests` or `PendingModelResponse` when content is queued, and deliver that content to the resumed run
- Closes #7537
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. BackgroundToolsno 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:
test_enqueue_at_deferred_tool_pause_is_carried_to_the_resumed_runtest_enqueue_recorded_at_a_deferred_tool_pause_reaches_the_resumed_run_oncetest_content_carried_from_a_pause_leaves_how_the_history_resumes_unchangedtest_carried_content_announces_its_delivery_oncetest_carried_content_is_not_accepted_from_an_untrusted_historytest_carried_binary_content_survives_storing_the_historytest_enqueue_at_pending_model_response_is_carried_to_the_resumed_runtest_result_finished_at_a_fully_deferred_pause_reaches_the_resumed_runtest_task_cancelled_at_a_pause_is_reported_to_the_resumed_runtest_task_cancelled_while_the_provider_works_is_reported_once_to_the_resumed_runtest_report_queued_at_a_deferred_pause_reaches_the_resumed_run_once
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 -
- Decision: A run ending with DeferredToolRequests or PendingModelResponse returns its pause whatever is queued
- Why: Redirecting skips approvals silently; Douwe's suggested fix
- Source: https://github.com/pydantic/pydantic-ai/issues/7537#issuecomment-6499476446
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 -
- Decision: Emit EnqueuedMessagesEvent when content is committed at the pause and dispatch it to on_event listeners immediately; no second event on redelivery
- Why: SubAgents acknowledges on the event; without it a report was delivered twice (measured)
- Source: https://github.com/pydantic/pydantic-ai/issues/7537#issuecomment-6499476446
2026-10-30 · pre-push review 1 of 3 at c8123494ac8ed0bcde6433044efe7efa308f92b1 · iter -
- Decision: reserved; dispatching fresh reviewer
- Why: tree 4a96ba47cba71614b709acafb9d5a3ccb2763e82; scope 43ddebfc88ac8ba29b3a59dd3c48e5467c2df7a3..c8123494ac8ed0bcde6433044efe7efa308f92b1 full branch; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/fix/issue-7537-keep-pause-carry-queued
2026-10-30 · Keep BackgroundTools cancellation notice in this PR · iter -
- Decision: Maintainer chose to keep the notice for tasks a pause cancels; add PendingModelResponse coverage; live probes owed before merge
- Why: Pre-push review 1 flagged it as new steering text outside #7537 scope
- Source: https://github.com/pydantic/pydantic-ai/issues/11741
2026-10-30 · run_stream gaps go to a follow-up issue · iter -
- Decision: File one issue for run_stream: content queued at a pause missing from the streamed result history, and the queue closing before late hooks
- Why: Both reproduce identically on origin/main; out of scope for this fix
- Source: https://github.com/pydantic/pydantic-ai/issues/7537#issuecomment-6499476446
2026-10-30 · pre-push review 1 of 3 at c8123494ac8ed0bcde6433044efe7efa308f92b1 · iter -
- Decision: remediated: typed carried record, single suspended-request owner, docstrings, binary/partial/PendingModelResponse tests; cancellation notice kept by maintainer
- Why: tree 4a96ba47cba71614b709acafb9d5a3ccb2763e82; full branch from 43ddebfc88; codex second opinion also triaged (run_stream gaps pre-existing, follow-up issue)
- Source: https://github.com/pydantic/pydantic-ai/tree/fix/issue-7537-keep-pause-carry-queued
2026-10-30 · pre-push review 2 of 3 at 9cc51f07034efa58213fa69d98be50a659dcd54e · iter -
- Decision: reserved; dispatching fresh reviewer
- Why: tree da70ad5e203f94a9b6f470a8c6fe8829902df613; follow-up scope c8123494ac..9cc51f07034efa58213fa69d98be50a659dcd54e plus prior findings
- Source: https://github.com/pydantic/pydantic-ai/tree/fix/issue-7537-keep-pause-carry-queued
2026-10-30 · pre-push review 2 of 3 at 9cc51f07034efa58213fa69d98be50a659dcd54e · iter -
- Decision: no findings (current); all review 1 findings verified fixed
- Why: tree da70ad5e203f94a9b6f470a8c6fe8829902df613; follow-up scope c8123494ac..9cc51f0703
- Source: https://github.com/pydantic/pydantic-ai/tree/fix/issue-7537-keep-pause-carry-queued
2026-10-30 · defer: run_stream gaps at a pause · iter -
- Decision: Filed as #11955 (immediate work, assignee adtyavrdhn) instead of fixing here
- Why: Out of scope for this PR: both reproduce identically on origin/main
- Source: https://github.com/pydantic/pydantic-ai/issues/11955
2026-10-30 · exempt delta after review 2 at 72be3f1f13892b0ed88bf0408beb5d8ef65ec357 · iter -
- Decision: test-only coverage delta, no further pre-push review
- Why: chains from reviewed 9cc51f07034efa58213fa69d98be50a659dcd54e; diff touches tests/test_capability_pending_messages.py only (missing coverage)
- Source: https://github.com/pydantic/pydantic-ai/pull/11956
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.