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
Conversation
…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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes an off-by-one error in
convertBaseMessagesToContentthat prevents consecutiveToolMessages 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 alwaysundefinedbecause arrays are 0-indexed. The duplicate-role check never fires, so consecutiveToolMessages (which should be merged into a single user turn per Gemini API requirements) silently create separate content entries.Changes
acc.content[acc.content.length]→acc.content[acc.content.length - 1]Test Plan
Added unit tests verifying:
ToolMessages are merged into a single user turnToolMessages are all mergedFixes #10342