Run tool calls as Temporal child workflows via `child_workflow` tool metadata in `TemporalDurability`

Merged
YH
1mo ago
+1243-48

Inside a Temporal workflow, every function tool call runs as an activity. That doesn't work for a tool that runs a nested agent with its own TemporalDurability: the nested agent's model requests and tool calls would either run inside one long activity, losing their durability, or run inline in the parent's workflow code and grow its history. This adds an opt-in way to run an async function tool as a Temporal child workflow instead, using the metadata contract agreed on #6744:

@toolset.tool_plain(metadata={'temporal': {'child_workflow': ChildWorkflowConfig(...)}})
async def research(topic: str) -> str:
    return (await researcher.run(topic)).output
  • Selection: the 'temporal' tool metadata (set on the tool or through SetToolMetadata) can now be {'child_workflow': ChildWorkflowConfig(...)}, besides False or an ActivityConfig. child_workflow must be the only key, and the config is validated against Temporal's ChildWorkflowConfig. Sync, dynamic and MCP tools are rejected with a UserError. Argument validation for such a tool still runs as an activity, because it never executes the tool body.
  • Execution: the tool body runs as workflow code in the child, so a nested durable agent's operations are recorded in the child's own history. When id is omitted, the child ID is <parent workflow ID>--<workflow.uuid4()>, which is deterministic on replay. Cancelling the parent cancels the child without livelocking the workflow task. An ordinary exception in the tool body fails the child workflow (subject to its retry_policy) and reaches the parent's tool call, like a failed activity.
  • Registration: every function toolset's call-tool operation gets a generated workflow definition, named <call-tool activity name>__child_workflow and pinned in the durable-exec compatibility test. PydanticAIPlugin and AgentPlugin register it with the worker only when, at worker configuration, a tool in that toolset has child_workflow metadata (@agent.tool tools included) or a SetToolMetadata capability sets it. Workers for agents that don't use the feature register exactly the activities they did before. Metadata that first appears later fails the call with a UserError naming the toolset. No new public property is added: temporal_activities raises a UserError when a child workflow would be registered, because registering only activities would leave it out. The generated workflow refuses to run unless it was started as a child.
  • Unchanged default: tools without child_workflow metadata keep running as activities.

The docs gain a "Child workflows" section with a full example of a lead agent delegating to a nested durable agent, including registering both agents. The agent skill's Temporal section covers the option too. The tests cover metadata errors failing the workflow, a child workflow running an activity (with replay), a nested TemporalDurability agent running in the child, ModelRetry (with replay), CallDeferred and an ordinary exception raised from a child workflow tool, an @agent.tool child workflow tool, metadata added after the worker was configured, top-level start rejection, class-name collisions between similar agent names, and parent cancellation under both cancellation types, all against a real Temporal dev server.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.