Skip to content

fix(google-genai): merge consecutive function-role messages in convertBaseMessagesToContent#10345

Open
Mohammad Jafari (mossein) wants to merge 4 commits intolangchain-ai:mainfrom
mossein:fix/google-genai-merge-function-messages
Open

fix(google-genai): merge consecutive function-role messages in convertBaseMessagesToContent#10345
Mohammad Jafari (mossein) wants to merge 4 commits intolangchain-ai:mainfrom
mossein:fix/google-genai-merge-function-messages

Conversation

@mossein

Fixes #10342

When multiple tool calls are executed in parallel, convertBaseMessagesToContent receives consecutive ToolMessages that all map to the function role. The existing code throws "Google Generative AI requires alternate messages between authors" instead of merging them.

This patch:

  1. Fixes an off-by-one bug (acc.content[acc.content.length]acc.content[acc.content.length - 1]) that prevented the duplicate-role check from ever triggering.
  2. When consecutive function-role messages are detected, merges their functionResponse parts into the previous content entry instead of throwing. Gemini expects all functionResponse parts for a given model turn to live in a single user turn.

@mossein
Copy link
Author

cc Hunter Lovell (@hntrl) Jacob Lee (@jacoblee93), this fixes a bug that causes Gemini to hang when LangChain sends parallel tool results as separate user turns instead of merging them into one. Happy to adjust anything if needed.

@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2026

🦋 Changeset detected

Latest commit: de97364

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@langchain/google-genai Patch
@langchain/google-common Patch
@langchain/google-gauth Patch
@langchain/google-webauth Patch
@langchain/google-vertexai Patch
@langchain/google-vertexai-web Patch

Not sure what this means? Click here to learn what changesets are.

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

@pawel-twardziak
Copy link
Contributor

hi Mohammad Jafari (@mossein)

great work! I think it would be good to use actualRole in the merge condition, e.g. compute actualRole before the prevContent check and use prevContent.role === actualRole and then role === "function" to merge

and add unit tests

Move actualRole computation before the duplicate-role check so
prevContent.role is compared against actualRole (the mapped API role)
rather than the raw internal role. Add unit tests for merging
consecutive ToolMessages (parallel tool calls).
@mossein
Copy link
Author

hi Mohammad Jafari (Mohammad Jafari (@mossein))

great work! I think it would be good to use actualRole in the merge condition, e.g. compute actualRole before the prevContent check and use prevContent.role === actualRole and then role === "function" to merge

and add unit tests

Good call pawel-twardziak. I moved the actualRole computation before the prevContent check so the comparison uses the mapped API role.
Also added unit tests covering parallel tool calls with 2 and 3 consecutive ToolMessages. Let me know if anything else needs adjusting.

@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2026

⚠️ No Changeset found

Latest commit: 15ad50d

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

@pawel-twardziak
Copy link
Contributor

LGTM, thanks Mohammad Jafari (@mossein) 💪

@mossein
Copy link
Author

Hey Christian Bromann (@christian-bromann) - this fixes the same class of bug as #10343 (which was merged) but for the @langchain/google-genai package. Would love a review when you get a chance!

Copy link
Member

@hntrl Hunter Lovell (hntrl) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix!

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

3 participants