Skip to content

The agent engineering platform.

fix(core): preserve index and timestamp fields when merging (#34731)

Mason Daughertyba3ad67328fd
diff --git a/libs/core/langchain_core/utils/_merge.py b/libs/core/langchain_core/utils/_merge.py
index 9a39ecea80..251f9260b2 100644
--- a/libs/core/langchain_core/utils/_merge.py
+++ b/libs/core/langchain_core/utils/_merge.py
@@ -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 "
diff --git a/libs/core/tests/unit_tests/utils/test_utils.py b/libs/core/tests/unit_tests/utils/test_utils.py
index 78988b71fd..815296f888 100644
--- a/libs/core/tests/unit_tests/utils/test_utils.py
+++ b/libs/core/tests/unit_tests/utils/test_utils.py
@@ -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(