AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Merge_dicts Raises TypeError For Unequal Float Values During Streaming Chunk Aggregation

The `merge_dicts` function in langchain-core fails to merge dictionaries containing unequal float values for the same key, causing a `TypeError` during streaming chunk aggregation. The type check for numeric values only covers `int`, not `float`.

highConfidence 95%Langchain-CoreRetrieverslangchain-coreChat ModelsAffected V1.2.19

Origin Analysis

In `langchain_core/utils/_merge.py`, the type dispatch for handling unequal values of the same key only checks `isinstance(merged[right_k], int)`, omitting `float`. Consequently, when both sides have non-equal floats, the logic falls through to the final `else` branch, which raises `TypeError`. The same omission exists in `merge_obj`.
1. Import `merge_dicts` from `langchain_core.utils._merge`. 2. Call `merge_dicts({"score": 0.5}, {"score": 0.3})`. 3. Observe `TypeError: Additional kwargs key score already exists in left dict and value has unsupported type <class 'float'>`.

Fixing Code Block

Edge Case Audit

While this fix allows floating-point addition, be aware that floating-point precision may lead to unexpected accumulated values (e.g., 0.1+0.2 != 0.3). In streaming aggregation, repeated float additions can amplify precision errors. If exact decimal arithmetic is required, consider using `Decimal` or rounding after aggregation. Additionally, this change may alter behavior for `bool` values since `bool` is a subclass of `int`; original code already treated bool as int, so no change there. Rollback: if any regression occurs, revert to checking `isinstance(merged[right_k], int)` and handle floats separately (e.g., by preserving latest value) or defer to downstream logic.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state