Add `ToolDefinition.durable` to run a tool outside its durable unit on every durable execution engine
- Closes #13203
Adds ToolDefinition.durable: bool | None, a typed tool field that says where a durable execution engine runs a tool. durable=False runs an async function tool outside its durable unit (activity, step or task), in workflow code, on every engine built on BaseDurabilityCapability: Temporal, DBOS, Prefect, AWS Lambda, Azure Durable Task, Absurd, pgtask and Render Workflows. The deprecated TemporalAgent also reads it. Douwe chose the name durable on the issue, and then asked for a typed field instead of a metadata key, following sequential and timeout.
The PR adds only the field. Nothing changes for a tool that doesn't set it, apart from the wording of the per-run toolset rejection message. SubAgents and FileSystem keep their {'temporal': False} metadata. Switching them gets its own PR with per-engine tests: #13204 for SubAgents, and #13224 for FileSystem, which raises the replay and side-effect question per engine.
Public API
ToolDefinition.durable: bool | None = None. It isn't sent to the model.Nonekeeps the engine's default placement.Falseruns the tool outside its durable unit.Trueputs the default back over an earlier opt-out.
- The same
durable=parameter whereversequentialandtimeoutcan be set:Tool(...)Agent.toolandAgent.tool_plainFunctionToolset(...), as a default for its tools, includingToolinstances that don't set oneFunctionToolset.tool,.tool_plainand.add_functionCapability.toolandCapability.tool_plain
- Tools you don't define, such as a capability's or an MCP server's: set the field in a
preparefunction or withPrepareTools(replace(tool_def, durable=False)). There is no definition-level sibling ofSetToolMetadata, so the docs showPrepareTools.
Engine behaviour
- Core: one place for every engine.
BaseDurabilityCapabilityreadstool_def.durablein the function, dynamic and MCP toolset resolvers, before the engine backend's per-tool config. So no engine needs its own code, and Absurd and pgtask can now opt a tool out. - Precedence. An engine's own metadata key wins when it's set on the tool and keeps carrying the engine's config:
'temporal','prefect','aws_lambda','azure_durable_task'or'render_workflows'. Otherwisedurable=Falsewins over config set in code, such as Azure's and Render'sresolve_tool_options. - One rule for the tools an engine wraps. Only async function tools can opt out. On a sync tool, an MCP tool or a dynamic toolset's tool that the engine wraps,
durable=Falseraises aUserError. DBOS doesn't wrap function tools, so it doesn't check them. - Deprecated wrappers.
resolve_tool_durable_config, whichTemporalAgentuses, reads the field after the engine key, with Temporal's own refusals.PrefectAgentdoesn't read it, and the Prefect page says so. - Per-run toolsets. A
FunctionToolsetpassed per run whose tools are all async and setdurable=Falseis accepted, as one opted out with the engine key is. The rejection message now suggestsdurable=Falseand names the engine's key where there is one. Absurd and pgtask now show the hint too. - Render Workflows. Its registration check no longer holds a tool with
durable=Falseto its toolset's task options, since the tool runs in the calling task run.
pydantic_ai/durable_exec/_toolset.py :: DurableFunctionToolset.call_tool()
└─ pydantic_ai/durable_exec/_base.py :: _build_function_toolset.resolve_tool_config(tool, name)
+ └─ _base.py :: BaseDurabilityCapability._runs_outside_durable_unit(tool, name)
+ ├─ _toolset.py :: opts_out_of_durable_unit(tool_def.durable, tool_def.metadata, engine_key=...)
+ │ └─ engine key set on the tool → engine decides; else durable is False
+ └─ async function tool → False: the tool runs in workflow code
└─ backend.config_for_tool(...) → the unit's config
Docs
- New section. "Running a tool outside the durable unit" in the durable execution overview, with a per-engine table and a
PrepareToolsexample. - Links to it from the Temporal and Prefect pages, the workspace guide, and the AWS Lambda, Azure, Absurd, pgtask and Render pages and their READMEs.
- Skill references. The core and harness skill references are updated.
Not done here
SubAgentsandFileSystemstay on{'temporal': False}(see #13204 and #13224).- No per-engine keys for Absurd and pgtask. Their steps take no per-step config, so the field covers the only thing a key would do.
ask_usersets no tool metadata onmain, so it has nothing to switch.
Verification
- New in-process tests, through
Agent.runon a stub engine:- the parameter reaches the
ToolDefinitionfrom every setter, with the toolset default - the opt-out, and precedence of the engine key
PrepareToolssettingFalseandTrue- argument validation running inline
- the
UserErrors for sync, dynamic and MCP tools - acceptance of a toolset passed per run
- the parameter reaches the
- Extended tests:
- the Temporal resolver test covers the deprecated wrapper path
- a Render test covers registration
- dumped
ToolDefinitionsnapshots gain the new field
- Contract manifest:
scripts/durable_exec_contract.jsonregenerated. - Locally:
ruffand pre-commit hooks only. CI runs tests, type checks and coverage.
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
Adds ToolDefinition.durable so durable=False runs an async function tool in workflow code instead of its durable unit (activity, step, or task) on every engine built on BaseDurabilityCapability and on the deprecated TemporalAgent; durable=True restores the default. Closes #13203.
Behavior
- Set it like
sequentialortimeout:@agent.tool,Tool(...),@toolset.tool, a wholeFunctionToolset, orPrepareTools/preparefor tools you don't define, includingadd_function. - An engine's own metadata key wins when set; otherwise
durable=Falseoverrides per-tool config set in code. - Only async function tools can opt out; sync, MCP, and dynamic toolset tools raise a
UserError. DBOS runs function tools inline and doesn't check it. - A per-run
FunctionToolsetis accepted when all its tools opt out; the rejection hint now suggestsdurable=Falseand names the engine's key. - Render no longer holds a
durable=Falsetool to its toolset's task options; its resolver must returnFalsefor opt-outs from apreparefunction. - The deprecated
PrefectAgentdoesn't readdurable; use'prefect': Falsethere. - An opted-out tool runs again on every replay, so keep it deterministic and I/O-free.
- Docs cover the option across the engine guides, with each engine's opt-out key named in the workspace guide.
SubAgentsandFileSystemkeep their{'temporal': False}metadata.