# merge_message_runs

> **Function** in `langchain_core`

📖 [View in docs](https://reference.langchain.com/python/langchain-core/messages/utils/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.

## Signature

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

## Description

**Example:**

```python
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)
```

```python
[
    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

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `messages` | `Iterable[MessageLikeRepresentation] \| PromptValue` | Yes | Sequence Message-like objects to merge. |
| `chunk_separator` | `str` | No | Specify the string to be inserted between message chunks. (default: `'\n'`) |

## Returns

`list[BaseMessage]`

list of BaseMessages with consecutive runs of message types merged into single

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/fb6ab993a73180538f6cca876b3c85d46c08845f/libs/core/langchain_core/messages/utils.py#L950)