Skip to main content

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

Metadata

Release files for mubit-langchain 0.6.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 mubit-langchain 0.6.0
File Size Uploaded Provenance Actions
mubit_langchain-0.6.0.tar.gz 15.6 kB Without provenance Details for mubit_langchain-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mubit-langchain 0.6.0
File Interpreter ABI Platform Actions
mubit_langchain-0.6.0-py3-none-any.whl Python 3 none any Details for mubit_langchain-0.6.0-py3-none-any.whl

Total release size: 26.9 kB

Release files / mubit_langchain-0.6.0.tar.gz

Download URL mubit_langchain-0.6.0.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2eeed1be061d514cef8e93e95c3fd67af6e0f0c079a3d1ba3ad052f4f544ba31
BLAKE2b-256 checksum
How to use checksums
c79370b87b8de57048af36f6d4b254d33ef00bd92ed12c544d72abeabd6a014a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / mubit_langchain-0.6.0-py3-none-any.whl

Download URL mubit_langchain-0.6.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c13a90c90f9f5492755265d39488b2ddad17bcf2fc792de9a823d87c5869ec20
BLAKE2b-256 checksum
How to use checksums
73c507e535149d5aac314f91535265a383d351d0c67aff763e7fc6354925977c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Security for mubit-langchain 0.6.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