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

- **Description:** Combining the Int Types by adding them which makes
the most sense.
- **Issue:**  #31565
This commit is contained in:
Mohammad Mohtashim 2025-07-04 23:44:16 +05:00 committed by GitHub
parent 6a5073b227
commit b26d2250ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View File

@ -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 "

View File

@ -9,6 +9,7 @@ import pytest
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,
@ -375,3 +376,10 @@ def test_using_secret_from_env_as_default_factory(
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})