Skip to main content

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, and call_tool in 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

pydantic_ai_pgtask-0.0.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_ai_pgtask-0.0.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_ai_pgtask-0.0.1.tar.gz.

File metadata

  • Download URL: pydantic_ai_pgtask-0.0.1.tar.gz
  • Upload date:
  • Size: 9.7 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

Hashes for pydantic_ai_pgtask-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c07868b92d52df9dfdcd6063b49aae70174e6bc637014bec20987feb50966272
MD5 edd3fa63895d317b708ad7766957cf97
BLAKE2b-256 0248c93d74d1c1d8bddf35b18f8c44f00316bde4ac15481a555d35c5c6957e30

See more details on using hashes here.

File details

Details for the file pydantic_ai_pgtask-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pydantic_ai_pgtask-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 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

Hashes for pydantic_ai_pgtask-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6cda1d276954935662cf4cc2600ccd6691e6adca979df487d4e45d1806bbfbf2
MD5 749cbb305a29f2baf066ca33288dfe83
BLAKE2b-256 76a7eef1f9e4048f721913b88f3db6d0a6ff0bea440e3ccfae5fa3781b88d604

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page