Add a Temporal `ExternalEventWaiter` so `HandleDeferredToolCalls` token handlers and `AskUser` wait durably in a workflow

Merged
DO
10d ago
+940-112

Closes #13199

Why

Under TemporalDurability, approvals through a HandleDeferredToolCalls handler that takes a resume token (formerly RequestInput) and harness AskUser questions failed the workflow with UserError: the base durability capability refuses waits an engine can't record. This adds the Temporal ExternalEventWaiter (#12975 tracks the other engines), so the same agent waits durably in a workflow, as it already does on AWS Lambda.

  • The token handler (ask below, or AskUser's answerer) runs in a local activity, agent__<name>__external_event_ask, on the worker running the workflow. The history records its result, so a replay doesn't ask again and an in-place answer is reused. It gets the live RunContext, so deps needn't be serializable.
  • Unless ask answered, the workflow waits on workflow.wait_condition; a timeout is a workflow timer and raises ExternalEventError (so HandleDeferredToolCalls denies the calls).
  • The token is <workflow ID>:<uuid>. Resolving it signals the workflow; TemporalDurability registers the signal handler on the first wait, so users declare nothing.
  • ask is the closure the workflow's agent built, so under the workflow sandbox it is the sandbox's copy unless the agent's module is imported under workflow.unsafe.imports_passed_through(); the docs warn about it, and #13227 tracks running the worker's copy instead.
  • Inside a durable unit, such as a Temporal or Azure Durable Task activity, harness AskUser's tool can't emit events yet (#7971), so it now skips AskUserRequestedEvent (via the harness's event_ctx) instead of failing; the question is still deferred and answered.

New public surface

  • pydantic_ai.durable_exec.temporal.resolve_external_event(client, token, payload) (async)
  • pydantic_ai.durable_exec.temporal.fail_external_event(client, token, reason) (async)

User-visible behavior

  @workflow.defn
  class SupportWorkflow:  # agent has TemporalDurability() + HandleDeferredToolCalls(ask, timeout=timedelta(days=1))
      @workflow.run
      async def run(self, prompt: str) -> str:
-         return (await agent.run(prompt)).output  # UserError: Temporal does not support waiting for external events yet
+         return (await agent.run(prompt)).output  # waits for the approval, durably

+ await resolve_external_event(client, token, {'approvals': {tool_call_id: True}})  # in your approval endpoint

Verification

  • tests/durable_exec/temporal/test_external_events.py on Temporal's time-skipping server: suspend and resume on a new worker without asking again (plus Replayer), approve with override_args, deny (sandboxed, defining module passed through), fail_external_event, timeout, in-place answers under the default sandbox, a late resolution of an ended wait, invalid token.
  • tests/harness/ask_user/test_temporal.py: an AskUser question answered through the token, not re-asked on replay.
  • Locally only ruff; CI runs the suites (Temporal tests run on Python < 3.14).

What changes for existing users

Token-handler HandleDeferredToolCalls (and deprecated RequestInput) and AskUser waits now work under TemporalDurability instead of raising, and every TemporalDurability agent registers one more activity (the local ask activity) with its worker.

PR decisions

2026-11-11 · ask runs in a local activity via a per-worker callback registry · iter -

  • Decision: The Temporal waiter calls ask in a local activity agent____external_event_ask that looks the ask closure up in a module-level dict keyed by run ID and event key
  • Why: ask is a closure over the live run (RequestInput requests, AskUser answerer) that can't cross a process boundary; a local activity runs in the worker process running the workflow task, and its result is a history marker, so replay reuses it and deps need not be serializable
  • Source: https://github.com/pydantic/pydantic-ai/issues/13199

2026-11-11 · token is workflow ID plus a per-wait UUID · iter -

  • Decision: Token = '<workflow_id>:' drawn with workflow.uuid4(); no run ID; signal targets the workflow ID
  • Why: A random per-wait key means a stale token (earlier wait or earlier execution reusing the workflow ID) can never resolve a later wait; parsing with rpartition works for any workflow ID; drawing from workflow.uuid4 is replay-safe and only on a path that previously raised, so no existing history changes
  • Source: https://github.com/pydantic/pydantic-ai/issues/13199

2026-11-11 · fixed signal name registered at runtime, not the dynamic handler · iter -

  • Decision: Register workflow.set_signal_handler('pydantic_ai__external_event', inbox.receive) on the first wait instead of set_dynamic_signal_handler
  • Why: Users still declare nothing, but a workflow's own dynamic signal handler is not overridden; Temporal delivers buffered signals once the handler is set; first outcome per key wins, later ones are ignored
  • Source: https://github.com/pydantic/pydantic-ai/issues/13199

2026-11-11 · client helpers are async resolve_external_event/fail_external_event in durable_exec.temporal · iter -

  • Decision: Public API: pydantic_ai.durable_exec.temporal.resolve_external_event(client, token, payload) and fail_external_event(client, token, reason); RPCError for a finished workflow propagates
  • Why: Mirrors pydantic_ai.external_events names so the docs read the same per engine; module path disambiguates; a signal API needs a Client and is async
  • Source: https://github.com/pydantic/pydantic-ai/issues/13199

2026-11-11 · ask local activity reuses activity_config, no new parameter · iter -

  • Decision: Local activity takes start_to_close/schedule_to_close/schedule_to_start timeouts, retry_policy and cancellation_type from TemporalDurability.activity_config
  • Why: YAGNI: those are the settings a local activity has; Lambda likewise reuses its base step config for ask
  • Source: https://github.com/pydantic/pydantic-ai/issues/13199

2026-11-11 · ask activity registered with every TemporalDurability agent · iter -

  • Decision: worker_registrations/temporal_activities gain one activity per agent (pinned-count tests updated)
  • Why: Per-agent naming avoids duplicate activity names when users register several agents' temporal_activities manually; registration is invisible to replay of existing histories
  • Source: https://github.com/pydantic/pydantic-ai/issues/13199

2026-11-11 · scope: no Code Mode nested-approval test, TemporalAgent wrapper untouched · iter -

  • Decision: Tests cover suspend/new-worker replay, approve, deny, fail, timeout (time-skipping), in-place answers in the sandbox, and one AskUser case; the deprecated TemporalAgent wrapper gets no waiter
  • Why: The delegating brief listed those cases; the Code Mode case is tracked by #13201; TemporalAgent has no durability capability and is deprecated
  • Source: https://github.com/pydantic/pydantic-ai/issues/13199

2026-11-11 · pre-push review 1 of 3 at 46e64665287ba747abbc382abccc2fc192cf7fea · iter -

2026-11-12 · pre-push review 1 of 3 at 46e64665287ba747abbc382abccc2fc192cf7fea · iter -

2026-11-12 · ask stays the workflow's (sandbox) copy; documented passthrough · iter -

  • Decision: Keep running the workflow's ask closure in the local activity; document that under the sandbox it is the sandbox re-import's copy and to define it in a module imported under workflow.unsafe.imports_passed_through() to share worker state
  • Why: The waiter only receives an opaque closure; running the worker-registered copy would need RequestInput/AskUser-specific reconstruction. Flagged for Douwe
  • Source: https://github.com/pydantic/pydantic-ai/issues/13199

2026-11-12 · pre-push review 2 of 3 at 7e53649ec3d482931b43dc202c8ecbde3c02118b · iter -

2026-11-12 · pre-push review 2 of 3 at 7e53649ec3d482931b43dc202c8ecbde3c02118b · iter -

2026-11-12 · pre-push review 3 of 3 at 4fa72e9f3208ef755b1c4b1cd9cbc3c5097bf201 · iter -

2026-11-12 · pre-push review 3 of 3 at 4fa72e9f3208ef755b1c4b1cd9cbc3c5097bf201 · iter -

2026-11-12 · signal handler is the callable inbox · iter -

  • Decision: Register workflow.set_signal_handler('pydantic_ai__external_event', inbox) with a callable _Inbox, found again with isinstance
  • Why: Pre-push review 1: avoid getattr duck typing on self
  • Source: https://github.com/pydantic/pydantic-ai/tree/temporal-external-events
  • Supersedes: fixed signal name registered at runtime, not the dynamic handler

2026-11-12 · AskUser tool skips its requested event under Temporal · iter -

  • Decision: ask_user_question emits AskUserRequestedEvent through harness event_ctx, which returns None for a TemporalRunContext, so under Temporal the question is deferred and answered without that event (documented, #7971)
  • Why: CI reproduced: the tool runs in an activity whose ctx.emit raises UserError, so AskUser could not ask at all on Temporal; event_ctx is the harness's established #7971 pattern; running the tool in the workflow instead would draw AskUserRequest's uuid4 in workflow code. Pre-push budget exhausted (3/3); CI and douwebot verify
  • Source: https://github.com/pydantic/pydantic-ai/pull/13215

2026-11-12 · inbox keeps only outcomes for waits in progress · iter -

2026-11-12 · ask keeps running the workflow's copy; prominent passthrough warning + follow-up #13227 · iter -

  • Decision: Douwe asked for the worker's copy via @durable_operation if it fits; it doesn't in a reasonable change, so kept the local-activity design, added warning admonitions with the imports_passed_through pattern (temporal.md, request-input.md, ask-user docs/README, skills) and filed #13227
  • Why: wait_for_external_event runs ask inside durable_unit_scope by public ExternalEventWaiter contract, where durable operations run inline by design, so routing it to its own unit needs a waiter/primitive API change (also Lambda and third-party waiters); RequestInput has no id, and durable operations require one (adding one breaks released id-less durable users or collides on a default)
  • Source: https://github.com/pydantic/pydantic-ai/issues/13227
  • Supersedes: ask stays the workflow's (sandbox) copy; documented passthrough

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.