Skip to main content

ACID-like memory for Agents. Atomically syncs Pydantic models & Vector DBs with Git-style versioning.

Project description

🧠 MemState

PyPI version Python versions License Tests

ACID-like Memory for AI Agents. Atomically syncs Pydantic models & Vector DBs with Git-style versioning.


⚡ Why MemState?

Building agents is easy. Keeping their state consistent is hard.

Most memory systems are just wrappers around Vector DBs or JSON files. This leads to Data Drift:

  • Split Brain: The agent updates a User Profile in SQL, but the Vector DB still holds the old embedding. Result: Hallucinations.
  • Dirty Reads: A complex chain fails halfway, leaving the memory in a broken, partial state.
  • No Undo: If an agent overwrites critical context, you can't hit "Ctrl+Z".

MemState brings database semantics to Agent Memory:

  • ⚛️ Atomicity: Changes to Structured Data (SQL) and Semantic Data (Vectors) happen together or not at all.
  • 🛡️ Isolation: Changes in a session are invisible to the main memory until committed.
  • ⏪ Time Travel: Built-in history log allows you to rollback an agent's state to any point in time.

✨ Key Features

  • ⚡ Real-time RAG Sync: Automatically upserts/deletes vectors in ChromaDB when you commit facts. No more manual sync logic.
  • 🛡️ Type-Safe: Uses Pydantic schemas. If an agent tries to save a string into an age: int field, it fails before corruption happens.
  • ⏪ Git-like Versioning: Every change is a transaction. rollback(steps=1) undoes the last action instantly.
  • 🔒 Constraints: Enforce logic like "One User Profile per Email" (Singleton). No more duplicate profiles.
  • 🔌 Pluggable Backends: SQLite, Redis, In-Memory.

🏗 Architecture

flowchart LR
    User[User] -->|Input| Agent[AI Agent]
    Agent -->|1. Commit Fact| Mem{MemState}

    subgraph Core [ACID Layer]
        direction TB
        Schema[🛡️ Pydantic Schema]
        Log[📝 Tx Log]
    end

    Mem --> Schema --> Log

    Log -->|2. Atomic Write| DB[(SQLite/Redis)]
    Log -.->|3. Auto-Sync| Vec[(ChromaDB)]

🚀 Quick Start

Installation

pip install memstate

For ChromaDB support (RAG Sync):

pip install memstate[chromadb]

For LangGraph support:

pip install memstate[langgraph]

For Redis support:

pip install memstate[redis]

1. Basic Usage (Structured Memory)

from memstate.storage import MemoryStore, Constraint
from memstate.schemas import Fact
from memstate.backends.sqlite import SQLiteStorage
from pydantic import BaseModel

# 1. Define schema
class UserProfile(BaseModel):
    username: str
    level: int = 1

# 2. Initialize Storage
storage = SQLiteStorage("agent_brain.db")
memory = MemoryStore(storage)
memory.register_schema("user", UserProfile, Constraint(singleton_key="username"))

# 3. Commit a Fact (Transactional)
user_fid = memory.commit(
    Fact(type="user", payload={"username": "neo", "level": 99}),
    actor="Agent_Smith"
)

# 4. Rollback (Time Travel)
memory.update(fact_id=user_fid, patch={"payload": {"level": 0}})  # Mistake
memory.rollback(1)  # Level is back to 99

2. Automatic RAG Sync (ChromaDB)

MemState ensures your Vector DB always matches your Structured DB.

import chromadb
from memstate.integrations.chroma import ChromaSyncHook

# 1. Setup Chroma Client
chroma_client = chromadb.Client()

# 2. Create the Hook
# This tells MemState: "When data changes, put 'content' field into Chroma"
rag_hook = ChromaSyncHook(
    client=chroma_client,
    collection_name="agent_memory",
    text_field="content",             # Field to embed
    metadata_fields=["role", "topic"] # Fields to filter by
)

# 3. Attach to Memory
memory = MemoryStore(storage)
memory.add_hook(hook=rag_hook)

# 4. That's it!
# When you commit, it automatically upserts to Chroma.
memory.commit(
    Fact(type="memory", payload={"content": "The sky is blue", "topic": "nature"})
)

# Verify in Chroma
coll = chroma_client.get_collection("agent_memory")
print(coll.get()['documents'])  # ['The sky is blue']

3. Using with LangGraph

MemState includes a native checkpointer that persists your agent's graph state.

from memstate.integrations.langgraph import MemStateCheckpointer

checkpointer = MemStateCheckpointer(memory=memory)
# Compile your graph
app = workflow.compile(checkpointer=checkpointer)

💡 Use Cases

1. Hybrid Search (The "Holy Grail")

Problem: User asks "Who is the admin?". Vector search fails because "admin" isn't semantically close to a name. Solution: MemState keeps structured data structured.

  • Use SQL for precise queries: memory.query(role="admin").
  • Use Vector for fuzzy queries: chroma.query("What do we know about cats?").
  • Both are always in sync.

2. Financial & Legal Bots (Compliance)

Problem: An LLM hallucinates a loan interest rate. Solution: Use Immutable constraints for signed contracts. Use Transaction Logs to audit exactly when and why a fact was changed.

3. RPGs & Interactive Fiction

Problem: The player picked up a key, then died. The agent forgets the key is gone. Solution: Use rollback() to reset the world state to the last checkpoint perfectly.


📂 Demos

Check the examples/ folder:


🛠 Status

Alpha. Ready for local development.

Supports: InMemoryStorage, RedisStorage, SQLiteStorage, ChromaDB.


📄 License

Licensed under the Apache 2.0 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.3.0.tar.gz (213.5 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.3.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: memstate-0.3.0.tar.gz
  • Upload date:
  • Size: 213.5 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.3.0.tar.gz
Algorithm Hash digest
SHA256 628ebe6860456808172be735f5d6a0a00060e6705af6df419d432b7066675d3a
MD5 95b494ee4eef35edcdd78861166f6bb5
BLAKE2b-256 430f499a888e847704c75bbf91dd19cd6d7efe1586b2a8e6a1b82f2b11450459

See more details on using hashes here.

File details

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

File metadata

  • Download URL: memstate-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68d7e2aa5c021cfb4eab0f29b42c8793bf09f9cab08b8a188ef692d4077cb46a
MD5 8453e00e0fd564f62408e6f6c7e2e6a7
BLAKE2b-256 0b25523ab7e97c0d06bf66d5de2dae431786a7328db4d70b9ef8600441d0d402

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