One-line, persistent, temporal memory for LangChain — powered by OpenMemory.
from langchain_openmemory import Memory
m = Memory() # zero friction, no user id needed!That’s it. Memory works as:
- a retriever
- a chat history backend
- a LCEL Runnable that injects rich context
- a persistent long-term memory across sessions
All backed by OpenMemory: local-first, temporal, explainable memory for AI agents.
- 🧠 One-line API —
Memory()is all you need - 🪢 LangChain-native — works as a
Runnable, retriever, and chat history - 🕒 Temporal memory — recall state across time, not just similar text
- 📚 Multi-chat context — memory persists over many conversations
- 💾 Local-first — backed by OpenMemory’s SQLite / engine, no vector DB required
- 🔍 Explainable (via OpenMemory metadata) — you can inspect what was recalled and why
pip install openmemory-py langchain-core langchain-openmemoryRequires Python 3.9+.
from langchain_openmemory import Memory
memory = Memory() # optional: Memory("user123")from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openmemory import Memory
memory = Memory()
prompt = ChatPromptTemplate.from_template(
"You are a helpful assistant."
"Here is what you remember: {context}"
"User: {question}"
)
llm = ChatOpenAI()
chain = (
{"context": memory, "question": RunnablePassthrough()}
| prompt
| llm
)
print(chain.invoke("Remember that I like dark themes and short answers."))
print(chain.invoke("What did I say about themes?"))print(memory("what does the user prefer?"))memory.store("user123 loves Minecraft and Pterodactyl panels.")Internally, Memory:
- Uses the Python
openmemoryclient in local mode by default. - Stores chat messages and facts into OpenMemory.
- Retrieves relevant memories with temporal + sector-aware ranking.
- Exposes a LangChain-compatible
Runnablethat returns a context block. - Provides an internal retriever and chat history implementation.
You get:
- real long-term memory
- across many sessions
- with minimal boilerplate
from langchain_openai import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain_openmemory import Memory
memory = Memory()
retriever = memory.retriever
llm = ChatOpenAI()
qa = ConversationalRetrievalChain.from_llm(
llm,
retriever=retriever,
return_source_documents=True,
)
res = qa.invoke({"question": "What does this user like?"})
print(res["answer"])from langchain_core.runnables import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_openmemory import Memory
memory = Memory()
prompt = ChatPromptTemplate.from_messages(
[("system", "You are a helpful assistant."),
("human", "{input}")]
)
llm = ChatOpenAI()
base_chain = prompt | llm
def get_history(session_id: str):
return memory.history
chain = RunnableWithMessageHistory(
base_chain,
get_history,
input_messages_key="input",
history_messages_key="history",
)
print(chain.invoke({"input": "Remember that I live in Hyderabad."}, config={"configurable": {"session_id": "s1"}}))
print(chain.invoke({"input": "Where do I live?"}, config={"configurable": {"session_id": "s1"}}))See the examples/ folder for:
chatbot.py— simple chatbot with persistent memoryagent.py— agent-style usageretrieval.py— manual recall demo
- Better temporal filters
- First-class LangChain docs integration
- Benchmarks vs vector DB + Redis memory
MIT — see LICENSE.