Skip to main content

Encrypted Redis chat message history for LangChain with AES-128 encryption via mores-encryption.

Project description

LangChain-Encrypted-Redis-Memory

Python PyPI License: MIT

A Secure, Encrypted Redis Chat Memory for LangChain Applications

LangChain-Encrypted-Redis-Memory extends RedisChatMessageHistory 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-Redis-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
  • TTL Support — Optional time-to-live for automatic message expiration
  • Type Filtering — Supports human, AI, and system message types
  • Zero-Config Encryption — Automatic key handling via mores-encryption

Installation

pip install langchain-encrypted-redis-memory

Or install from source:

git clone https://github.com/HATAKEkakshi/langchain-encrypted-redis-memory.git
cd langchain-encrypted-redis-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_redis_memory import EncryptedRedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage

# Create encrypted history
history = EncryptedRedisChatMessageHistory(
    session_id="user-123",
    url="redis://localhost:6379"
)

# 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. With TTL (Auto-expiry)

# Messages expire after 1 hour
history = EncryptedRedisChatMessageHistory(
    session_id="user-123",
    url="redis://localhost:6379",
    ttl=3600  # seconds
)

3. With LangChain ConversationBufferMemory

from langchain.memory import ConversationBufferMemory
from langchain_encrypted_redis_memory import EncryptedRedisChatMessageHistory

history = EncryptedRedisChatMessageHistory(
    session_id="user-123",
    url="redis://localhost:6379"
)

memory = ConversationBufferMemory(
    chat_memory=history,
    return_messages=True
)

4. Reset Session History

# Clear history for a specific session
EncryptedRedisChatMessageHistory.reset_session_history(
    url="redis://localhost:6379",
    session_id="user-123"
)

Why Use LangChain-Encrypted-Redis-Memory?

Because securing chat history shouldn't be painful.

Most developers store sensitive conversation data in Redis without encryption — exposing PII, medical records, and confidential information to potential breaches.

LangChain-Encrypted-Redis-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 RedisChatMessageHistory

Configuration

Environment Variables

Variable Description Required Default
ENCRYPTION_KEY Base64-encoded 32-byte Fernet key No (Auto-generated if missing) N/A

Redis Connection URLs

# Local Redis
url="redis://localhost:6379"

# With password
url="redis://:password@localhost:6379"

# With database selection
url="redis://localhost:6379/1"

# TLS connection
url="rediss://localhost:6379"

Key Generation

Run these commands to generate secure values for your .env file:

Generate Encryption Key:

python -c "from cryptography.fernet import Fernet; print('ENCRYPTION_KEY=' + Fernet.generate_key().decode())"

API Reference

EncryptedRedisChatMessageHistory

EncryptedRedisChatMessageHistory(
    session_id: str,
    url: str,
    ttl: Optional[int] = None,
    key_prefix: str = "message_store:"
)
Parameter Type Description
session_id str Unique identifier for the chat session
url str Redis connection URL
ttl int Optional TTL in seconds for message expiration
key_prefix str Prefix for Redis keys (default: message_store:)

Properties

Property Type Description
messages List[BaseMessage] List of decrypted messages

Methods

Method Description
add_message(message) Encrypt and store a message
clear() Delete all messages for this session
reset_session_history(url, session_id) Static method to clear a session

Security Implementation Details

  • Encryption: cryptography.fernet.Fernet (AES-128 CBC with PKCS7 padding, HMAC-SHA256 for integrity)
  • Key Management: Automatic loading from ENCRYPTION_KEY environment 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-redis-memory.git
cd langchain-encrypted-redis-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_redis_memory

# Skip integration tests
pytest -m "not integration"

Code Quality

# Format code
black langchain_encrypted_redis_memory tests

# Lint
flake8 langchain_encrypted_redis_memory tests

# Type check
mypy langchain_encrypted_redis_memory

Documentation & Source Code


License

MIT License — see LICENSE for details.


Author

Hemant KumarGitHub


Acknowledgments

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

langchain_encrypted_redis_memory-1.0.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file langchain_encrypted_redis_memory-1.0.0.tar.gz.

File metadata

File hashes

Hashes for langchain_encrypted_redis_memory-1.0.0.tar.gz
Algorithm Hash digest
SHA256 56475607740d257839035c0c168fb8147db4caf38bed7465eb096a96e40d4e84
MD5 5a98d4997573b090f027b2afdd867ca2
BLAKE2b-256 ebe00e5fca8029694e4f8480e927729125c6be2f78802b5475740d6b4206b470

See more details on using hashes here.

File details

Details for the file langchain_encrypted_redis_memory-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_encrypted_redis_memory-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c41c1c97e7e332d90e86d9bcbaf5fdb7ae96bea89ab7f8f1a558b7c29086fe54
MD5 ead692a6a6f9b2c2e46041c279c6a2d0
BLAKE2b-256 8b9d2ecd2b992e76fefd912064ec391a4529b0f0d3ef92f34a219fda4c9eb224

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