Pydantic AI pgtask
Durable execution for Pydantic AI agents, on Postgres alone.
Agents run for a while, a model call, a tool call, another model call. When the worker dies in the middle of that, the run is usually lost: you restart from zero and pay for every token again.
Pydantic AI pgtask fixes that. Call agent.run() inside a durable pgtask handler and every model and MCP call is checkpointed into Postgres. If the worker crashes, a new one resumes from the last completed step, no restart, no re-spent tokens. Same idea as Pydantic AI's Temporal integration, but with no Temporal, no Redis, no broker: just the Postgres you already have.
Installation
pip install pydantic-ai-pgtask
Example
import asyncio
import os
from typing import Any
from pgtask import Client, Task, TaskRegistry, Worker
from pydantic_ai import Agent
from pydantic_ai_pgtask import PGTaskDurability
tasks = TaskRegistry(queue_name="agents")
agent = Agent("openai:gpt-5.2", name="analyst", capabilities=[PGTaskDurability()])
@tasks.task("analyse")
async def analyse(task: Task, payload: dict[str, Any]) -> dict[str, Any]:
result = await agent.run(payload["prompt"])
return {"output": result.output}
async def main() -> None:
database_url = os.environ["PGTASK_DATABASE_URL"]
client = await Client.connect(database_url)
await client.migrate()
await client.enqueue(analyse.request({"prompt": "Analyse Q3 revenue"}))
await Worker(database_url, tasks).run()
if __name__ == "__main__":
asyncio.run(main())
You author a task, call the agent inside it, and run it durably. That's the whole idea.
How it works
PGTaskDurability is a Pydantic AI capability. When the agent runs inside any pgtask handler (discovered via pgtask's get_current_task()), it wraps:
- every model request in a
task.step(...)checkpoint, - every function tool call in its own checkpoint, so side effects run exactly once,
- every MCP
get_tools,get_instructions, andcall_toolin its own checkpoint.
Step results are stored in Postgres as JSON. On a retry after a crash, pgtask replays completed steps from their cached results instead of re-executing them, so the run resumes exactly where it stopped.
Outside a durable task the capability is transparent: the agent behaves like a regular, non-durable agent.
!!! warning "Deterministic step order"
Repeated step names are disambiguated with an encounter-order occurrence counter, so tool calls run in 'sequential' mode by default. 'parallel' execution is excluded by type - a replay must reach checkpoints in the same order they were recorded.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_ai_pgtask-0.0.2.tar.gz.
File metadata
- Download URL: pydantic_ai_pgtask-0.0.2.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.12 {"installer":{"name":"uv","version":"0.9.12"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc621c06cae67924a329a66af7f0a3275966240db3726402617431cfdca1af8f
|
|
| MD5 |
1dc96852f3395d9d776e55424d6b5a0a
|
|
| BLAKE2b-256 |
cca9dba9fd638e51f46e8f870a44e9efcb89a8b7c4a1343a27911f550dcad657
|
File details
Details for the file pydantic_ai_pgtask-0.0.2-py3-none-any.whl.
File metadata
- Download URL: pydantic_ai_pgtask-0.0.2-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.12 {"installer":{"name":"uv","version":"0.9.12"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0958f40b58b0eb229ed59cb3fbdc21dc127ee1b3abc32e62a6fdb57cb2d19a78
|
|
| MD5 |
49c635eaa381a7f5fe6faefd72577871
|
|
| BLAKE2b-256 |
0f35584237701b7bc72ea22074cad19aa53a9f0311e10e2dcba58e26923bf3e0
|