Metadata-Version: 2.5
Name: langchain-memorysync
Version: 1.0.1
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
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"}},
)
```

Turns are stored verbatim through MemorySync's episodic ingestion — no
extraction gates, so short turns like "yes" survive — and the full LangChain
message (tool calls, additional kwargs) is serialised into metadata for exact
reconstruction. Writes are idempotent: a retried write is recognised, never
duplicated.

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

## Documentation

Full guide: [docs.memorysync.io/integrations/langchain](https://docs.memorysync.io/integrations/langchain)
