Skip to content
Merged
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
9 changes: 8 additions & 1 deletion libs/core/langchain_core/utils/_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ def merge_dicts(left: dict[str, Any], *others: dict[str, Any]) -> dict[str, Any]
elif merged[right_k] == right_v:
continue
elif isinstance(merged[right_k], int):
merged[right_k] += right_v
# Preserve identification and temporal fields using last-wins strategy
# instead of summing:
# - index: identifies which tool call a chunk belongs to
# - created/timestamp: temporal values that shouldn't be accumulated
if right_k in {"index", "created", "timestamp"}:
merged[right_k] = right_v
else:
merged[right_k] += right_v
else:
msg = (
f"Additional kwargs key {right_k} already exists in left dict and "
Expand Down
12 changes: 12 additions & 0 deletions libs/core/tests/unit_tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ def test_check_package_version(
{"a": [{"idx": 0, "b": "f"}]},
{"a": [{"idx": 0, "b": "{"}, {"idx": 0, "b": "f"}]},
),
# Integer 'index' should be preserved, not summed (tool call identification)
({"index": 1}, {"index": 1}, {"index": 1}),
({"index": 0}, {"index": 1}, {"index": 1}),
# 'created' timestamp should be preserved, not summed
({"created": 1700000000}, {"created": 1700000000}, {"created": 1700000000}),
({"created": 1700000000}, {"created": 1700000001}, {"created": 1700000001}),
# 'timestamp' should be preserved, not summed
({"timestamp": 100}, {"timestamp": 100}, {"timestamp": 100}),
({"timestamp": 100}, {"timestamp": 200}, {"timestamp": 200}),
# Other integer fields should still be summed (e.g., token counts)
({"tokens": 10}, {"tokens": 5}, {"tokens": 15}),
({"count": 1}, {"count": 2}, {"count": 3}),
],
)
def test_merge_dicts(
Expand Down