This release is a pre-release and may not be stable for production use.
django-pydantic-ai
Use Django as a persistent backend for Pydantic AI agent memory. The package provides an asynchronous memory store for Pydantic AI Harness, backed by Django models and your configured database.
Requirements
- Python 3.12 or newer
- Django 6.1 or newer
Table of Contents
Installation
pip install django-pydantic-ai
The package depends on Pydantic AI and Pydantic AI Harness; pip installs them along with Django.
Django Setup
Add the memory app to INSTALLED_APPS:
INSTALLED_APPS = [
# Your other Django apps...
"django_pydantic_ai.memory",
]
Configure a database as usual, then create the memory tables:
python manage.py migrate
Use With An Agent
Create a Directory for each isolation boundary you need, such as a user, tenant, or agent. A DjangoModelStore is bound to one directory. The following async factory creates or retrieves that directory and attaches its store to the Harness Memory capability:
from pydantic_ai import Agent
from pydantic_ai_harness.memory import Memory
from django_pydantic_ai.memory.models import Directory
from django_pydantic_ai.memory.store import DjangoModelStore
async def create_agent(model: str, user_id: int) -> Agent:
directory, _ = await Directory.objects.aget_or_create(name=f"user:{user_id}")
store = DjangoModelStore(directory)
return Agent(model, capabilities=[Memory(store=store)])
Use the resulting agent through Pydantic AI's async API:
agent = await create_agent("your-model", user_id=42)
result = await agent.run("Remember that my preferred language is Portuguese.")
The Memory capability injects the main memory notebook into agent context and provides tools for writing, reading, deleting, and searching memory files. Its default in-memory store is process-local; pass DjangoModelStore to persist data in Django's database across runs.
Store Behavior
- Files and operation receipts are scoped to the
Directorypassed toDjangoModelStore. - Writes use a SHA-256 hex digest of the file content as the string version. Identical content has the same version.
expected_version=Nonerequires that a file does not already exist. Supplying a version requires the stored file to still have that version; otherwise the store raisesMemoryConflictError.- Delete mutations return
Noneas their version because deleted content has no hash. - Supplying a
MemoryOperationto a write or delete records a receipt so a repeated operation can return its prior result. Reusing an operation ID with different arguments raisesMemoryOperationConflictError. - Store methods are asynchronous. Reads, writes, deletes, path listing, and lexical search operate on the directory bound to the store.
Development
Run the test suite with Hatch:
hatch run pytest
License
django-pydantic-ai is distributed under the terms of the MIT license.
Metadata
Release files for django-pydantic-ai 6.1.0a1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_pydantic_ai-6.1.0a1.tar.gz | 16.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_pydantic_ai-6.1.0a1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.0 kB
Release files / django_pydantic_ai-6.1.0a1.tar.gz
| Download URL | django_pydantic_ai-6.1.0a1.tar.gz |
|---|---|
| Size | 16.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
92d937a60f04d6393fcbff6cd867d36f6ff27857b08dc0f80b04bb4dbd7a65f0
|
|
BLAKE2b-256 checksum How to use checksums |
b958c7c3a6ada48616a6cabff57db81f40b5a0e179ea1a373160f14aca2e80b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.
Transparency logRelease files / django_pydantic_ai-6.1.0a1-py3-none-any.whl
| Download URL | django_pydantic_ai-6.1.0a1-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
64b4a92f852111e9147162cb13e093c718f10b8956ccce1cc2768484483e2925
|
|
BLAKE2b-256 checksum How to use checksums |
24498e4797982ac0f1647ba5131a1a427abd43cdcb73d7eee6d3289b39dbc151
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.
Transparency log