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.KVHistoryStoredoes exactly that. - Step persistence — PydanticAI's
StepStore(append-only events, continuable snapshots, tool-effect ledger) for durable/resumable runs.KVStepStoreimplements the asyncStepStoreprotocol.
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:
StepStoreis 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
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_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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd7dba206d1e58bd4c1c8ebbbb87dd8068511d323e8ab632fe7166818c5f388f
|
|
| MD5 |
bd71f4c6d515eedbd384394eeede3f8a
|
|
| BLAKE2b-256 |
c03919e1367a7c38e3982d0d132130ecc9da717cd553e8c84ac38f5019ae0726
|
File details
Details for the file pydantic_ai_persistence-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_ai_persistence-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0d035ed246ecb11c0ccbc80c79f61ffbb02953b4406d767231178c43a275c13
|
|
| MD5 |
6cd33e5c09d59bcd4be4beaf2be09fe8
|
|
| BLAKE2b-256 |
dd86ba4d78f9f888f6cdc8f39ac4807952023ed72549163d08979cd73afed740
|