ACID-like memory for Agents. Atomically syncs Pydantic models & Vector DBs with Git-style versioning.
Project description
🧠 MemState
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
Pydanticschemas. If an agent tries to save a string into anage: intfield, 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 import MemoryStore, Constraint, Fact, 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:
- examples/main_demo.py - Full tour: Schemas, Singletons, Hallucination Correction via Rollback.
- examples/rag_hook_demo.py - Shows how to use MemState as the "Master DB" that automatically syncs text to a mock Vector DB for RAG.
- examples/langgraph_checkpoint_demo.py - Shows how to plug
MemStateCheckpointerinto a LangGraph workflow. - examples/pizza_agent_demo.py - A "Pizza Ordering" agent that separates Chat History from Business State (the JSON order).
🛠 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file memstate-0.3.1.tar.gz.
File metadata
- Download URL: memstate-0.3.1.tar.gz
- Upload date:
- Size: 213.9 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3da01f4c9c3b4ec0efbefb371bde10c9651e9c3b9fdb2fa54c54cfba23abf7fe
|
|
| MD5 |
db055f68d307d505cd7c12bb401f033b
|
|
| BLAKE2b-256 |
b17276bff757558a79c90082560801fe9753c3388be9b70e7983b27105381d3f
|
File details
Details for the file memstate-0.3.1-py3-none-any.whl.
File metadata
- Download URL: memstate-0.3.1-py3-none-any.whl
- Upload date:
- Size: 20.3 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40aa48f6fb56b07b913a6622956b58fa005b1bf83bbe0f0185b1345efd3dc8f9
|
|
| MD5 |
e782ab2eaa71c4529b3bae3d8ef4f1d8
|
|
| BLAKE2b-256 |
fe7c062832d2aefa818c6bffb424e9f314e62edbda1be910c1c218d44da8899f
|