*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to merge consecutive messages of the same type

On this page

How to merge consecutive messages of the same type
==================================================

Certain models do not support passing in consecutive messages of the same type (a.k.a. "runs" of the same message type).

The `merge_message_runs` utility makes it easy to merge consecutive messages of the same type.

Basic usage[​](#basic-usage "Direct link to Basic usage")
---------------------------------------------------------

    from langchain_core.messages import (    AIMessage,    HumanMessage,    SystemMessage,    merge_message_runs,)messages = [    SystemMessage("you're a good assistant."),    SystemMessage("you always respond with a joke."),    HumanMessage([{"type": "text", "text": "i wonder why it's called langchain"}]),    HumanMessage("and who is harrison chasing anyways"),    AIMessage(        'Well, I guess they thought "WordRope" and "SentenceString" just didn\'t have the same ring to it!'    ),    AIMessage("Why, he's probably chasing after the last cup of coffee in the office!"),]merged = merge_message_runs(messages)print("\n\n".join([repr(x) for x in merged]))

**API Reference:**[AIMessage](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.ai.AIMessage.html) | [HumanMessage](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.human.HumanMessage.html) | [SystemMessage](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.system.SystemMessage.html) | [merge\_message\_runs](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.utils.merge_message_runs.html)

    SystemMessage(content="you're a good assistant.\nyou always respond with a joke.")HumanMessage(content=[{'type': 'text', 'text': "i wonder why it's called langchain"}, 'and who is harrison chasing anyways'])AIMessage(content='Well, I guess they thought "WordRope" and "SentenceString" just didn\'t have the same ring to it!\nWhy, he\'s probably chasing after the last cup of coffee in the office!')

Notice that if the contents of one of the messages to merge is a list of content blocks then the merged message will have a list of content blocks. And if both messages to merge have string contents then those are concatenated with a newline character.

Chaining[​](#chaining "Direct link to Chaining")
------------------------------------------------

`merge_message_runs` can be used in an imperatively (like above) or declaratively, making it easy to compose with other components in a chain:

    # pip install -U langchain-anthropicfrom langchain_anthropic import ChatAnthropicllm = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0)# Notice we don't pass in messages. This creates# a RunnableLambda that takes messages as inputmerger = merge_message_runs()chain = merger | llmchain.invoke(messages)

**API Reference:**[ChatAnthropic](https://api.python.langchain.com/en/latest/chat_models/langchain_anthropic.chat_models.ChatAnthropic.html)

    AIMessage(content=[], response_metadata={'id': 'msg_01D6R8Naum57q8qBau9vLBUX', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 84, 'output_tokens': 3}}, id='run-ac0c465b-b54f-4b8b-9295-e5951250d653-0', usage_metadata={'input_tokens': 84, 'output_tokens': 3, 'total_tokens': 87})

Looking at the LangSmith trace we can see that before the messages are passed to the model they are merged: [https://smith.langchain.com/public/ab558677-cac9-4c59-9066-1ecce5bcd87c/r](https://smith.langchain.com/public/ab558677-cac9-4c59-9066-1ecce5bcd87c/r)

Looking at just the merger, we can see that it's a Runnable object that can be invoked like all Runnables:

    merger.invoke(messages)

    [SystemMessage(content="you're a good assistant.\nyou always respond with a joke."), HumanMessage(content=[{'type': 'text', 'text': "i wonder why it's called langchain"}, 'and who is harrison chasing anyways']), AIMessage(content='Well, I guess they thought "WordRope" and "SentenceString" just didn\'t have the same ring to it!\nWhy, he\'s probably chasing after the last cup of coffee in the office!')]

API reference[​](#api-reference "Direct link to API reference")
---------------------------------------------------------------

For a complete description of all arguments head to the API reference: [https://api.python.langchain.com/en/latest/messages/langchain\_core.messages.utils.merge\_message\_runs.html](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.utils.merge_message_runs.html)

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/merge_message_runs.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to split Markdown by Headers

](/v0.2/docs/how_to/markdown_header_metadata_splitter/)[

Next

How to add message history

](/v0.2/docs/how_to/message_history/)

*   [Basic usage](#basic-usage)
*   [Chaining](#chaining)
*   [API reference](#api-reference)