Skip to content

fix: add float support to merge_dicts and merge_obj#36048

Closed
RoomWithOutRoof (Jah-yee) wants to merge 1 commit intolangchain-ai:masterfrom
Jah-yee:fix/merge_dicts_float_support
Closed

fix: add float support to merge_dicts and merge_obj#36048
RoomWithOutRoof (Jah-yee) wants to merge 1 commit intolangchain-ai:masterfrom
Jah-yee:fix/merge_dicts_float_support

Conversation

@Jah-yee

Summary

Fixes issue #36011: merge_dicts raises TypeError for float values during streaming chunk aggregation.

Changes

  1. merge_dicts(): Changed isinstance(merged[right_k], int) to isinstance(merged[right_k], (int, float)) to handle float values the same way as integers.

  2. merge_obj(): Added numeric type handling for int and float - now sums numeric values (consistent with merge_dicts behavior).

Root Cause

The function handled str, dict, list, equal values, and int types — but not float. When two dicts contain the same key with unequal float values (e.g., logprob, score, safety scores, cost fields in streaming), the function falls through to the final else branch and raises a TypeError.

Testing

This fix enables the following to work without errors:

from langchain_core.utils._merge import merge_dicts

# Now works!
merge_dicts({"score": 0.5}, {"score": 0.3})  # Returns {"score": 0.8}
merge_dicts({"logprob": -1.2}, {"logprob": -0.8})  # Returns {"logprob": -2.0}

Related

- Changed isinstance(merged[right_k], int) to isinstance(merged[right_k], (int, float))
- Added numeric type handling in merge_obj for int and float
- Fixes issue langchain-ai#36011: merge_dicts raises TypeError for float values during streaming chunk aggregation
@github-actions github-actions bot added core `langchain-core` package issues & PRs fix For PRs that implement a fix size: XS < 50 LOC labels Mar 18, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 18, 2026

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 13 untouched benchmarks
⏩ 23 skipped benchmarks1


Comparing Jah-yee:fix/merge_dicts_float_support (397d11e) with master (07fa576)2

Open in CodSpeed

Footnotes

  1. 23 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on master (2476f55) during the generation of this report, so 07fa576 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@github-actions
Copy link

This PR has been automatically closed because it does not link to an approved issue.

All external contributions must reference an approved issue or discussion. Please:

  1. Find or open an issue describing the change
  2. Wait for a maintainer to approve and assign you
  3. Add Fixes #<issue_number>, Closes #<issue_number>, or Resolves #<issue_number> to your PR description and the PR will be reopened automatically

@github-actions github-actions bot closed this Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix missing-issue-link new-contributor size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant