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.1.tar.gz (728.7 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.1-py3-none-any.whl (49.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: memstate-0.5.1.tar.gz
  • Upload date:
  • Size: 728.7 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.1.tar.gz
Algorithm Hash digest
SHA256 34ba1b6259a48934cdace480d8c8bf024381be11a81e8413d92ea5abc62470c2
MD5 94397a32c64ff93917edf0217b605275
BLAKE2b-256 0c7e4a9be5a6be5c4b14b242f89cf03cb6954744ca6a35370a327f538f24c725

See more details on using hashes here.

File details

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

File metadata

  • Download URL: memstate-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 49.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 89e773351137589033fc80e3e435ffa17c45179d135dc7415590b2c7f54c141b
MD5 7d9e73d692b8a7aa912780e5ee4d78f0
BLAKE2b-256 33807adfc0a28c6cbb347ef7eabadd90a21083e9d00ef72e1afc467ce585e684

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