Skip to main content

langchain-memorysync

MemorySync integration for LangChain: persistent chat message history, structured memory tools for agents, and prompt-ready recall context. Works with langchain-core 0.3.x and 1.x.

pip install langchain-memorysync

Set MEMORYSYNC_API_KEY (create a key at memorysync.io, or run npx memorysync-cli init).

Chat message history

MemorySyncChatMessageHistory implements BaseChatMessageHistory, so it plugs straight into RunnableWithMessageHistory:

from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_memorysync import MemorySyncChatMessageHistory

chain_with_history = RunnableWithMessageHistory(
    chain,
    lambda session_id: MemorySyncChatMessageHistory(session_id=session_id),
    input_messages_key="input",
    history_messages_key="history",
)

chain_with_history.invoke(
    {"input": "My name is Ada."},
    config={"configurable": {"session_id": "thread-42"}},
)

The transcript is kept in MemorySync's conversation history, separate from the user's memories. Every message is stored as written, short turns like "yes" included, together with the full LangChain message (tool calls, additional kwargs) so it is rebuilt exactly. User messages are also sent to fact extraction: the durable facts in them become memories that recall and the agent tools can find. Assistant, system and tool messages are not extracted, and the transcript itself never shows up as memories.

Each message carries its position in the session, so a replayed write is recognised instead of duplicated and the transcript reads back in order. clear() deletes the session's transcript; facts already extracted from it stay in the user's memory.

history = MemorySyncChatMessageHistory(
    session_id="thread-42",
    user_id="customer-7",   # share memory across a user's sessions
    max_messages=30,        # cap the transcript tail handed to the LLM
)

Agent tools

from langchain.agents import create_agent
from langchain_memorysync import create_memorysync_tools

tools = create_memorysync_tools(end_user_id="customer-7")
agent = create_agent(model, tools=tools)

Returns add_memory, search_memory, list_memories, update_memory, and delete_memory. Tools return readable strings and never raise, so a memory failure cannot abort an agent run. Pass read_only=True to hand an agent only search_memory and list_memories.

add_memory derives an idempotency key from the content — an agent that repeats itself gets "already stored", not a duplicate.

Recall context

from langchain_memorysync import MemorySyncContextProvider

provider = MemorySyncContextProvider(user_id="customer-7")
context = provider.get_context("What should I cook tonight?")

Returns a grouped, type-labelled context block built by MemorySync's hierarchical retrieval, ready to inject into a system prompt. Empty string — not an exception — when the user has no relevant memories yet.

LangGraph store

pip install "langchain-memorysync[langgraph]"
from langchain_memorysync.langgraph import MemorySyncStore

store = MemorySyncStore()
graph = builder.compile(store=store)

store.put(("memories", "user-1"), "diet", {"text": "vegetarian"})
store.search(("memories", "user-1"), query="what do they eat?")

MemorySyncStore is a LangGraph BaseStore for cross-thread state. Values live in MemorySync's framework state store, not among the user's memories. Each namespace is isolated server-side, a key holds one live value (a put replaces it, and repeating a put changes nothing), and query search works without configuring an embedding index. Pass index=False to store a value without embedding it. search with a namespace prefix spans every child namespace a MemorySyncStore has written to (up to 25 per search), and list_namespaces supports prefix, suffix and max_depth.

The same module exports create_memorysync_pre_model_hook (recall context for create_react_agent(pre_model_hook=...)), MemorySyncMemoryMiddleware (recall context for langchain.agents.create_agent; needs the langchain package), create_persist_turn_node and create_memory_search_tool. The persist node, and the middleware with auto_persist=True, send the latest user message for fact extraction: only the durable facts in it become memories, tagged with the session id. Assistant replies are not sent and are not stored as memories, and a re-run node or retried model call is recognised, so nothing is extracted twice.

Documentation

Full guide: docs.memorysync.io/integrations/langchain

Metadata

Release files for langchain-memorysync 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-memorysync 1.2.0
File Size Uploaded Provenance
langchain_memorysync-1.2.0.tar.gz 27.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-memorysync 1.2.0
File Interpreter ABI Platform
langchain_memorysync-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 62.0 kB

Release files / langchain_memorysync-1.2.0.tar.gz

Download URL langchain_memorysync-1.2.0.tar.gz
Size 27.2 kB
Tags Source
SHA-256 checksum
How to use checksums
52ea3e8847a677d9fa29e9fc7a17bf923d6a1fb9ae6a53be3b949e63ddf8481a
BLAKE2b-256 checksum
How to use checksums
836a6d885e4033f4400a716b2c1f81fd6dd18cc6403fd13a33c26b47ebe16862
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.5

Release files / langchain_memorysync-1.2.0-py3-none-any.whl

Download URL langchain_memorysync-1.2.0-py3-none-any.whl
Size 34.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
268cc34ec61960d5c324c3d2822e0d0843061b6e7ef6f6f8c0f89cdfdbaabe09
BLAKE2b-256 checksum
How to use checksums
b0a9ca8284f9d44c575b68c588ca2a10c4f8af5332170966761dd94f2d0e470e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.5

Security for langchain-memorysync 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page