Add `ToolDefinition.durable` to run a tool outside its durable unit on every durable execution engine

Merged
DO
17d ago
+737-80

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.
    • None keeps the engine's default placement.
    • False runs the tool outside its durable unit.
    • True puts the default back over an earlier opt-out.
  • The same durable= parameter wherever sequential and timeout can be set:
    • Tool(...)
    • Agent.tool and Agent.tool_plain
    • FunctionToolset(...), as a default for its tools, including Tool instances that don't set one
    • FunctionToolset.tool, .tool_plain and .add_function
    • Capability.tool and Capability.tool_plain
  • Tools you don't define, such as a capability's or an MCP server's: set the field in a prepare function or with PrepareTools (replace(tool_def, durable=False)). There is no definition-level sibling of SetToolMetadata, so the docs show PrepareTools.

Engine behaviour

  • Core: one place for every engine. BaseDurabilityCapability reads tool_def.durable in 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'. Otherwise durable=False wins over config set in code, such as Azure's and Render's resolve_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=False raises a UserError. DBOS doesn't wrap function tools, so it doesn't check them.
  • Deprecated wrappers. resolve_tool_durable_config, which TemporalAgent uses, reads the field after the engine key, with Temporal's own refusals. PrefectAgent doesn't read it, and the Prefect page says so.
  • Per-run toolsets. A FunctionToolset passed per run whose tools are all async and set durable=False is accepted, as one opted out with the engine key is. The rejection message now suggests durable=False and 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=False to 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 PrepareTools example.
  • 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

  • SubAgents and FileSystem stay 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_user sets no tool metadata on main, so it has nothing to switch.

Verification

  • New in-process tests, through Agent.run on a stub engine:
    • the parameter reaches the ToolDefinition from every setter, with the toolset default
    • the opt-out, and precedence of the engine key
    • PrepareTools setting False and True
    • argument validation running inline
    • the UserErrors for sync, dynamic and MCP tools
    • acceptance of a toolset passed per run
  • Extended tests:
    • the Temporal resolver test covers the deprecated wrapper path
    • a Render test covers registration
    • dumped ToolDefinition snapshots gain the new field
  • Contract manifest: scripts/durable_exec_contract.json regenerated.
  • Locally: ruff and 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 sequential or timeout: @agent.tool, Tool(...), @toolset.tool, a whole FunctionToolset, or PrepareTools/prepare for tools you don't define, including add_function.
  • An engine's own metadata key wins when set; otherwise durable=False overrides 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 FunctionToolset is accepted when all its tools opt out; the rejection hint now suggests durable=False and names the engine's key.
  • Render no longer holds a durable=False tool to its toolset's task options; its resolver must return False for opt-outs from a prepare function.
  • The deprecated PrefectAgent doesn't read durable; use 'prefect': False there.
  • 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. SubAgents and FileSystem keep their {'temporal': False} metadata.