Skip to content

Conversation

@RONAK-AI647
Copy link
Contributor

Description

This PR resolves issue #1059 where the LangChain adapter for Google Gemini incorrectly translates the ToolMessage (and the legacy FunctionMessage) into a Gemini Content object using the role “function”.
Whereas "according to gemini docs, "function" is not a valid role , only user and model are accepted valid roles.

Relevant issues

Fixes #1059

Type

🐛 Bug Fix
🧹 Refactoring

Changes(optional)

  1. Role Correction: Changed the assigned role for both FunctionMessage and ToolMessage from the invalid “function” to the API-compliant “user” and **model**
  2. Logic Correction: Updated the conditional logic in the message merging section to check for prev_content.role == "user". This ensures that consecutive tool responses are correctly merged into a single “user” turn, maintaining the strict alternating sequence required by the Gemini chat models.

Testing(optional)

Note(optional)

This ensures tool-calling flows are correctly translated and accepted by the Gemini API, especially when the agent is passing tool results back to the model for the final time.
This will not throw the

WARNING - langchain_core.language_models.llms - Retrying langchain_google_vertexai.chat_models._acompletion_with_retry.<locals>._completion_with_retry_inner in 4.0 seconds as it raised BadRequest: 400 POST https://.../models/gemini-2.5-flash:generateContent: Please use a valid role: user, model.. now.

@RONAK-AI647 RONAK-AI647 changed the title fix(vertexai): ToolMessage is translated to function role which does not exist . fix(vertex): ToolMessage is translated to function role which does not exist . Oct 10, 2025
@RONAK-AI647 RONAK-AI647 marked this pull request as draft October 10, 2025 10:34
…ting chatmodel.py again to change the variable so the merging of messges happen to 6==6.
@RONAK-AI647 RONAK-AI647 marked this pull request as ready for review October 10, 2025 11:10
@RONAK-AI647 RONAK-AI647 marked this pull request as draft October 10, 2025 11:11
@RONAK-AI647 RONAK-AI647 marked this pull request as ready for review October 10, 2025 11:15
@RONAK-AI647
Copy link
Contributor Author

@lkuligin @mdrxy , take a look.

@lkuligin lkuligin merged commit fb85af6 into langchain-ai:main Oct 15, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ToolMessage is translated to function role which does not exist

2 participants