fix(google-genai): merge consecutive function-role messages in convertBaseMessagesToContent#10345
Conversation
…tBaseMessagesToContent
|
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 detectedLatest commit: de97364 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
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 |
|
great work! I think it would be good to use 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).
Good call pawel-twardziak. I moved the actualRole computation before the prevContent check so the comparison uses the mapped API role. |
|
|
LGTM, thanks Mohammad Jafari (@mossein) 💪 |
|
Hey Christian Bromann (@christian-bromann) - this fixes the same class of bug as #10343 (which was merged) but for the |
Hunter Lovell (hntrl)
left a comment
There was a problem hiding this comment.
thanks for the fix!
Fixes #10342
When multiple tool calls are executed in parallel,
convertBaseMessagesToContentreceives consecutiveToolMessages that all map to thefunctionrole. The existing code throws "Google Generative AI requires alternate messages between authors" instead of merging them.This patch:
acc.content[acc.content.length]→acc.content[acc.content.length - 1]) that prevented the duplicate-role check from ever triggering.function-role messages are detected, merges theirfunctionResponseparts into the previous content entry instead of throwing. Gemini expects allfunctionResponseparts for a given model turn to live in a single user turn.