LangChain chat-history adapter backed by MuBit memory engine
Project description
mubit-langchain
LangChain chat-history adapter backed by MuBit.
The primary surface is MubitChatMessageHistory, a current-contract
langchain_core.chat_history.BaseChatMessageHistory implementation that persists
a conversation through MuBit's control plane. MubitLessonRetriever surfaces
MuBit's accumulated lessons as Document objects, and MubitCallbackHandler
closes the v0.7.0 reinforcement loop by crediting the recalled lessons.
Targets langchain-core>=1.2,<2 (verified against 1.4.x).
Install
pip install mubit-langchain[langchain]
Chat history with RunnableWithMessageHistory
MubitChatMessageHistory implements the current contract — add_messages /
aadd_messages, the messages property / aget_messages, and clear /
aclear. Wire it into RunnableWithMessageHistory via get_session_history:
from functools import partial
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from mubit_langchain import get_session_history
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
MessagesPlaceholder(variable_name="history"),
("human", "{input}"),
])
runnable = prompt | ChatOpenAI(model="gpt-4o-mini")
chain = RunnableWithMessageHistory(
runnable,
# Bind connection details; the factory receives the session_id per call.
partial(get_session_history, api_key="mbt_...", endpoint="http://127.0.0.1:3000"),
input_messages_key="input",
history_messages_key="history",
)
chain.invoke(
{"input": "Remember my name is Ada."},
config={"configurable": {"session_id": "user-42"}},
)
Each turn is written with remember and replayed with recall, so history
spans the whole conversation (and earlier sessions sharing the run) without a
separate checkpointer.
Direct usage
from mubit_langchain import MubitChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
history = MubitChatMessageHistory(session_id="chat-1", api_key="mbt_...")
history.add_messages([HumanMessage(content="hi"), AIMessage(content="hello!")])
msgs = history.messages # sync; chronological list[BaseMessage]
msgs = await history.aget_messages() # async
history.clear()
BaseChatMessageHistory interface
| Method | MuBit mapping |
|---|---|
add_messages(messages) / aadd_messages |
remember — one entry per message |
messages (property) / aget_messages() |
recall — replayed in write order |
clear() / aclear() |
delete_run |
Lessons + reinforcement loop (v0.7.0)
MubitLessonRetriever pulls recurring success/failure patterns into a chain's
context, and MubitCallbackHandler credits exactly those lessons when the chain
finishes — closing the recall → act → credit attribution loop.
from mubit_langchain import MubitLessonRetriever, MubitCallbackHandler
retriever = MubitLessonRetriever(api_key="mbt_...", session_id="s1")
handler = MubitCallbackHandler(api_key="mbt_...", session_id="s1", retriever=retriever)
# Lessons surfaced by the retriever are credited automatically at chain end.
docs = retriever.get_relevant_documents("deploy strategy") # sync
docs = await retriever.aget_relevant_documents("deploy strategy") # truly async
result = chain.invoke({"input": ...}, config={"callbacks": [handler]})
# Manual attribution: credit the entries that grounded an answer.
from mubit_langchain import MubitControlClient, extract_entry_ids, extract_citations
client = MubitControlClient(api_key="mbt_...")
recall = client.recall(session_id="s1", query="deploy strategy")
handler.track_recall(recall, cited_only=True) # capture grounding entries
handler.record_outcome("success", verified_in_production=True)
handler.record_step_outcome("plan", outcome="success", signal=0.7)
entry_ids = extract_entry_ids(recall) # reference_ids of recalled evidence
citations = extract_citations(recall) # 0-based grounding indices
MubitCallbackHandler exposes record_outcome(entry_ids=..., verified_in_production=...)
and record_step_outcome(...) on its public surface, in addition to the
on_chain_end / on_chain_error / on_retriever_end callback hooks.
Legacy BaseMemory shim (deprecated)
MubitMemory and MubitChatMemory target the old
langchain_core.memory.BaseMemory, which was deprecated in 0.3.1 and removed
in langchain-core 1.0+. They are retained only for backwards compatibility,
behind a pydantic import fallback so the package always imports. Do not use
them for new code — use MubitChatMessageHistory above.
Config
| Parameter | Default | Purpose |
|---|---|---|
endpoint |
http://127.0.0.1:3000 |
MuBit HTTP endpoint |
api_key |
"" |
MuBit API key |
session_id |
"default" |
Session/run scope (run_id) |
user_id |
"" |
Optional user scope |
agent_id |
"langchain" |
Agent attribution |
recall_query |
"conversation history" |
Query used to replay history |
limit |
50 |
Max messages fetched per read |
sdk_client |
None |
Inject a pre-built mubit.Client (tests) |
Development
cd integrations/python/mubit_langchain
python3 -m pytest tests/test_memory.py -v
License
Apache-2.0
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mubit_langchain-0.6.0.tar.gz.
File metadata
- Download URL: mubit_langchain-0.6.0.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2eeed1be061d514cef8e93e95c3fd67af6e0f0c079a3d1ba3ad052f4f544ba31
|
|
| MD5 |
d1a68e8ebdfae0a82a6f87fad6063f5e
|
|
| BLAKE2b-256 |
c79370b87b8de57048af36f6d4b254d33ef00bd92ed12c544d72abeabd6a014a
|
File details
Details for the file mubit_langchain-0.6.0-py3-none-any.whl.
File metadata
- Download URL: mubit_langchain-0.6.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c13a90c90f9f5492755265d39488b2ddad17bcf2fc792de9a823d87c5869ec20
|
|
| MD5 |
8e17f6c684a7444e0e016cd4e4404d5e
|
|
| BLAKE2b-256 |
73c507e535149d5aac314f91535265a383d351d0c67aff763e7fc6354925977c
|