Skip to content

fix(core): prevent cross-type content block merging in merge_lists#35984

Open
Mason Daugherty (mdrxy) wants to merge 2 commits intomasterfrom
mdrxy/fix-merge-list
Open

fix(core): prevent cross-type content block merging in merge_lists#35984
Mason Daugherty (mdrxy) wants to merge 2 commits intomasterfrom
mdrxy/fix-merge-list

Conversation

@mdrxy
Copy link
Member

@mdrxy Mason Daugherty (mdrxy) commented Mar 17, 2026

merge_lists merges dicts by matching index values but does not check type. When a model returns both text and tool calls in a streaming response with output_version="v1", text blocks (index: 0 from the streaming wrapper) and tool_call_chunk blocks (index: 0 from the API) merge into a single corrupt dict with type: "text" carrying orphaned name/args/id fields. The init_tool_calls validator cannot find type: "tool_call_chunk" to promote, so tool calls silently vanish from content_blocks.

This affects BaseChatOpenAI subclassers without registered block translators and any model that returns text alongside tool calls (e.g., GLM-5 via Baseten). Related to #32888 which proposes the broader fix of raising on type conflicts in merge_dicts.

Changes

  • Add a type guard to merge_lists index-matching: prevent merging when one block has type: "text" and the other does not — a text-vs-everything-else partition that keeps text and tool_call_chunk blocks separate so init_tool_calls can promote them correctly. Non-text blocks at the same index (e.g., Anthropic's tool_use + input_json_delta) still merge as before.

Revert basetenlabs/langchain-baseten#6 when released

@github-actions github-actions bot added size: XS < 50 LOC core `langchain-core` package issues & PRs internal fix For PRs that implement a fix and removed fix For PRs that implement a fix labels Mar 17, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 17, 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 mdrxy/fix-merge-list (d8537d1) with master (69a7b9c)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 (2bad58a) during the generation of this report, so 69a7b9c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@mdrxy Mason Daugherty (mdrxy) changed the title fix(core): prevent cross-type content block merging in merge_lists fix(core): prevent cross-type content block merging in merge_lists Mar 17, 2026
@github-actions github-actions bot added fix For PRs that implement a fix and removed fix For PRs that implement a fix labels Mar 17, 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 fix For PRs that implement a fix internal size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant