Skip to content

Commit 443ddac

Browse files
authored
Document that various functions need to be async to be used with Temporal (#2809)
1 parent 35d054d commit 443ddac

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

docs/temporal.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,20 @@ For more information on how to use Temporal in Python applications, see their [P
155155

156156
There are a few considerations specific to agents and toolsets when using Temporal for durable execution. These are important to understand to ensure that your agents and toolsets work correctly with Temporal's workflow and activity model.
157157

158-
### Agent and Toolset Requirements
158+
### Agent Names and Toolset IDs
159159

160160
To ensure that Temporal knows what code to run when an activity fails or is interrupted and then restarted, even if your code is changed in between, each activity needs to have a name that's stable and unique.
161161

162162
When `TemporalAgent` dynamically creates activities for the wrapped agent's model requests and toolsets (specifically those that implement their own tool listing and calling, i.e. [`FunctionToolset`][pydantic_ai.toolsets.FunctionToolset] and [`MCPServer`][pydantic_ai.mcp.MCPServer]), their names are derived from the agent's [`name`][pydantic_ai.agent.AbstractAgent.name] and the toolsets' [`id`s][pydantic_ai.toolsets.AbstractToolset.id]. These fields are normally optional, but are required to be set when using Temporal. They should not be changed once the durable agent has been deployed to production as this would break active workflows.
163163

164164
Other than that, any agent and toolset will just work!
165165

166+
### Instructions Functions, Output Functions, and History Processors
167+
168+
Pydantic AI runs non-async [instructions](agents.md#instructions) and [system prompt](agents.md#system-prompts) functions, [history processors](message-history.md#processing-message-history), [output functions](output.md#output-functions), and [output validators](output.md#output-validator-functions) in threads, which are not supported inside Temporal workflows and require an activity. Ensure that these functions are async instead.
169+
170+
Synchronous tool functions are supported, as tools are automatically run in activities unless this is [explicitly disabled](#activity-configuration). Still, it's recommended to make tool functions async as well to improve performance.
171+
166172
### Agent Run Context and Dependencies
167173

168174
As workflows and activities run in separate processes, any values passed between them need to be serializable. As these payloads are stored in the workflow execution event history, Temporal limits their size to 2MB.

tests/test_temporal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,12 +1617,12 @@ def setup_logfire(send_to_logfire: bool = True, metrics: Literal[False] | None =
16171617

16181618

16191619
@hitl_agent.tool
1620-
def create_file(ctx: RunContext[None], path: str) -> None:
1620+
async def create_file(ctx: RunContext[None], path: str) -> None:
16211621
raise CallDeferred
16221622

16231623

16241624
@hitl_agent.tool
1625-
def delete_file(ctx: RunContext[None], path: str) -> bool:
1625+
async def delete_file(ctx: RunContext[None], path: str) -> bool:
16261626
if not ctx.tool_call_approved:
16271627
raise ApprovalRequired
16281628
return True

0 commit comments

Comments
 (0)