翻篇收录说明
翻篇网页快照

Durable Pydantic AI agents with Azure Durable Task

https://pydantic.dev/articles/azure-durable-task-pydantic-ai

最近抓取:2026-12-03 23:19:43(北京时间)

最近内容变化:2026-11-30 18:47:47(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Make Pydantic AI agents survive worker restarts with Azure Durable Task Scheduler. Model requests and tool calls run as activities by default.

正文摘录

/Pydantic AI Durable Pydantic AI agents with Azure Durable Task Laís Carvalho 8 mins 2026/11/16 As Markdown A support agent gets a ticket: order A-1001 arrived damaged. It asks the model what to do, looks up the order, asks the model again, and issues a refund. Then the worker running it is killed by a deploy, a scale-in event, or an out-of-memory error, and the process takes everything it knew about the run with it. Restart the run from the top and you pay for the model requests twice, call the order API twice, and maybe refund the customer twice. Don't restart it and the ticket sits half-handled until someone notices. Durable Task is Microsoft's answer to that problem, and it is the engine behind Azure Durable Functions. You write an orchestrator as ordinary Python code. The work it delegates runs as activities, and Azure Durable Task Scheduler records the result of every completed activity. When a worker dies, another worker picks up the orchestration, replays the recorded results, and carries on from the step that was in flight. Pydantic AI Harness now ships AzureDurableTaskDurability, a capability that brings the agent loop into that model. By default, model requests, function tool calls, MCP calls, and durable capability operations each run as a Durable Task activity. Outside an orchestration the agent runs exactly as it did before. The Azure team announced it on their side too, in Introducing Azure Durable Task for Pydantic AI agents. #Getting started uv add logfire "pydantic-ai-harness[azure-durable-task]" "pydantic-ai-slim[anthropic]" The azure-durable-task extra installs the Durable Task Python SDK, the Azure-managed worker and client, and azure-identity. The examples use Anthropic, but any provider Pydantic AI supports works. Pydantic Logfire instruments the agent, so each run shows up as a trace with every model request, tool call, and activity. Set your model provider key and a Logfire write token before running the examples: export ANTHROPIC_API_KEY='your-anthropic-api-key' export LOGFIRE_TOKEN='your-logfire-write-token' For local development, start the Durable Task Scheduler emulator in Docker: docker run --rm --name dts-emulator \ -p 127.0.0.1:8080:8080 -p 127.0.0.1:8082:8082 \ mcr.microsoft.com/dts/dts-emulator:v0.0.9 The emulator serves the SDK on port 8080 with a task hub called default, and its dashboard on http://localhost:8082. No Azure subscription needed yet. #A durable support agent Attach the capability to the agent, give the agent a name, and write the orchestrator. The name prefixes every activity the agent registers, so it is recorded in orchestration history. Save this as support_agent.py: from collections.abc import Generator import logfire from durabletask import task from pydantic_ai import Agent, RunContext from pydantic_ai_harness.azure_durable_task import AzureDurableTaskDurability, run_durable logfire.configure(service_name='support-agent') logfire.instrument_pydantic_ai() agent = Agent( 'anthropic:claude-sonnet-5-5', name='support', instructions='Look up the order, then refund it if it arrived damaged.', capabilities=[AzureDurableTaskDurability()], ) @agent.tool_plain def lookup_order(order_id: str) -> dict[str, str]: return {'order_id': order_id, 'status': 'delivered', 'note': 'box arrived crushed', 'total': '42.00'} @agent.tool def issue_refund(ctx: RunContext[None], order_id: str, amount: str) -> str: idempotency_key = f'{ctx.run_id}:{ctx.tool_call_id}' logfire.info('refund {order_id} {amount}', order_id=order_id, amount=amount, idempotency_key=idempotency_key) # Stub: call your payments API here with idempotency_key, so a repeated call is a no-op. return f'Refunded {amount} on {order_id}' def support(ctx: task.OrchestrationContext, prompt: str) -> Generator[task.Task[object], object, str]: result = yield from run_durable(ctx, lambda: agent.run(prompt)) return result.output The tools are ordinary Pydantic AI tools. issue_refund is a stub that logs the refund to Logfire instead of moving money, so the example runs without a payments account. Nothing about either tool changes to make it durable, apart from the idempotency key, which the section on replay explains. run_durable is the bridge between the Durable Task generator style and an async agent run, and you call it wherever the agent belongs in your workflow. The orchestrator returns JSON-compatible output. If the agent has a Pydantic model as output_type, return result.output.model_dump(mode='json'). Finally, start a worker and schedule an orchestration from main.py: import logfire from durabletask.client import TaskHubGrpcClient from durabletask.worker import TaskHubGrpcWorker from support_agent import AzureDurableTaskDurability, agent, support emulator = dict(host_address='localhost:8080', metadata=[('taskhub', 'default')], secure_channel=False) worker = TaskHubGrpcWorker(**emulator) worker.add_orchestrator(support) durability = AzureDurableTaskDurability.from_agent(agent) assert durability is not None with durability.register(worker), worker, TaskHubGrpcClient(**emulator) as client: worker.start() with logfire.span('support ticket A-1001'): instance_id = client.schedule_new_orchestration( support, input='Order A-1001 arrived damaged. Please sort it out.' ) state = client.wait_for_orchestration_completion(instance_id, timeout=60) assert state is not None state.raise_if_failed() print(state.get_output()) Agent binds its own copy of each capability, so from_agent fetches the bound one. Enter register(worker) before the worker starts and leave it after the worker stops. It registers the agent's activities and runs them. Orchestrators and clients are plain Durable Task SDK code, and a client can live in a different process that never imports the agent. The logfire.span wraps client code, which never replays, so it appears once per ticket in Logfire. #What happens when the worker dies I ran the agent above against the emulator and killed the worker with os._exit(1) inside issue_refund, after the refund ran and before its result reached the scheduler. Then I started a second worker. For the test, each tool also appended a line to a file every time its body executed: lookup_order A-1001 pid=10761 issue_refund A-1001 key=d3885ebc...:support:d6b9f9de...:toolu_01M455EcNkh3yCYePF2ieJAt pid=10761 issue_refund A-1001 key=d3885ebc...:support:d6b9f9de...:toolu_01M455EcNkh3yCYePF2ieJAt pid=10821 The first worker made two model requests, ran lookup_order, and died inside issue_refund. The second worker didn't repeat any of the first three activities. It replayed them from history, ran issue_refund again because its result was never recorded, made the final model request, and completed the orchestration about a second later: COMPLETED I've refunded order A-1001 in full, $42.00. The order was marked delivered, and its note said the box arrived crushed, which matches the damage you reported. The refund tool ran twice. That is the honest shape of durable execution, and it brings us to the rules. #Replay and side effects Each time an activity completes, the SDK runs your orchestrator again from the start and serves every completed result from history. That is how a replacement worker resumes the orchestration, and it means a few things about how you write the agent: Activities run at least once. A worker can die after a tool's side effect and before its result is recorded, as it did above. Make tools idempotent. ctx.run_id and ctx.tool_call_id survive the restart unchanged, which makes them a good idempotency key: the two issue_refund lines carry the same one. Orchestrator-side code runs on every replay. Hooks, instruction functions, output validators, and inline tools must be deterministic and must not do network, file, or thread I/O. Put I/O in a tool or a capability's @durable_operation method. Names are persisted. Activity names come from the agent's name, toolset IDs, and tool names. Don't rename any of them while orchestrations that use them are still running. Replay cost grows with run length. Each replay re-runs the agent up to its latest step, so orchestrator CPU grows with the square of the number of steps. Keep very long conversations in separate orchestrations. Spans repeat. Pydantic AI spans created in orchestrator code are emitted again on every replay, so one run shows its agent run and model request spans several times in Logfire. Errors change type. An exception raised in a model request or tool reaches the orchestrator as durabletask.task.TaskFailedError. UserError keeps its type. Dependencies follow the same logic. They cross into activities as data, so they must be JSON-serializable (dataclasses, Pydantic models, and JSON values), and activities rebuild them from the agent's deps_type. Create database clients and HTTP sessions inside the tool that uses them, not in deps. #Approvals that do not occupy an activity while they wait A refund above a threshold, a production deploy, a payment: some tool calls need a person to say yes. With Durable Task the approval belongs to the orchestrator, and the orchestration waits for an external event. No activity runs while it waits, so a review can take five minutes or three days. Mark the tool with requires_approval=True and add DeferredToolRequests to the output type: from collections.abc import Generator from durabletask.task import OrchestrationContext, Task from pydantic import TypeAdapter import logfire from pydantic_ai import Agent, DeferredToolRequests, DeferredToolResults, UserError from pydantic_ai_harness.azure_durable_task import AzureDurableTaskDurability, run_durable logfire.configure(service_name='payment-review') logfire.instrument_pydantic_ai() agent = Agent( 'anthropic:claude-sonnet-5-5', name='review', output_type=[str, DeferredToolRequests], capabilities=[AzureDurableTaskDurability()], ) @agent.tool_plain(requires_approval=True) def approve_payment(amount: int) -> str: return f'Paid {amount}' def review(ctx: OrchestrationContext, prompt: str) -> Generator[Task