Skip to main content

pydantic-ai-persistence

Backend-agnostic persistence for PydanticAI: chat history and durable step persistence, over one small async KV interface. Backends (DynamoDB, CosmosDB, Firestore) implement AsyncKV and get both stores.

PydanticAI has two persistence layers:

  • History — you serialize result.all_messages() and store it yourself. KVHistoryStore does exactly that.
  • Step persistence — PydanticAI's StepStore (append-only events, continuable snapshots, tool-effect ledger) for durable/resumable runs. KVStepStore implements the async StepStore protocol.

Install

pip install pydantic-ai-persistence            # core (in-memory backend)
pip install "pydantic-ai-persistence[dynamodb]"  # + DynamoDB backend

Core pieces

Component What it does
AsyncKV tiny async interface a backend implements (put/get/query/delete)
InMemoryAsyncKV in-memory backend for tests/dev
KVHistoryStore save(conversation_id, messages) / load(conversation_id)
KVStepStore implements PydanticAI's async StepStore over an AsyncKV

History

from pydantic_ai import Agent
from pydantic_ai_persistence import KVHistoryStore, InMemoryAsyncKV

store = KVHistoryStore(InMemoryAsyncKV())

result = agent.run_sync("Hi, I'm Kamal")
await store.save("conv-1", result.all_messages())

prior = await store.load("conv-1")
result = agent.run_sync("What's my name?", message_history=prior)

Step persistence

from pydantic_ai import Agent
from pydantic_ai_harness.step_persistence import StepPersistence
from pydantic_ai_persistence import KVStepStore, InMemoryAsyncKV

step_store = KVStepStore(InMemoryAsyncKV(), max_snapshots_per_run=10)
agent = Agent("openai:gpt-4o", capabilities=[StepPersistence(store=step_store)])

KVStepStore records runs, append-only events, continuable snapshots (with pruning), and the tool-effect ledger — everything the StepStore protocol requires.

Build your own backend

Implement AsyncKV (four async methods) and hand it to KVStepStore / KVHistoryStore. See pydantic-ai-dynamodb-persistence for an example.

Note: StepStore is a beta/experimental PydanticAI harness feature; its API may still change.

License

MIT

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_persistence-0.1.0.tar.gz (7.3 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_persistence-0.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_ai_persistence-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic_ai_persistence-0.1.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for pydantic_ai_persistence-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bd7dba206d1e58bd4c1c8ebbbb87dd8068511d323e8ab632fe7166818c5f388f
MD5 bd71f4c6d515eedbd384394eeede3f8a
BLAKE2b-256 c03919e1367a7c38e3982d0d132130ecc9da717cd553e8c84ac38f5019ae0726

See more details on using hashes here.

File details

Details for the file pydantic_ai_persistence-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_ai_persistence-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0d035ed246ecb11c0ccbc80c79f61ffbb02953b4406d767231178c43a275c13
MD5 6cd33e5c09d59bcd4be4beaf2be09fe8
BLAKE2b-256 dd86ba4d78f9f888f6cdc8f39ac4807952023ed72549163d08979cd73afed740

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