Skip to content

fix(google-genai): merge consecutive ToolMessages into single turn for parallel function calling#10442

Open
guoyangzhen (guoyangzhen) wants to merge 1 commit intolangchain-ai:mainfrom
guoyangzhen:fix/consecutive-toolmessage-merging
Open

fix(google-genai): merge consecutive ToolMessages into single turn for parallel function calling#10442
guoyangzhen (guoyangzhen) wants to merge 1 commit intolangchain-ai:mainfrom
guoyangzhen:fix/consecutive-toolmessage-merging

Conversation

@guoyangzhen

Summary

Fixes an off-by-one error in convertBaseMessagesToContent that prevents consecutive ToolMessages from being detected and merged. This causes the Gemini API to hang indefinitely when processing parallel function call results.

Root Cause

acc.content[acc.content.length] is always undefined because arrays are 0-indexed. The duplicate-role check never fires, so consecutive ToolMessages (which should be merged into a single user turn per Gemini API requirements) silently create separate content entries.

Changes

  1. Fixed off-by-one: acc.content[acc.content.length]acc.content[acc.content.length - 1]
  2. Added merge logic: When consecutive function-role messages are detected, their parts are merged into a single content entry instead of throwing an error

Test Plan

Added unit tests verifying:

  • Consecutive ToolMessages are merged into a single user turn
  • 3+ consecutive ToolMessages are all merged
  • Consecutive non-function messages with the same role still throw correctly

Fixes #10342

…r parallel function calling

Fixes off-by-one error in convertBaseMessagesToContent where
acc.content[acc.content.length] was always undefined, preventing
the duplicate-role check from detecting consecutive function messages.

When consecutive ToolMessages are detected (parallel function call
responses), they are now merged into a single user content entry
with multiple functionResponse parts, as required by the Gemini API.

Previously, each ToolMessage created a separate user turn, causing
the Gemini API to hang indefinitely when processing parallel
function call results.

Fixes langchain-ai#10342
@changeset-bot
Copy link

changeset-bot bot commented Mar 17, 2026

⚠️ No Changeset found

Latest commit: b91e68b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: @langchain/google-genai creates separate turns for consecutive ToolMessages, breaking Gemini parallel function calling

2 participants