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()
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
Metadata
Release files for langchain-encrypted-mongo-memory 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langchain_encrypted_mongo_memory-1.0.0.tar.gz | 9.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_encrypted_mongo_memory-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.1 kB
Release files / langchain_encrypted_mongo_memory-1.0.0.tar.gz
| Download URL | langchain_encrypted_mongo_memory-1.0.0.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b2822d3d8574eff3dfc129db1bb9052060115bc324c1d0fab78db8f28d3d6a8
|
|
BLAKE2b-256 checksum How to use checksums |
80dc4553e4247e06cce9254ea15e40e3ffef5fc0cad782ff77734ea83fcb4f23
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|
Release files / langchain_encrypted_mongo_memory-1.0.0-py3-none-any.whl
| Download URL | langchain_encrypted_mongo_memory-1.0.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0ea79d6bf8f8b43e571f4b9153cc6289ae1f777dbedd4b1647a12ac0a19458e3
|
|
BLAKE2b-256 checksum How to use checksums |
52063cec69facaa465fe8ae096925c634cc1d669217dd2cef23789ef7891fe8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|