merge_message_runs() — langchain Function Reference
Architecture documentation for the merge_message_runs() function in utils.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 23370d26_f8d8_9847_9d6f_8efb2a70861b["merge_message_runs()"] 0b528c80_0ce7_1c74_8932_bc433bcb03c6["utils.py"] 23370d26_f8d8_9847_9d6f_8efb2a70861b -->|defined in| 0b528c80_0ce7_1c74_8932_bc433bcb03c6 7d8fdbaf_a57f_bad7_f47e_85c3fa1f78fe["convert_to_messages()"] 23370d26_f8d8_9847_9d6f_8efb2a70861b -->|calls| 7d8fdbaf_a57f_bad7_f47e_85c3fa1f78fe d31a20bf_ff12_4993_e1ff_ba8646e7ebd5["_msg_to_chunk()"] 23370d26_f8d8_9847_9d6f_8efb2a70861b -->|calls| d31a20bf_ff12_4993_e1ff_ba8646e7ebd5 42b7cd62_fb9b_666d_1162_ef1187915080["_chunk_to_msg()"] 23370d26_f8d8_9847_9d6f_8efb2a70861b -->|calls| 42b7cd62_fb9b_666d_1162_ef1187915080 style 23370d26_f8d8_9847_9d6f_8efb2a70861b fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/messages/utils.py lines 951–1076
def merge_message_runs(
messages: Iterable[MessageLikeRepresentation] | PromptValue,
*,
chunk_separator: str = "\n",
) -> list[BaseMessage]:
r"""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.
Args:
messages: Sequence Message-like objects to merge.
chunk_separator: Specify the string to be inserted between message chunks.
Returns:
list of BaseMessages with consecutive runs of message types merged into single
messages. By default, if two messages being merged both have string contents,
the merged content is a concatenation of the two strings with a new-line
separator.
The separator inserted between message chunks can be controlled by specifying
any string with `chunk_separator`. If at least one of the messages has a list
of content blocks, the merged content is a list of content blocks.
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"}
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does merge_message_runs() do?
merge_message_runs() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/utils.py.
Where is merge_message_runs() defined?
merge_message_runs() is defined in libs/core/langchain_core/messages/utils.py at line 951.
What does merge_message_runs() call?
merge_message_runs() calls 3 function(s): _chunk_to_msg, _msg_to_chunk, convert_to_messages.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free