LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-coremessagesutilsmerge_message_runs
    Function●Since v0.2

    merge_message_runs

    Merge consecutive Messages of the same type.

    Note

    ToolMessage objects are not merged, as each has a distinct tool call id that can't be merged.

    Copy
    merge_message_runs(
      messages: Iterable[MessageLikeRepresentation] | PromptValue,
      *,
      chunk_separator: str = '\n'
    ) -> list[BaseMessage]

    Example:

    from langchain_core.messages import (
        merge_message_runs,
        AIMessage,
        HumanMessage,
        SystemMessage,
        ToolCall,
    )
    
    messages = [
        SystemMessage("you're a good assistant."),
        HumanMessage(
            "what's your favorite color",
            id="foo",
        ),
        HumanMessage(
            "wait your favorite food",
            id="bar",
        ),
        AIMessage(
            "my favorite colo",
            tool_calls=[
                ToolCall(
                    name="blah_tool", args={"x": 2}, id="123", type="tool_call"
                )
            ],
            id="baz",
        ),
        AIMessage(
            [{"type": "text", "text": "my favorite dish is lasagna"}],
            tool_calls=[
                ToolCall(
                    name="blah_tool",
                    args={"x": -10},
                    id="456",
                    type="tool_call",
                )
            ],
            id="blur",
        ),
    ]
    
    merge_message_runs(messages)
    [
        SystemMessage("you're a good assistant."),
        HumanMessage(
            "what's your favorite color\\n"
            "wait your favorite food", id="foo",
        ),
        AIMessage(
            [
                "my favorite colo",
                {"type": "text", "text": "my favorite dish is lasagna"}
            ],
            tool_calls=[
                ToolCall({
                    "name": "blah_tool",
                    "args": {"x": 2},
                    "id": "123",
                    "type": "tool_call"
                }),
                ToolCall({
                    "name": "blah_tool",
                    "args": {"x": -10},
                    "id": "456",
                    "type": "tool_call"
                })
            ]
            id="baz"
        ),
    ]
    

    Parameters

    NameTypeDescription
    messages*Iterable[MessageLikeRepresentation] | PromptValue

    Sequence Message-like objects to merge.

    chunk_separatorstr
    Default:'\n'

    Specify the string to be inserted between message chunks.

    View source on GitHub