LangChain Integration

SynapseChatMessageHistory implements LangChain's BaseChatMessageHistory interface. Every message is automatically encrypted with AES-256-GCM and sanitized for PII before storage.

Overview

LangChain's message history interface lets you plug in any storage backend for conversation persistence. SynapseChatMessageHistory provides a drop-in replacement that routes all messages through the Cognitive Security Pipeline before storage — PII redaction, intent classification, and AES-256-GCM encryption.

Installation

bash
pip install synapse-layer[langchain] langchain-core

Quick Start

python
import asyncio
from synapse_memory.integrations import SynapseChatMessageHistory

async def main():
    # Initialize Synapse Layer as your LangChain message history
    history = SynapseChatMessageHistory(
        agent_id="demo-agent",
        session_id="onboarding-session",
    )

    # Store conversation messages
    # Each message passes through the Cognitive Security Pipeline:
    # PII redaction → Intent validation → AES-256 encryption
    history.add_user_message("I prefer concise, technical responses.")
    history.add_ai_message("Noted — I'll keep responses brief and precise.")
    history.add_user_message("My project deadline is next Friday.")
    history.add_ai_message("I'll factor that deadline into my suggestions.")

    # Retrieve messages in LangChain-compatible format
    messages = await history.aget_messages()
    for msg in messages:
        role = "User" if msg.type == "human" else "AI"
        print(f"  [{role}] {msg.content}")

asyncio.run(main())

LCEL Integration

Use with LangChain's RunnableWithMessageHistory for automatic conversation persistence:

python
from langchain_core.runnables.history import RunnableWithMessageHistory
from synapse_memory.integrations import SynapseChatMessageHistory

# Wrap your chain with message history
chain_with_history = RunnableWithMessageHistory(
    runnable=your_chain,
    get_session_history=lambda session_id: SynapseChatMessageHistory(
        agent_id="your-agent",
        session_id=session_id,
    ),
)

# Every invocation automatically persists messages
response = chain_with_history.invoke(
    {"input": "What were my preferences?"},
    config={"configurable": {"session_id": "user-123"}},
)

Note

The session_id parameter creates isolated memory namespaces per user or conversation. Combined with agent_id, this provides multi-tenant memory isolation.

API Reference

SynapseChatMessageHistory

ParameterTypeDescription
agent_idstrUnique identifier for the agent's memory namespace
session_idstrSession identifier for conversation isolation

Methods

MethodDescription
add_user_message(content)Store a user message through the security pipeline
add_ai_message(content)Store an AI message through the security pipeline
aget_messages()Retrieve all messages (async, LangChain-compatible format)
clear()Clear all messages for this session

Source: synapse_memory/integrations/langchain_memory.py