Skip to main content

MemState - Transactional Memory for AI Agents

Agents hallucinate because their memory drifts. SQL says one thing, the Vector DB says another. MemState keeps them in sync, always.

Mental Model: MemState extends database transactions to your Vector DB.
One unit. One commit. One rollback.

PyPI version PyPI Downloads Python versions License Tests


Documentation: https://scream4ik.github.io/MemState/

Source Code: https://github.com/scream4ik/MemState


MemState Demo


Quick Start

pip install memstate[chromadb]
from pydantic import BaseModel
from memstate import MemoryStore, SQLiteStorage, HookError
from memstate.integrations.chroma import ChromaSyncHook
import chromadb

# 1. Define Data Schema
class UserPref(BaseModel):
    content: str
    role: str

# 2. Setup Storage (Local)
sqlite = SQLiteStorage("agent_memory.db")
chroma = chromadb.Client()

# 3. Initialize with Sync Hook
mem = MemoryStore(sqlite)
mem.add_hook(ChromaSyncHook(chroma, "agent_memory", text_field="content", metadata_fields=["role"]))
mem.register_schema("preference", UserPref)

# 4. Atomic Commit
# Validates Pydantic model -> Writes SQL -> Upserts Vector
try:
    mem.commit_model(model=UserPref(content="User prefers vegetarian", role="preference"))
except HookError as e:
    print("Commit failed, SQL rolled back automatically:", e)

# 5. Undo (if needed)
# mem.rollback(1)

👉 See full Documentation & Examples


The Problem

AI agents usually store memory in two places: SQL (structured facts) and Vector DB (semantic search).

These two stores drift easily. If a network request to the Vector DB fails, or the agent crashes mid-operation, you end up with "Split-Brain" memory:

  • SQL: "User lives in London"
  • Vector DB: "User lives in New York" (Stale embedding)

Result: The agent retrieves wrong context and hallucinates.

Key Features

MemState acts as a Consistency Layer between your agent and its storage.

  • Atomic Commits: SQL and Vector DB stay in sync. If one fails, both rollback.
  • Async & Fast: Full asyncio support for high-performance FastAPI/LangGraph apps.
  • Type Safety: Pydantic validation prevents LLMs from corrupting your JSON schema.
  • Hybrid Search: Search by meaning (Vector), filter by facts (SQL).
  • Time Travel: Undo N steps with rollback(n). Great for user corrections.

Proof: Benchmark under failure

1000 memory updates with 10% random vector DB failures:

METRIC MANUAL SYNC MEMSTATE
SQL Records 1000 900
Vector Records 910 900
DATA DRIFT 90 0
INCONSISTENCY RATE 9.0% 0.0%

Why 900 instead of 1000? MemState refuses partial writes.
If vector sync fails, SQL is rolled back automatically.

Manual sync produces silent drift.
Drift compounds over time, stale embeddings keep being retrieved forever.

Full benchmark script: benchmarks/


Ecosystem

Category Supported
Storage Backends SQLite, PostgreSQL (JSONB), Redis, In-Memory
Vector Hooks ChromaDB, Qdrant (more coming)
Frameworks LangGraph (Native Checkpointer), LangChain
Runtime Sync & Async (FastAPI ready)

LangGraph Integration

from memstate.integrations.langgraph import MemStateCheckpointer

checkpointer = MemStateCheckpointer(memory=mem)
app = workflow.compile(checkpointer=checkpointer)

Status

Beta. The API is stable. Suitable for production agents that require high reliability.

Read the Docs | Report an Issue


License

Apache 2.0 - see LICENSE


Contributing

Issues and PRs welcome. See CONTRIBUTING.md for details.

Metadata

Release files for memstate 0.5.1

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

Source distribution (sdist)

Source distribution for memstate 0.5.1
File Size Uploaded
memstate-0.5.1.tar.gz 728.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for memstate 0.5.1
File Interpreter ABI Platform
memstate-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 778.1 kB

Release files / memstate-0.5.1.tar.gz

Download URL memstate-0.5.1.tar.gz
Size 728.7 kB
Tags Source
SHA-256 checksum
How to use checksums
34ba1b6259a48934cdace480d8c8bf024381be11a81e8413d92ea5abc62470c2
BLAKE2b-256 checksum
How to use checksums
0c7e4a9be5a6be5c4b14b242f89cf03cb6954744ca6a35370a327f538f24c725
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / memstate-0.5.1-py3-none-any.whl

Download URL memstate-0.5.1-py3-none-any.whl
Size 49.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89e773351137589033fc80e3e435ffa17c45179d135dc7415590b2c7f54c141b
BLAKE2b-256 checksum
How to use checksums
33807adfc0a28c6cbb347ef7eabadd90a21083e9d00ef72e1afc467ce585e684
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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