Metadata-Version: 2.5
Name: langchain-memorysync
Version: 1.2.0
Summary: MemorySync integration for LangChain: chat message history, memory tools, and recall context.
Project-URL: Homepage, https://memorysync.io
Project-URL: Documentation, https://docs.memorysync.io/integrations/langchain
Project-URL: API Reference, https://docs.memorysync.io/api/overview
Project-URL: Changelog, https://docs.memorysync.io/release-notes
Project-URL: Support, https://docs.memorysync.io/debugging/support
Project-URL: Status, https://status.memorysync.io
Author: MemorySync
License: MIT
Keywords: agent-memory,ai,ai-agents,chat-history,langchain,llm,long-term-memory,memory,memorysync,rag
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: httpx<1.0,>=0.25
Requires-Dist: langchain-core<2,>=0.3
Requires-Dist: memorysync>=1.6
Provides-Extra: langgraph
Requires-Dist: langgraph<2,>=1.2; extra == 'langgraph'
Description-Content-Type: text/markdown

# langchain-memorysync

MemorySync integration for [LangChain](https://python.langchain.com): persistent
chat message history, structured memory tools for agents, and prompt-ready
recall context. Works with langchain-core 0.3.x and 1.x.

```bash
pip install langchain-memorysync
```

Set `MEMORYSYNC_API_KEY` (create a key at [memorysync.io](https://memorysync.io),
or run `npx memorysync-cli init`).

## Chat message history

`MemorySyncChatMessageHistory` implements `BaseChatMessageHistory`, so it plugs
straight into `RunnableWithMessageHistory`:

```python
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.

```python
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

```python
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

```python
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

```bash
pip install "langchain-memorysync[langgraph]"
```

```python
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](https://docs.memorysync.io/integrations/langchain)
