Run tool calls as Temporal child workflows via `child_workflow` tool metadata in `TemporalDurability`
- Closes #6744
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 throughSetToolMetadata) can now be{'child_workflow': ChildWorkflowConfig(...)}, besidesFalseor anActivityConfig.child_workflowmust be the only key, and the config is validated against Temporal'sChildWorkflowConfig. Sync, dynamic and MCP tools are rejected with aUserError. 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
idis 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 itsretry_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_workflowand pinned in the durable-exec compatibility test.PydanticAIPluginandAgentPluginregister it with the worker only when, at worker configuration, a tool in that toolset haschild_workflowmetadata (@agent.tooltools included) or aSetToolMetadatacapability 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 aUserErrornaming the toolset. No new public property is added:temporal_activitiesraises aUserErrorwhen 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_workflowmetadata 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.