Skip to content

Commit f5fbad2

Browse files
inf3rnusErick Friis
authored andcommitted
Core: Fix __add__ for concatting two BaseMessageChunk's (#29531)
Description: The change allows you to use the overloaded `+` operator correctly when `+`ing two BaseMessageChunk subclasses. Without this you *must* instantiate a subclass for it to work. Which feels... wrong. Base classes should be decoupled from sub classes and should have in no way a dependency on them. Issue: You can't `+` a BaseMessageChunk with a BaseMessageChunk e.g. this will explode ```py from langchain_core.outputs import ( ChatGenerationChunk, ) from langchain_core.messages import BaseMessageChunk chunk1 = ChatGenerationChunk( message=BaseMessageChunk( type="customChunk", content="HI", ), ) chunk2 = ChatGenerationChunk( message=BaseMessageChunk( type="customChunk", content="HI", ), ) # this will throw new_chunk = chunk1 + chunk2 ``` In case anyone ran into this issue themselves, it's probably best to use the AIMessageChunk: a la ```py from langchain_core.outputs import ( ChatGenerationChunk, ) from langchain_core.messages import AIMessageChunk chunk1 = ChatGenerationChunk( message=AIMessageChunk( content="HI", ), ) chunk2 = ChatGenerationChunk( message=AIMessageChunk( content="HI", ), ) # No explosion! new_chunk = chunk1 + chunk2 ``` Dependencies: None! Twitter handle: `aaron_vogler` Keeping these for later if need be: ``` baskaryan efriis eyurtsev ccurme vbarda hwchase17 baskaryan efriis ``` Co-authored-by: Erick Friis <[email protected]>
1 parent 121de59 commit f5fbad2

File tree

1 file changed

+1
-0
lines changed
  • libs/core/langchain_core/messages

1 file changed

+1
-0
lines changed

libs/core/langchain_core/messages/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def __add__(self, other: Any) -> BaseMessageChunk: # type: ignore
198198

199199
return self.__class__( # type: ignore[call-arg]
200200
id=self.id,
201+
type=self.type,
201202
content=merge_content(self.content, other.content),
202203
additional_kwargs=merge_dicts(
203204
self.additional_kwargs, other.additional_kwargs

0 commit comments

Comments
 (0)