A secure, encrypted MongoDB chat memory for LangChain applications
Project description
LangChain-Encrypted-Mongo-Memory
A Secure, Encrypted MongoDB Chat Memory for LangChain Applications
LangChain-Encrypted-Mongo-Memory extends MongoDBChatMessageHistory to provide AES-128 encryption for all stored messages. Built on top of mores-encryption, it ensures sensitive conversation data remains protected at rest.
Perfect for securing chat histories containing PII, medical data, financial information, or any sensitive conversation data in your LangChain applications.
LangChain-Encrypted-Mongo-Memory removes the cryptographic complexity so you can focus on building — not configuring.
Features
- AES-128 Encryption — Messages encrypted using Fernet (AES-128 CBC with PKCS7 padding)
- HMAC-SHA256 Integrity — Cryptographic verification of message integrity
- URL-safe Base64 Output — Encrypted data stored in URL-safe format
- Drop-in Replacement — Compatible with LangChain's chat memory interface
- Persistent Storage — Long-term message storage in MongoDB
- Type Filtering — Supports human, AI, and system message types
- Zero-Config Encryption — Automatic key handling via mores-encryption
Installation
pip install langchain-encrypted-mongo-memory
Or install from source:
git clone https://github.com/HATAKEkakshi/langchain-encrypted-mongo-memory.git
cd langchain-encrypted-mongo-memory
pip install -e .
Setup
Generate Encryption Key
Run this command in your terminal:
python -c "from cryptography.fernet import Fernet; print('ENCRYPTION_KEY=' + Fernet.generate_key().decode())"
Save to .env
Copy the output and save it in your .env file:
ENCRYPTION_KEY=your_generated_key_here
Usage
1. Basic Usage
from langchain_encrypted_mongo_memory import EncryptedMongoDBChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
# Create encrypted history
history = EncryptedMongoDBChatMessageHistory(
connection_string="mongodb://localhost:27017",
database_name="chat_app",
collection_name="encrypted_messages",
session_id="user-123"
)
# Add messages (automatically encrypted)
history.add_message(HumanMessage(content="Hello, how are you?"))
history.add_message(AIMessage(content="I'm doing great, thank you!"))
# Retrieve messages (automatically decrypted)
messages = history.messages
for msg in messages:
print(f"{msg.type}: {msg.content}")
2. Clear Session History
# Clear history for the current session
history.clear()
3. With LangChain Memory
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
chat_memory=history,
return_messages=True
)
Why Use LangChain-Encrypted-Mongo-Memory?
Because securing chat history shouldn't be painful.
Most developers store sensitive conversation data in MongoDB without encryption — exposing PII, medical records, and confidential information to potential breaches.
LangChain-Encrypted-Mongo-Memory gives you:
- Automatic Encryption — Messages encrypted before storage
- Transparent Decryption — Seamless retrieval without extra code
- LangChain Compatible — Works with all LangChain memory patterns
- Production Ready — Built on proven cryptographic standards
- Minimal Code Changes — Drop-in replacement for MongoDBChatMessageHistory
Configuration
Environment Variables
| Variable | Description | Required | Default |
|---|---|---|---|
ENCRYPTION_KEY |
Base64-encoded 32-byte Fernet key | No (Auto-generated if missing) | N/A |
MongoDB Connection URLs
# Local MongoDB
connection_string="mongodb://localhost:27017"
# With authentication
connection_string="mongodb://user:password@localhost:27017"
# MongoDB Atlas
connection_string="mongodb+srv://user:password@cluster.mongodb.net"
# With replica set
connection_string="mongodb://host1:27017,host2:27017/?replicaSet=rs0"
API Reference
EncryptedMongoDBChatMessageHistory
EncryptedMongoDBChatMessageHistory(
connection_string: str,
database_name: str,
collection_name: str,
session_id: str
)
| Parameter | Type | Description |
|---|---|---|
connection_string |
str |
MongoDB connection URL |
database_name |
str |
Name of the database |
collection_name |
str |
Name of the collection |
session_id |
str |
Unique identifier for the chat session |
Properties
| Property | Type | Description |
|---|---|---|
messages |
List[BaseMessage] |
List of decrypted messages |
Methods
| Method | Description |
|---|---|
add_message(message) |
Encrypt and store a message |
add_user_message(content) |
Add a human message |
add_ai_message(content) |
Add an AI message |
clear() |
Delete all messages for this session |
Security Implementation Details
- Encryption:
cryptography.fernet.Fernet(AES-128 CBC with PKCS7 padding, HMAC-SHA256 for integrity) - Key Management: Automatic loading from
ENCRYPTION_KEYenvironment variable - Encoding: All outputs are URL-safe Base64 encoded strings
- Library: Built on mores-encryption for proven security
Development
Setup
git clone https://github.com/HATAKEkakshi/langchain-encrypted-mongo-memory.git
cd langchain-encrypted-mongo-memory
python -m venv venv
source venv/bin/activate
pip install -e ".[dev]"
Running Tests
# Run all tests
pytest
# With verbose output
pytest -v
# With coverage
pytest --cov=langchain_encrypted_mongo_memory
Documentation & Source Code
- GitHub: https://github.com/HATAKEkakshi/langchain-encrypted-mongo-memory
- PyPI: https://pypi.org/project/langchain-encrypted-mongo-memory/
- mores-encryption: https://pypi.org/project/mores-encryption/
License
MIT License — see LICENSE for details.
Author
Hemant Kumar — GitHub
Acknowledgments
- LangChain for the excellent LLM framework
- mores-encryption for encryption utilities
- cryptography for secure encryption primitives
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 langchain_encrypted_mongo_memory-0.1.0.tar.gz.
File metadata
- Download URL: langchain_encrypted_mongo_memory-0.1.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3985f8bc83c9919aa76f6781109d9bf221c751a97e71b1d93657313aac6e82c1
|
|
| MD5 |
72073c102e079f88e933f62af11c0fe3
|
|
| BLAKE2b-256 |
f8f1d9aa59a399f84d800f8891b8a5b40f3bb9703ef4c382afe380218ea953f0
|
File details
Details for the file langchain_encrypted_mongo_memory-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_encrypted_mongo_memory-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0119ca148b9ab607ae74666ca4e493eddee045b6009de8f75bb28382281b636
|
|
| MD5 |
1ed3448cb18bc7017dc29727e71ef6c6
|
|
| BLAKE2b-256 |
4b01823d1f876d9c96c423cec9f55ba673aa3e4def092478dec5b5abc5287ffb
|