Skip to content

fix(core): prevent tool call chunks from merging incorrectly in AIMes… #8433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

christian-bromann
Copy link
Contributor

Description

This PR fixes a bug in AIMessageChunk.concat() where tool call chunks with the same index were being incorrectly merged instead of preserved as separate tool calls.

Problem

When using models like Ollama that return multiple tool calls in chunks, the _mergeLists function was merging tool call chunks that had the same index property. This resulted in malformed tool calls with concatenated properties:

// Before fix - incorrectly merged:
{
  name: "add_new_taskadd_ideas",
  args: '{"tasks":["buy tomatoes","help child with math"]}{"ideas":["read about Angular 19 updates"]}',
  id: "9fb5c937-6944-4173-84be-ad1caee1cedd1c9d8514-874a-4d8a-a9a9-604b9d831b60"
}

// After fix - correctly preserved as separate calls:
[
  {
    name: "add_new_task",
    args: '{"tasks":["buy tomatoes","help child with math"]}',
    id: "9fb5c937-6944-4173-84be-ad1caee1cedd"
  },
  {
    name: "add_ideas", 
    args: '{"ideas":["read about Angular 19 updates"]}',
    id: "5abf542e-87f3-4899-87c6-8f7d9cb6a28d"
  }
]

Solution

  • Replaced _mergeLists() with simple array concatenation using the spread operator
  • Added comprehensive test case to verify tool call chunks remain separate during concatenation
  • Removed unused _mergeLists import

Impact

  • ✅ Multiple tool calls now work correctly with streaming providers like Ollama
  • ✅ Tool call arguments remain valid JSON and can be parsed properly
  • ✅ No more invalid_tool_calls due to malformed merged data
  • ✅ Existing functionality preserved (all tests pass)

Testing

  • Added specific test case for the reported scenario
  • All existing AIMessageChunk tests continue to pass
  • Verified fix works with the exact example from the issue report

Fixes #8276

…sageChunk.concat()

When concatenating AIMessageChunk instances with tool_call_chunks that have
the same index, the previous implementation used _mergeLists which would
merge tool calls with matching indices by concatenating their properties
(name, args, id). This resulted in malformed tool calls like:
- name: "add_new_taskadd_ideas"
- args: "{...}{...}" (invalid JSON)
- id: "id1id2"

This fix replaces _mergeLists with simple array concatenation to preserve
each tool call chunk as a separate entity, ensuring proper tool execution
when multiple tools are called simultaneously.

Fixes issue where Ollama and other providers returning multiple tool calls
in chunks would result in invalid_tool_calls due to malformed merged data.

- Replace _mergeLists with array spread operator for tool_call_chunks
- Add test case to verify tool call chunks remain separate during concat
- Remove unused _mergeLists import
Copy link

vercel bot commented Jun 28, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Jun 28, 2025 3:16am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Jun 28, 2025 3:16am

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. auto:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Jun 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AIMessageChunk incorrectly merged tool_calls
1 participant