Skip to main content

pydantic-ai-mongodb-memory

⚠️ ALPHA — NOT AN OFFICIAL MONGODB PRODUCT. This integration is in Alpha and is not a supported or official MongoDB product. Use at your own risk.

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: the store always constructs and owns its own MongoClient from your connection string, so connection appName = devrel-integ-pydanticai-python and handshake driver_info name pydantic-ai-mongodb-memory are always present and non-overridable; embeddings voyage-3.5 (1024-dim). Extra MongoClient options (e.g. tls=True, maxPoolSize=…) can be passed through as keyword args.

v0.1.1: the store now always owns its client — pass connection_string (the former client= parameter was removed) so appName + driver-info are guaranteed on every connection.

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

Metadata

Release files for pydantic-ai-mongodb-memory 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-ai-mongodb-memory 0.1.2
File Size Uploaded Provenance
pydantic_ai_mongodb_memory-0.1.2.tar.gz 19.9 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-ai-mongodb-memory 0.1.2
File Interpreter ABI Platform
pydantic_ai_mongodb_memory-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 35.6 kB

Release files / pydantic_ai_mongodb_memory-0.1.2.tar.gz

Download URL pydantic_ai_mongodb_memory-0.1.2.tar.gz
Size 19.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8138a1c58f6b5e21ce41f1306c2aff709a50e633c9b0984c20f04529a2db5cff
BLAKE2b-256 checksum
How to use checksums
07ac49bebbeb483cd731bc7fb1a1cf20c91d61f17a311b37b016e8b01d0b89cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 18, 2026.

Transparency log

Release files / pydantic_ai_mongodb_memory-0.1.2-py3-none-any.whl

Download URL pydantic_ai_mongodb_memory-0.1.2-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2e13becd0394a848b1bdb320848b8fe2fd8a369953b293d33d4f2217083a85fc
BLAKE2b-256 checksum
How to use checksums
bdb3e4e03fe3af910f3866aa0648651ac1d81f774817b736c01334c168832398
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 18, 2026.

Transparency log

Security for pydantic-ai-mongodb-memory 0.1.2

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pydantic_ai_mongodb_memory-0.1.2.tar.gz

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 Jun 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page