Skip to content

fix(gemini): add missing role="user" to function_response (refs [#206…#20692

Closed
ZqinKing wants to merge 1 commit intoBerriAI:mainfrom
ZqinKing:fix/gemini-function-response-role
Closed

fix(gemini): add missing role="user" to function_response (refs [#206…#20692
ZqinKing wants to merge 1 commit intoBerriAI:mainfrom
ZqinKing:fix/gemini-function-response-role

Conversation

@ZqinKing
Copy link

@ZqinKing ZqinKing commented Feb 8, 2026

(#20690))

Relevant issues

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Type

🆕 New Feature
🐛 Bug Fix
🧹 Refactoring
📖 Documentation
🚄 Infrastructure
✅ Test

Changes

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@vercel
Copy link

vercel bot commented Feb 8, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 8, 2026 5:39am

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 8, 2026

Greptile Overview

Greptile Summary

This PR adjusts the Vertex/Gemini message transformation so that standalone tool/function response turns are emitted as ContentType(role="user", parts=...) instead of omitting the role.

The change is localized to _gemini_convert_messages_with_history() in litellm/llms/vertex_ai/gemini/transformation.py, which is responsible for converting OpenAI-style chat messages (including tool call results) into the contents list sent to Vertex AI / Gemini generateContent APIs.

Confidence Score: 4/5

  • This PR is low-risk and localized, but should not be merged without a regression test for the fixed tool/function response formatting.
  • The code change is minimal (adds an explicit role on two ContentType appends) and aligns with the ContentType schema, so runtime impact is likely positive. The main merge risk is lack of automated coverage for the specific failure mode reported in [Bug]: Gemini function_response missing role="user" causes INVALID_ARGUMENT #20690, so regressions could reappear unnoticed.
  • litellm/llms/vertex_ai/gemini/transformation.py (add test coverage for tool/function response roles)

Important Files Changed

Filename Overview
litellm/llms/vertex_ai/gemini/transformation.py Ensures tool/function response content blocks are emitted with role="user" when converting OpenAI messages to Gemini/Vertex contents; no regressions spotted in the transformation logic, but the PR needs a unit test to cover this formatting requirement.

Sequence Diagram

sequenceDiagram
  participant Client as LiteLLM Client
  participant Tx as gemini/transformation.py
  participant Vertex as Vertex/Gemini API

  Client->>Tx: OpenAI-format messages
  Note over Tx: _gemini_convert_messages_with_history()
  Tx->>Tx: Convert user/system messages -> Content(role="user")
  Tx->>Tx: Convert assistant messages -> Content(role="model")
  Tx->>Tx: Convert tool/function result -> Part(function_response)
  Tx->>Tx: Append tool/function response Content(role="user", parts=[...])
  Tx->>Vertex: RequestBody(contents=[Content...])
  Vertex-->>Tx: generateContent response
  Tx-->>Client: Mapped response
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines 479 to +482
messages[msg_i]["role"] not in tool_call_message_roles
):
if len(tool_call_responses) > 0:
contents.append(ContentType(parts=tool_call_responses))
contents.append(ContentType(role="user", parts=tool_call_responses))
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing regression test

This change fixes Vertex/Gemini request formatting by adding role="user" when appending function_response parts, but the PR doesn’t add a unit test to lock the behavior. Please add a test under tests/litellm/ that builds a message sequence with an assistant tool call followed by a tool/function role message, then asserts the transformed contents includes a ContentType with role == "user" for that function response turn.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@krrishdholakia
Copy link
Member

Fix merged into staging.

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.

3 participants