BaseMessageChunk Class — langchain Architecture
Architecture documentation for the BaseMessageChunk class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 6a1cf81e_444c_4b27_c510_8e86e205cabb["BaseMessageChunk"] b9553aad_b797_0a7b_73ed_8d05b0819c0f["BaseMessage"] 6a1cf81e_444c_4b27_c510_8e86e205cabb -->|extends| b9553aad_b797_0a7b_73ed_8d05b0819c0f 6a1cf81e_444c_4b27_c510_8e86e205cabb["BaseMessageChunk"] 6a1cf81e_444c_4b27_c510_8e86e205cabb -->|extends| 6a1cf81e_444c_4b27_c510_8e86e205cabb 323271a6_dbeb_1ac5_e224_a66bfb56191b["base.py"] 6a1cf81e_444c_4b27_c510_8e86e205cabb -->|defined in| 323271a6_dbeb_1ac5_e224_a66bfb56191b c8d6f5a3_7591_846c_3f4f_2dbb710f44eb["__add__()"] 6a1cf81e_444c_4b27_c510_8e86e205cabb -->|method| c8d6f5a3_7591_846c_3f4f_2dbb710f44eb
Relationship Graph
Source Code
libs/core/langchain_core/messages/base.py lines 409–471
class BaseMessageChunk(BaseMessage):
"""Message chunk, which can be concatenated with other Message chunks."""
def __add__(self, other: Any) -> BaseMessageChunk: # type: ignore[override]
"""Message chunks support concatenation with other message chunks.
This functionality is useful to combine message chunks yielded from
a streaming model into a complete message.
Args:
other: Another message chunk to concatenate with this one.
Returns:
A new message chunk that is the concatenation of this message chunk
and the other message chunk.
Raises:
TypeError: If the other object is not a message chunk.
Example:
```txt
AIMessageChunk(content="Hello", ...)
+ AIMessageChunk(content=" World", ...)
= AIMessageChunk(content="Hello World", ...)
```
"""
if isinstance(other, BaseMessageChunk):
# If both are (subclasses of) BaseMessageChunk,
# concat into a single BaseMessageChunk
return self.__class__(
id=self.id,
type=self.type,
content=merge_content(self.content, other.content),
additional_kwargs=merge_dicts(
self.additional_kwargs, other.additional_kwargs
),
response_metadata=merge_dicts(
self.response_metadata, other.response_metadata
),
)
if isinstance(other, list) and all(
isinstance(o, BaseMessageChunk) for o in other
):
content = merge_content(self.content, *(o.content for o in other))
additional_kwargs = merge_dicts(
self.additional_kwargs, *(o.additional_kwargs for o in other)
)
response_metadata = merge_dicts(
self.response_metadata, *(o.response_metadata for o in other)
)
return self.__class__( # type: ignore[call-arg]
id=self.id,
content=content,
additional_kwargs=additional_kwargs,
response_metadata=response_metadata,
)
msg = (
'unsupported operand type(s) for +: "'
f"{self.__class__.__name__}"
f'" and "{other.__class__.__name__}"'
)
raise TypeError(msg)
Defined In
Extends
Source
Frequently Asked Questions
What is the BaseMessageChunk class?
BaseMessageChunk is a class in the langchain codebase, defined in libs/core/langchain_core/messages/base.py.
Where is BaseMessageChunk defined?
BaseMessageChunk is defined in libs/core/langchain_core/messages/base.py at line 409.
What does BaseMessageChunk extend?
BaseMessageChunk extends BaseMessage, BaseMessageChunk.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free