Skip to content

core[patch]: Int Combine when Merging Dicts #31572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/core/langchain_core/utils/_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def merge_dicts(left: dict[str, Any], *others: dict[str, Any]) -> dict[str, Any]
merged[right_k] = merge_lists(merged[right_k], right_v)
elif merged[right_k] == right_v:
continue
elif isinstance(merged[right_k], int):
merged[right_k] += right_v
else:
msg = (
f"Additional kwargs key {right_k} already exists in left dict and "
Expand Down
8 changes: 8 additions & 0 deletions libs/core/tests/unit_tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pydantic import SecretStr

from langchain_core import utils
from langchain_core.outputs import GenerationChunk
from langchain_core.utils import (
check_package_version,
from_env,
Expand Down Expand Up @@ -395,3 +396,10 @@ class OhMy(BaseModel):

with pytest.raises(ValueError, match="Did not find FOOFOOFOOBAR"):
OhMy()


def test_generation_chunk_addition_type_error() -> None:
chunk1 = GenerationChunk(text="", generation_info={"len": 0})
chunk2 = GenerationChunk(text="Non-empty text", generation_info={"len": 14})
result = chunk1 + chunk2
assert result == GenerationChunk(text="Non-empty text", generation_info={"len": 14})
Loading