RunnableWithMessageHistory Class — langchain Architecture
Architecture documentation for the RunnableWithMessageHistory class in history.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD f9cb8198_4568_d401_f003_82c579cfa18f["RunnableWithMessageHistory"] 9a777f9f_fc96_a4c7_ebd5_632b655b53ae["RunnableBindingBase"] f9cb8198_4568_d401_f003_82c579cfa18f -->|extends| 9a777f9f_fc96_a4c7_ebd5_632b655b53ae abb7c122_ee7b_4c8f_ffaa_3d3d63c4fab7["BaseMessage"] f9cb8198_4568_d401_f003_82c579cfa18f -->|extends| abb7c122_ee7b_4c8f_ffaa_3d3d63c4fab7 937cdd40_d89c_5be8_355f_bff74b5220b9["history.py"] f9cb8198_4568_d401_f003_82c579cfa18f -->|defined in| 937cdd40_d89c_5be8_355f_bff74b5220b9 9880a1b1_9b78_5789_4483_510fb1ad8de7["__init__()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| 9880a1b1_9b78_5789_4483_510fb1ad8de7 57489ad1_cda1_fe07_3f0a_4ac4d4f8d048["config_specs()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| 57489ad1_cda1_fe07_3f0a_4ac4d4f8d048 d418c33a_f24b_8b71_a582_4f71d916cb0f["get_input_schema()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| d418c33a_f24b_8b71_a582_4f71d916cb0f b0279fd7_ea2e_57b1_1602_d9117c49e067["OutputType()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| b0279fd7_ea2e_57b1_1602_d9117c49e067 deb936ea_7f0b_1e11_2a99_45dbfcf6b283["get_output_schema()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| deb936ea_7f0b_1e11_2a99_45dbfcf6b283 1978b1f4_7f00_d1ef_ed53_b27b8e0a8df8["_get_input_messages()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| 1978b1f4_7f00_d1ef_ed53_b27b8e0a8df8 293dcfbd_674c_d56d_4708_39019a6881d5["_get_output_messages()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| 293dcfbd_674c_d56d_4708_39019a6881d5 5e360526_b3e5_8866_7151_d23f423330a1["_enter_history()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| 5e360526_b3e5_8866_7151_d23f423330a1 99a194bc_c69c_05f1_1e52_0e0bce187e74["_aenter_history()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| 99a194bc_c69c_05f1_1e52_0e0bce187e74 9297f3bd_05a3_097d_3fa4_b9184b373e13["_exit_history()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| 9297f3bd_05a3_097d_3fa4_b9184b373e13 c39acc04_5c83_fe40_f25c_83d4affe2c5d["_aexit_history()"] f9cb8198_4568_d401_f003_82c579cfa18f -->|method| c39acc04_5c83_fe40_f25c_83d4affe2c5d
Relationship Graph
Source Code
libs/core/langchain_core/runnables/history.py lines 38–616
class RunnableWithMessageHistory(RunnableBindingBase): # type: ignore[no-redef]
"""`Runnable` that manages chat message history for another `Runnable`.
A chat message history is a sequence of messages that represent a conversation.
`RunnableWithMessageHistory` wraps another `Runnable` and manages the chat message
history for it; it is responsible for reading and updating the chat message
history.
The formats supported for the inputs and outputs of the wrapped `Runnable`
are described below.
`RunnableWithMessageHistory` must always be called with a config that contains
the appropriate parameters for the chat message history factory.
By default, the `Runnable` is expected to take a single configuration parameter
called `session_id` which is a string. This parameter is used to create a new
or look up an existing chat message history that matches the given `session_id`.
In this case, the invocation would look like this:
`with_history.invoke(..., config={"configurable": {"session_id": "bar"}})`
; e.g., `{"configurable": {"session_id": "<SESSION_ID>"}}`.
The configuration can be customized by passing in a list of
`ConfigurableFieldSpec` objects to the `history_factory_config` parameter (see
example below).
In the examples, we will use a chat message history with an in-memory
implementation to make it easy to experiment and see the results.
For production use cases, you will want to use a persistent implementation
of chat message history, such as `RedisChatMessageHistory`.
Example: Chat message history with an in-memory implementation for testing.
```python
from operator import itemgetter
from langchain_openai.chat_models import ChatOpenAI
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.documents import Document
from langchain_core.messages import BaseMessage, AIMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from pydantic import BaseModel, Field
from langchain_core.runnables import (
RunnableLambda,
ConfigurableFieldSpec,
RunnablePassthrough,
)
from langchain_core.runnables.history import RunnableWithMessageHistory
class InMemoryHistory(BaseChatMessageHistory, BaseModel):
\"\"\"In memory implementation of chat message history.\"\"\"
messages: list[BaseMessage] = Field(default_factory=list)
def add_messages(self, messages: list[BaseMessage]) -> None:
\"\"\"Add a list of messages to the store\"\"\"
self.messages.extend(messages)
def clear(self) -> None:
self.messages = []
# Here we use a global variable to store the chat message history.
# This will make it easier to inspect it to see the underlying results.
store = {}
def get_by_session_id(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = InMemoryHistory()
return store[session_id]
history = get_by_session_id("1")
history.add_message(AIMessage(content="hello"))
print(store) # noqa: T201
```
Extends
Source
Frequently Asked Questions
What is the RunnableWithMessageHistory class?
RunnableWithMessageHistory is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/history.py.
Where is RunnableWithMessageHistory defined?
RunnableWithMessageHistory is defined in libs/core/langchain_core/runnables/history.py at line 38.
What does RunnableWithMessageHistory extend?
RunnableWithMessageHistory extends RunnableBindingBase, BaseMessage.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free