Skip to main content

SQLAlchemy-based database backend patch for mem0 memory storage

Project description

mem0-history-db-patch

This patch replaces the default SQLite-only memory storage layer in the Python SDK of mem0 with a flexible, SQLAlchemy-based implementation that supports other databases — including PostgreSQL, Supabase, and more.

🔧 Why?
The official Node.js SDK for mem0 already supports robust production databases (like Postgres and Supabase) for the history store. However, the Python SDK is currently limited to SQLite — which:

  • Only allows one writer at a time (threading.Lock() is used to compensate),
  • Is not efficient or scalable for concurrent, multi-user deployments,
  • Lacks native support for migrations or backend flexibility.

This patch fixes that. (also reduces lines of code from 217l to 145l while being more robust, more readable and supporting more DB Providers)

✨ What This Patch Does

  • Replaces raw SQLite logic with SQLAlchemy-backed ORM and connection management.
  • Supports multiple SQL backends:
    • PostgreSQL
    • Supabase (PostgreSQL-compatible)
    • SQLite (still works)
    • Any other SQLAlchemy-compatible engine
  • Maintains the original class and method interfaces (SQLiteManager) for drop-in compatibility.
  • Automatically handles:
    • UUID primary key generation
    • Timestamp fields (created_at, updated_at)
    • Thread-safe session access (lock only used with SQLite)

⚙️ Installation

Install the patch:

pip install mem0-history-db-patch

This overrides mem0.memory.storage with your patched version while keeping all public interfaces unchanged.


🧪 Usage

You don't need to change how you use mem0. Just point it at a database of your choice:

from mem0.memory.storage import SQLiteManager  # Still called that for compatibility

db = SQLiteManager(db_url="postgresql://user:pass@localhost:5432/mem0")  # this db url is what you would insert into your history_db_path config in mem0

For SQLite (default, still supported):

db = SQLiteManager()  # Uses sqlite:///:memory: by default

📦 Migration Notes

At this time, the automatic migration logic is disabled.

Why?

  • If you're switching to a new backend (e.g., PostgreSQL), you're probably starting fresh.
  • Adding migrations would increase patch complexity and risk breaking assumptions.

That said:

  • If you need to preserve your SQLite data, you'll need to migrate it manually (e.g., by dumping to CSV and reloading).
  • If you're starting with a new database, just set your db_url and go.

Migration tooling may be added in the future if needed.


📌 Technical Notes

  • All history records are stored in the same schema as before.
  • id is now a UUID string (auto-generated by SQLAlchemy).
  • created_at and updated_at use database time (func.now()).
  • For SQLite, threading.Lock() is still used to prevent concurrent write issues.
  • For other backends (like Postgres), locking is bypassed for performance.

📬 Example Postgres Connection String

SQLiteManager(db_url="postgresql+psycopg2://username:password@localhost:5432/mydb")

Make sure psycopg2 is installed:

pip install psycopg2-binary

📚 Future Enhancements

  • Optional: Migration from existing SQLite DBs
  • Optional: Rename SQLiteManager internally (if mem0 adopts the patch upstream)
  • Optional: Add tests for concurrency and Postgres behavior

📝 License

MIT — do whatever you want with it, just don’t blame us if your DB gets angry.


👤 Author

Maintained by @nocyphr

Feedback, issues, and PRs welcome.

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

mem0_history_db_patch-1.0.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

mem0_history_db_patch-1.0.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file mem0_history_db_patch-1.0.3.tar.gz.

File metadata

  • Download URL: mem0_history_db_patch-1.0.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for mem0_history_db_patch-1.0.3.tar.gz
Algorithm Hash digest
SHA256 849b8ebdc306ac4184678989b16ecb3dd0b1488fc7667131f843f246f990aeb6
MD5 86e090f9a927b22b85b2dc73eefc38ea
BLAKE2b-256 14d334d5777fe07ffdb6a2d7b441768400dbe25686e3b8d4d7f19e0ac8e39c8a

See more details on using hashes here.

File details

Details for the file mem0_history_db_patch-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for mem0_history_db_patch-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 45e622b6dd477da57291666fb93bc6499872daf19af9a21592a5988fc707f949
MD5 fbb7fa125a74a4a51b5b26d7fbd02b35
BLAKE2b-256 4b55dc1755d91342a746c22db8a9ec64e7b808e733ae2e85f5cd11b618be0d87

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