RunnableWithMessageHistory#

class langchain_core.runnables.history.RunnableWithMessageHistory[source]#

Bases: RunnableBindingBase

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.

Parameters:
  • get_session_history – Function that returns a new BaseChatMessageHistory. This function should either take a single positional argument session_id of type string and return a corresponding chat message history instance.

  • input_messages_key – Must be specified if the base runnable accepts a dict as input. The key in the input dict that contains the messages.

  • output_messages_key – Must be specified if the base Runnable returns a dict as output. The key in the output dict that contains the messages.

  • history_messages_key – Must be specified if the base runnable accepts a dict as input and expects a separate key for historical messages.

  • history_factory_config – Configure fields that should be passed to the chat history factory. See ConfigurableFieldSpec for more details.

Example: Chat message history with an in-memory implementation for testing.

from operator import itemgetter
from typing import List

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 langchain_core.pydantic_v1 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

Example where the wrapped Runnable takes a dictionary input:

from typing import Optional

from langchain_community.chat_models import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory


prompt = ChatPromptTemplate.from_messages([
    ("system", "You're an assistant who's good at {ability}"),
    MessagesPlaceholder(variable_name="history"),
    ("human", "{question}"),
])

chain = prompt | ChatAnthropic(model="claude-2")

chain_with_history = RunnableWithMessageHistory(
    chain,
    # Uses the get_by_session_id function defined in the example
    # above.
    get_by_session_id,
    input_messages_key="question",
    history_messages_key="history",
)

print(chain_with_history.invoke(  # noqa: T201
    {"ability": "math", "question": "What does cosine mean?"},
    config={"configurable": {"session_id": "foo"}}
))

# Uses the store defined in the example above.
print(store)  # noqa: T201

print(chain_with_history.invoke(  # noqa: T201
    {"ability": "math", "question": "What's its inverse"},
    config={"configurable": {"session_id": "foo"}}
))

print(store)  # noqa: T201

Example where the session factory takes two keys, user_id and conversation id):

store = {}

def get_session_history(
    user_id: str, conversation_id: str
) -> BaseChatMessageHistory:
    if (user_id, conversation_id) not in store:
        store[(user_id, conversation_id)] = InMemoryHistory()
    return store[(user_id, conversation_id)]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You're an assistant who's good at {ability}"),
    MessagesPlaceholder(variable_name="history"),
    ("human", "{question}"),
])

chain = prompt | ChatAnthropic(model="claude-2")

with_message_history = RunnableWithMessageHistory(
    chain,
    get_session_history=get_session_history,
    input_messages_key="question",
    history_messages_key="history",
    history_factory_config=[
        ConfigurableFieldSpec(
            id="user_id",
            annotation=str,
            name="User ID",
            description="Unique identifier for the user.",
            default="",
            is_shared=True,
        ),
        ConfigurableFieldSpec(
            id="conversation_id",
            annotation=str,
            name="Conversation ID",
            description="Unique identifier for the conversation.",
            default="",
            is_shared=True,
        ),
    ],
)

with_message_history.invoke(
    {"ability": "math", "question": "What does cosine mean?"},
    config={"configurable": {"user_id": "123", "conversation_id": "1"}}
)

Initialize RunnableWithMessageHistory.

Parameters:
  • runnable

    The base Runnable to be wrapped. Must take as input one of: 1. A sequence of BaseMessages 2. A dict with one key for all messages 3. A dict with one key for the current input string/message(s) and

    a separate key for historical messages. If the input key points to a string, it will be treated as a HumanMessage in history.

    Must return as output one of: 1. A string which can be treated as an AIMessage 2. A BaseMessage or sequence of BaseMessages 3. A dict with a key for a BaseMessage or sequence of BaseMessages

  • get_session_history

    Function that returns a new BaseChatMessageHistory. This function should either take a single positional argument session_id of type string and return a corresponding chat message history instance. .. code-block:: python

    def get_session_history(

    session_id: str, *, user_id: Optional[str]=None

    ) -> BaseChatMessageHistory:

    Or it should take keyword arguments that match the keys of session_history_config_specs and return a corresponding chat message history instance.

    def get_session_history(
        *,
        user_id: str,
        thread_id: str,
    ) -> BaseChatMessageHistory:
        ...
    

  • input_messages_key – Must be specified if the base runnable accepts a dict as input. Default is None.

  • output_messages_key – Must be specified if the base runnable returns a dict as output. Default is None.

  • history_messages_key – Must be specified if the base runnable accepts a dict as input and expects a separate key for historical messages.

  • history_factory_config – Configure fields that should be passed to the chat history factory. See ConfigurableFieldSpec for more details. Specifying these allows you to pass multiple config keys into the get_session_history factory.

  • **kwargs – Arbitrary additional kwargs to pass to parent class RunnableBindingBase init.

Note

RunnableWithMessageHistory implements the standard Runnable Interface. 🏃

The Runnable Interface has additional methods that are available on runnables, such as with_types, with_retry, assign, bind, get_graph, and more.

param bound: Runnable[Input, Output] [Required]#

The underlying Runnable that this Runnable delegates to.

param config: RunnableConfig [Optional]#

The config to bind to the underlying Runnable.

param config_factories: List[Callable[[RunnableConfig], RunnableConfig]] [Optional]#

The config factories to bind to the underlying Runnable.

param custom_input_type: Any | None = None#

Override the input type of the underlying Runnable with a custom type.

The type can be a pydantic model, or a type annotation (e.g., List[str]).

param custom_output_type: Any | None = None#

Override the output type of the underlying Runnable with a custom type.

The type can be a pydantic model, or a type annotation (e.g., List[str]).

param get_session_history: GetSessionHistoryCallable [Required]#
param history_factory_config: Sequence[ConfigurableFieldSpec] [Required]#
param history_messages_key: str | None = None#
param input_messages_key: str | None = None#
param kwargs: Mapping[str, Any] [Optional]#

kwargs to pass to the underlying Runnable when running.

For example, when the Runnable binding is invoked the underlying Runnable will be invoked with the same input but with these additional kwargs.

param output_messages_key: str | None = None#
async abatch(inputs: List[Input], config: RunnableConfig | List[RunnableConfig] | None = None, *, return_exceptions: bool = False, **kwargs: Any | None) List[Output]#

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:
  • inputs (List[Input]) – A list of inputs to the Runnable.

  • config (RunnableConfig | List[RunnableConfig] | None) – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

  • return_exceptions (bool) – Whether to return exceptions instead of raising them. Defaults to False.

  • kwargs (Any | None) – Additional keyword arguments to pass to the Runnable.

Returns:

A list of outputs from the Runnable.

Return type:

List[Output]

async abatch_as_completed(inputs: Sequence[Input], config: RunnableConfig | Sequence[RunnableConfig] | None = None, *, return_exceptions: bool = False, **kwargs: Any | None) AsyncIterator[Tuple[int, Output | Exception]]#

Run ainvoke in parallel on a list of inputs, yielding results as they complete.

Parameters:
  • inputs (Sequence[Input]) – A list of inputs to the Runnable.

  • config (RunnableConfig | Sequence[RunnableConfig] | None) – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None. Defaults to None.

  • return_exceptions (bool) – Whether to return exceptions instead of raising them. Defaults to False.

  • kwargs (Any | None) – Additional keyword arguments to pass to the Runnable.

Yields:

A tuple of the index of the input and the output from the Runnable.

Return type:

AsyncIterator[Tuple[int, Output | Exception]]

async ainvoke(input: Input, config: RunnableConfig | None = None, **kwargs: Any | None) Output#

Default implementation of ainvoke, calls invoke from a thread.

The default implementation allows usage of async code even if the Runnable did not implement a native async version of invoke.

Subclasses should override this method if they can run asynchronously.

Parameters:
  • input (Input) –

  • config (RunnableConfig | None) –

  • kwargs (Any | None) –

Return type:

Output

async astream(input: Input, config: RunnableConfig | None = None, **kwargs: Any | None) AsyncIterator[Output]#

Default implementation of astream, which calls ainvoke. Subclasses should override this method if they support streaming output.

Parameters:
  • input (Input) – The input to the Runnable.

  • config (RunnableConfig | None) – The config to use for the Runnable. Defaults to None.

  • kwargs (Any | None) – Additional keyword arguments to pass to the Runnable.

Yields:

The output of the Runnable.

Return type:

AsyncIterator[Output]

async astream_events(input: Input, config: RunnableConfig | None = None, **kwargs: Any | None) AsyncIterator[StandardStreamEvent | CustomStreamEvent]#

Beta

This API is in beta and may change in the future.

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the

    format: on_[runnable_type]_(start|stream|end).

  • name: str - The name of the Runnable that generated the event.

  • run_id: str - randomly generated ID associated with the given execution of

    the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.

  • parent_ids: List[str] - The IDs of the parent runnables that

    generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.

  • tags: Optional[List[str]] - The tags of the Runnable that generated

    the event.

  • metadata: Optional[Dict[str, Any]] - The metadata of the Runnable

    that generated the event.

  • data: Dict[str, Any]

Below is a table that illustrates some evens that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

ATTENTION This reference table is for the V2 version of the schema.

event

name

chunk

input

output

on_chat_model_start

[model name]

{“messages”: [[SystemMessage, HumanMessage]]}

on_chat_model_stream

[model name]

AIMessageChunk(content=”hello”)

on_chat_model_end

[model name]

{“messages”: [[SystemMessage, HumanMessage]]}

AIMessageChunk(content=”hello world”)

on_llm_start

[model name]

{‘input’: ‘hello’}

on_llm_stream

[model name]

‘Hello’

on_llm_end

[model name]

‘Hello human!’

on_chain_start

format_docs

on_chain_stream

format_docs

“hello world!, goodbye world!”

on_chain_end

format_docs

[Document(…)]

“hello world!, goodbye world!”

on_tool_start

some_tool

{“x”: 1, “y”: “2”}

on_tool_end

some_tool

{“x”: 1, “y”: “2”}

on_retriever_start

[retriever name]

{“query”: “hello”}

on_retriever_end

[retriever name]

{“query”: “hello”}

[Document(…), ..]

on_prompt_start

[template_name]

{“question”: “hello”}

on_prompt_end

[template_name]

{“question”: “hello”}

ChatPromptValue(messages: [SystemMessage, …])

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

Attribute

Type

Description

name

str

A user defined name for the event.

data

Any

The data associated with the event. This can be anything, though we suggest making it JSON serializable.

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: List[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])

format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [("system", "You are Cat Agent 007"), ("human", "{question}")]
).with_config({"run_name": "my_template", "tags": ["my_template"]})

Example:

from langchain_core.runnables import RunnableLambda

async def reverse(s: str) -> str:
    return s[::-1]

chain = RunnableLambda(func=reverse)

events = [
    event async for event in chain.astream_events("hello", version="v2")
]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)
Parameters:
  • input (Input) – The input to the Runnable.

  • config (RunnableConfig | None) – The config to use for the Runnable.

  • version – The version of the schema to use either v2 or v1. Users should use v2. v1 is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in v2.

  • include_names – Only include events from runnables with matching names.

  • include_types – Only include events from runnables with matching types.

  • include_tags – Only include events from runnables with matching tags.

  • exclude_names – Exclude events from runnables with matching names.

  • exclude_types – Exclude events from runnables with matching types.

  • exclude_tags – Exclude events from runnables with matching tags.

  • kwargs (Any | None) – Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

Yields:

An async stream of StreamEvents.

Raises:

NotImplementedError – If the version is not v1 or v2.

Return type:

AsyncIterator[StandardStreamEvent | CustomStreamEvent]

batch(inputs: List[Input], config: RunnableConfig | List[RunnableConfig] | None = None, *, return_exceptions: bool = False, **kwargs: Any | None) List[Output]#

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:
Return type:

List[Output]

batch_as_completed(inputs: Sequence[Input], config: RunnableConfig | Sequence[RunnableConfig] | None = None, *, return_exceptions: bool = False, **kwargs: Any | None) Iterator[Tuple[int, Output | Exception]]#

Run invoke in parallel on a list of inputs, yielding results as they complete.

Parameters:
  • inputs (Sequence[Input]) –

  • config (RunnableConfig | Sequence[RunnableConfig] | None) –

  • return_exceptions (bool) –

  • kwargs (Any | None) –

Return type:

Iterator[Tuple[int, Output | Exception]]

configurable_alternatives(which: ConfigurableField, *, default_key: str = 'default', prefix_keys: bool = False, **kwargs: Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]) RunnableSerializable[Input, Output]#

Configure alternatives for Runnables that can be set at runtime.

Parameters:
  • which (ConfigurableField) – The ConfigurableField instance that will be used to select the alternative.

  • default_key (str) – The default key to use if no alternative is selected. Defaults to “default”.

  • prefix_keys (bool) – Whether to prefix the keys with the ConfigurableField id. Defaults to False.

  • **kwargs (Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]) – A dictionary of keys to Runnable instances or callables that return Runnable instances.

Returns:

A new Runnable with the alternatives configured.

Return type:

RunnableSerializable[Input, Output]

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-sonnet-20240229"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI()
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(
        configurable={"llm": "openai"}
    ).invoke("which organization created you?").content
)
configurable_fields(**kwargs: ConfigurableField | ConfigurableFieldSingleOption | ConfigurableFieldMultiOption) RunnableSerializable[Input, Output]#

Configure particular Runnable fields at runtime.

Parameters:

**kwargs (ConfigurableField | ConfigurableFieldSingleOption | ConfigurableFieldMultiOption) – A dictionary of ConfigurableField instances to configure.

Returns:

A new Runnable with the fields configured.

Return type:

RunnableSerializable[Input, Output]

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print(
    "max_tokens_20: ",
    model.invoke("tell me something about chess").content
)

# max_tokens = 200
print("max_tokens_200: ", model.with_config(
    configurable={"output_token_number": 200}
    ).invoke("tell me something about chess").content
)
invoke(input: Input, config: RunnableConfig | None = None, **kwargs: Any | None) Output#

Transform a single input into an output. Override to implement.

Parameters:
  • input (Input) – The input to the Runnable.

  • config (RunnableConfig | None) – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details.

  • kwargs (Any | None) –

Returns:

The output of the Runnable.

Return type:

Output

stream(input: Input, config: RunnableConfig | None = None, **kwargs: Any | None) Iterator[Output]#

Default implementation of stream, which calls invoke. Subclasses should override this method if they support streaming output.

Parameters:
  • input (Input) – The input to the Runnable.

  • config (RunnableConfig | None) – The config to use for the Runnable. Defaults to None.

  • kwargs (Any | None) – Additional keyword arguments to pass to the Runnable.

Yields:

The output of the Runnable.

Return type:

Iterator[Output]

to_json() SerializedConstructor | SerializedNotImplemented#

Serialize the Runnable to JSON.

Returns:

A JSON-serializable representation of the Runnable.

Return type:

SerializedConstructor | SerializedNotImplemented

Examples using RunnableWithMessageHistory