Fail a durable run on a `UserError` from a `CodeMode` sandboxed call instead of sending the model a retry
Written by Claude Code on behalf of @DouweM.
Closes #13201
Why we make these changes
On an engine without an external-event waiter (DBOS, Prefect, Azure, Absurd, pgtask), the refusal UserError from wait_for_external_event reached the CodeMode sandbox when a sandboxed tool needed an approval resolved through a token-taking HandleDeferredToolCalls handler. The model saw a run_code retry about an unsupported engine, and a snippet's try/except Exception hid it completely, so the run succeeded. The same tool called directly fails the run. Per Douwe's decision on the issue, the run now fails.
In a durable run, UserError joins the errors CodeMode's Monty executor re-raises instead of delivering into the sandbox. This is the mechanism #12539 added for engine nondeterminism errors. The feed stops, the session resets, and the run fails with the original UserError. Plain (non-durable) runs build an empty re-raise set, as before.
New public surface
none
User-visible behavior
agent = Agent(model, toolsets=[FunctionToolset([Tool(delete_file, requires_approval=True)])],
capabilities=[CodeMode(tools=['delete_file']),
HandleDeferredToolCalls(handler=lambda ctx, requests, token: None),
DBOSDurability()])
# inside a DBOS workflow, the model runs: try: await delete_file(...) except Exception: ...
# before: the run succeeds; the snippet got "caught: DBOS does not support waiting for external events yet ..."
# after: UserError: DBOS does not support waiting for external events yet ...
Verification
tests/harness/code_mode/test_dbos.py::test_refused_wait_in_nested_call_fails_the_workflow: fails without the fix (DID NOT RAISE UserError). With the fix, the workflow fails with the refusal, and the model is asked only once, even though the snippet wraps the call intry/except.test_snippet_still_catches_the_error_outside_a_workflow[misconfigured]: outside a workflow (DBOSDurabilitytransparent), a nestedUserErrorstill reaches the snippet as before.- One engine file is enough for the refusal: the change sits in engine-agnostic CodeMode code keyed on an active durability capability, not on an engine. DBOS on SQLite runs in-process.
tests/harness/code_mode/test_temporal.py::test_nested_approval_waits_for_the_decision_and_the_snippet_gets_the_result: the test from the issue's second comment, usingHandleDeferredToolCallswith a token-taking handler (the replacement for the deprecatedRequestInput). UnderTemporalDurability, a snippet's nested call to an approval-required tool waits in the workflow. The approver resolves the token withpydantic_ai.durable_exec.temporal.resolve_external_event, the snippet gets the tool's result and the run finishes. The history then replays without asking again.
What changes for existing users
In a durable run, every UserError from a sandboxed call fails the run instead of reaching the model as a retry. That includes CodeMode's own error for a deferred or approval-required call that no handler resolved, and a host external_modules function raising UserError. Plain CodeMode runs are unchanged.
PR decisions
2026-12-02 · scope: durable runs only; UserError joins the executor's reraise · iter -
- Decision: In durable runs a UserError from any sandbox call (nested tool call, CodeMode's unresolved-deferral wrapper, host module function) fails the run; non-durable CodeMode unchanged
- Why: Douwe's ruling on #13201 (fail the run, re-raise the UserError); durable-only keeps the wider plain-CodeMode surface unchanged per AGENTS.md; delegating agent approved option (A) without a marker path
- Source: https://github.com/pydantic/pydantic-ai/issues/13201
2026-12-02 · pre-push review 1 of 3 at 1e5752a3f2f3b4be8c18ce320b5359e106e5e2f9 · iter -
- Decision: reserved (dispatching)
- Why: tree 3422066c7168cb127f719e94fcaf00b520250ff0; scope a36f5ad2ac9348e07ba5a6ee5dd8a5cd3060dba3..1e5752a3f2f3b4be8c18ce320b5359e106e5e2f9 full branch; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-13201-codemode-durable-refusal
2026-12-02 · pre-push review 1 of 3 at 1e5752a3f2f3b4be8c18ce320b5359e106e5e2f9 · iter -
- Decision: remediated: docs wording made exact (every UserError, no-handler error + refusal) in 54644433e70e5559429036468dbaac155049d5ee; declined: Temporal positive test from the issue's 2nd comment, deferred (one test file per run; delegating agent scoped tests to DBOS) and flagged in the PR body for Douwe
- Why: reviewed tree 3422066c7168cb127f719e94fcaf00b520250ff0; scope a36f5ad2ac9348e07ba5a6ee5dd8a5cd3060dba3..1e5752a3f2f3b4be8c18ce320b5359e106e5e2f9
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-13201-codemode-durable-refusal
2026-12-02 · pre-push review 2 of 3 at 54644433e70e5559429036468dbaac155049d5ee · iter -
- Decision: reserved (dispatching)
- Why: tree d00e89041096e97bc645403d4c72c92ea59e411d; scope follow-up 1e5752a3f2f3b4be8c18ce320b5359e106e5e2f9..54644433e70e5559429036468dbaac155049d5ee (docs wording) plus finding verification
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-13201-codemode-durable-refusal
2026-12-02 · pre-push review 2 of 3 at 54644433e70e5559429036468dbaac155049d5ee · iter -
- Decision: no findings (current at 54644433e70e5559429036468dbaac155049d5ee)
- Why: reviewed tree d00e89041096e97bc645403d4c72c92ea59e411d; scope 1e5752a3f2f3b4be8c18ce320b5359e106e5e2f9..54644433e70e5559429036468dbaac155049d5ee plus prior findings
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-13201-codemode-durable-refusal
2026-12-02 · Temporal nested-approval test added · iter -
- Decision: Include the Temporal positive test from the issue's 2nd comment in this PR (supersedes the earlier deferral)
- Why: Douwe asked for it alongside the fix; the one-test-file rule limits local runs per step, not files a PR touches (delegating agent)
- Source: https://github.com/pydantic/pydantic-ai/issues/13201
2026-12-02 · pre-push review 3 of 3 at 841369a3494dcd02ed290fd6524495a05b184c81 · iter -
- Decision: reserved (dispatching)
- Why: tree 0596162e1aedc50b4e71580bc4510d6b63657fc7; scope follow-up 54644433e70e5559429036468dbaac155049d5ee..841369a3494dcd02ed290fd6524495a05b184c81 (new Temporal test)
- Source: https://github.com/pydantic/pydantic-ai/pull/15829
2026-12-02 · pre-push review 3 of 3 at 841369a3494dcd02ed290fd6524495a05b184c81 · iter -
- Decision: remediated: token handler made async (a sync handler runs in a worker thread, where anyio send_nowait is not thread-safe) in 21e8007de487d499b425952f544a8fed0da594f9
- Why: reviewed tree 0596162e1aedc50b4e71580bc4510d6b63657fc7; scope 54644433e70e5559429036468dbaac155049d5ee..841369a3494dcd02ed290fd6524495a05b184c81
- Source: https://github.com/pydantic/pydantic-ai/pull/15829
2026-12-02 · pre-push remediation review at 21e8007de487d499b425952f544a8fed0da594f9 · iter -
- Decision: reserved (dispatching)
- Why: tree af63825768c6c4212c3051093072ac4bb14bfd18; scope remediation delta 841369a3494dcd02ed290fd6524495a05b184c81..21e8007de487d499b425952f544a8fed0da594f9
- Source: https://github.com/pydantic/pydantic-ai/pull/15829
2026-12-02 · pre-push remediation review at 21e8007de487d499b425952f544a8fed0da594f9 · iter -
- Decision: no findings (current at 21e8007de487d499b425952f544a8fed0da594f9); review budget spent
- Why: reviewed tree af63825768c6c4212c3051093072ac4bb14bfd18; scope 841369a3494dcd02ed290fd6524495a05b184c81..21e8007de487d499b425952f544a8fed0da594f9
- Source: https://github.com/pydantic/pydantic-ai/pull/15829
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.
Summary by cubic
Fixes #13201: in a durable run, a UserError from a CodeMode sandboxed call now fails the run instead of reaching the sandbox, where a try/except could hide it and let the run succeed. This matches direct tool calls, which already fail the run; non-durable CodeMode runs are unchanged.
A Temporal test covers the positive path: a sandboxed approval-required call waits, is recorded in the workflow, and resolves through the handler's token.