fix(vertex): ToolMessage is translated to function role which does not exist .
          #1238
        
          
      
  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.
  
    
  
    
Description
This PR resolves issue #1059 where the LangChain adapter for Google Gemini incorrectly translates the
ToolMessage(and the legacyFunctionMessage) into a GeminiContentobject using the role“function”.Whereas "according to gemini docs, "function" is not a valid role , only
userandmodelare accepted valid roles.Relevant issues
Fixes #1059
Type
🐛 Bug Fix
🧹 Refactoring
Changes(optional)
FunctionMessageandToolMessagefrom the invalid“function”to the API-compliant“user”and**model**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.