Skip to main content

Transactional Memory for AI Agents - Keep SQL and Vector DBs in sync with ACID-like guarantees

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

memstate-0.5.0.tar.gz (726.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

memstate-0.5.0-py3-none-any.whl (49.1 kB view details)

Uploaded Python 3

File details

Details for the file memstate-0.5.0.tar.gz.

File metadata

  • Download URL: memstate-0.5.0.tar.gz
  • Upload date:
  • Size: 726.3 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for memstate-0.5.0.tar.gz
Algorithm Hash digest
SHA256 49852e6a009c95e4055209c19714f4741b4cf79464b5d2882b1659224d7f1b42
MD5 9b3cd84bc45d1331756b745089aa994d
BLAKE2b-256 79ddc8b608264ab0dd7007fb89563beacdf2b019c4452d4cc9cbbb18dfaa18ab

See more details on using hashes here.

File details

Details for the file memstate-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: memstate-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 49.1 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for memstate-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3cfc0c89aad383282a67cd7ca0000c26c7bd3b24b540f4a594fea4de3c93c523
MD5 d1934d3dad1c7a0bb350cc4bce79f1b0
BLAKE2b-256 26f128991b32784eae7367910240cc814a865468320f8d78bd1a37a0f06d7f47

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page