MongoDB Atlas-backed persistent memory capability for Pydantic AI / pydantic-ai-harness.
Project description
pydantic-ai-mongodb-memory
A MongoDB Atlas–backed memory capability for Pydantic AI
and pydantic-ai-harness.
Drop MongoDBMemoryCapability into an agent's capabilities=[...] array and it gains
durable, cross-session, semantically-searchable memory — backed by MongoDB documents +
Atlas Vector Search. Anchored to pydantic-ai-harness#255.
Why
In a stateless agent, every new session forgets the user. Re-injecting the full transcript inflates tokens and hits context limits. This capability gives the agent a tiered memory:
- Short-term / episodic — recent turns, semantically searchable via
$vectorSearch. - Long-term structured profile — durable user preferences (always injected).
- Semantic recall — Voyage 3.5 embeddings + Atlas Vector Search, with prefiltering.
Install
pip install -e ".[embeddings]" # pymongo + pydantic-ai-slim + voyageai
Quickstart
from pydantic_ai import Agent
from pydantic_ai_mongodb_memory import MongoDBMemoryCapability
memory = MongoDBMemoryCapability(
connection_string="mongodb+srv://...",
database_name="agent_memory",
semantic_recall=True, # uses Atlas Vector Search + voyage-3.5
)
agent = Agent("google:gemini-2.0-flash", capabilities=[memory])
The capability implements two AbstractCapability lifecycle hooks:
before_model_request→ recalls memories for the active scope and injects them.after_model_request→ persists the latest turn(s) back to MongoDB.
How it maps to MongoDB
| Method | MongoDB operation |
|---|---|
add_memory(scope, role, content) |
insert_one (+ voyage-3.5 embed if semantic) |
get_recent(scope, n) |
find({scope}).sort([(ts,-1),(_id,-1)]) |
recall_semantic(scope, query, k, filters=…) |
$vectorSearch on embedding + prefilter |
set_preference(scope, k, v) |
update_one(upsert) on user_profiles |
get_profile(scope) |
find_one on user_profiles |
Conventions: connection appName = devrel-integ-pydanticai-python; handshake
driver_info name pydantic-ai-mongodb-memory; embeddings voyage-3.5 (1024-dim).
Demos
Both auto-load demo/.env (ATLAS_URI, VOYAGE_API_KEY, GEMINI_API_KEY).
python demo/main.py # data-level: seed team dataset, multi-turn, scope isolation, $vectorSearch
python demo/agent_demo.py # a Gemini agent: cross-session memory + Atlas Vector Search w/ department prefilter
agent_demo.py proves the headline value: an agent told "only staff Engineering" in
session 1 answers a fresh session-2 staffing question using $vectorSearch prefiltered
to Engineering — recalling the constraint from MongoDB with no history passed in.
Tests
PYTHONPATH=src pytest tests/ -v
Non-search tests run on mongomock (offline). test_vector_recall needs ATLAS_URI +
VOYAGE_API_KEY and is skipped otherwise. Suite: round-trip, scope isolation, profile
upsert, TTL index, vector recall, appName present, driver-info present.
Layout
src/pydantic_ai_mongodb_memory/
capability.py # MongoDBMemoryCapability (AbstractCapability subclass)
store.py # MongoDBMemoryStore — connection, indexes, CRUD, $vectorSearch
embeddings.py # voyage-3.5 helper
demo/ # main.py (data) + agent_demo.py (Gemini agent)
tests/ # acceptance suite
PLAN.md # the 7-phase build plan
Project details
Release history Release notifications | RSS feed
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_mongodb_memory-0.1.0.tar.gz.
File metadata
- Download URL: pydantic_ai_mongodb_memory-0.1.0.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0213f961a6bee21d72ce3c707066eccc7ad02c3f227e63275c2d4f4657f7eb3e
|
|
| MD5 |
a3e1341c022ddbe10dea59e81bdc2986
|
|
| BLAKE2b-256 |
7428e4d9df9d2e0b52f0ab300a7c20779e0e99885052d4a72077e77b70a53279
|
Provenance
The following attestation bundles were made for pydantic_ai_mongodb_memory-0.1.0.tar.gz:
Publisher:
release.yml on mongodb-developer/pydantic-ai-mongodb-memory
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_ai_mongodb_memory-0.1.0.tar.gz -
Subject digest:
0213f961a6bee21d72ce3c707066eccc7ad02c3f227e63275c2d4f4657f7eb3e - Sigstore transparency entry: 1692468258
- Sigstore integration time:
-
Permalink:
mongodb-developer/pydantic-ai-mongodb-memory@eda617fad7326ed0956dd05b6bb46392a0b51ace -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mongodb-developer
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@eda617fad7326ed0956dd05b6bb46392a0b51ace -
Trigger Event:
push
-
Statement type:
File details
Details for the file pydantic_ai_mongodb_memory-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_ai_mongodb_memory-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7886c734fb07e0149d70f337acea6a48b513ac4055ad63037c20622a1b70ca2a
|
|
| MD5 |
64c29cf7c5ba948ddf4da897753c4d03
|
|
| BLAKE2b-256 |
e2177900a705df28e309e6f2ec49b79f750e30aa000537bae58a3512ffec7ca9
|
Provenance
The following attestation bundles were made for pydantic_ai_mongodb_memory-0.1.0-py3-none-any.whl:
Publisher:
release.yml on mongodb-developer/pydantic-ai-mongodb-memory
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_ai_mongodb_memory-0.1.0-py3-none-any.whl -
Subject digest:
7886c734fb07e0149d70f337acea6a48b513ac4055ad63037c20622a1b70ca2a - Sigstore transparency entry: 1692468357
- Sigstore integration time:
-
Permalink:
mongodb-developer/pydantic-ai-mongodb-memory@eda617fad7326ed0956dd05b6bb46392a0b51ace -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mongodb-developer
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@eda617fad7326ed0956dd05b6bb46392a0b51ace -
Trigger Event:
push
-
Statement type: