Skip to content

Conversation

@Mr-Neutr0n
Copy link

Summary

  • Adds missing role="user" when constructing ContentType for function responses in Gemini/Vertex AI transformation
  • Without this role, multi-turn function calling fails with INVALID_ARGUMENT (400) from the Google API

The Bug

When using Gemini models with multi-turn function calling, LiteLLM fails to include role="user" when constructing the function_response part of the payload.

Error: 400 INVALID_ARGUMENT from Google API

Changes

File: litellm/llms/vertex_ai/gemini/transformation.py

Two locations where tool_call_responses are appended without role:

# Before (line 482)
contents.append(ContentType(parts=tool_call_responses))

# After
contents.append(ContentType(role="user", parts=tool_call_responses))
# Before (line 492)  
contents.append(ContentType(parts=tool_call_responses))

# After
contents.append(ContentType(role="user", parts=tool_call_responses))

Test plan

  • Test multi-turn function calling with Gemini models
  • Verify no 400 INVALID_ARGUMENT errors

Fixes #20690

🤖 Generated with Claude Code

@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 9, 2026 0:52am

Request Review

@CLAassistant
Copy link

CLAassistant commented Feb 8, 2026

CLA assistant check
All committers have signed the CLA.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 8, 2026

Greptile Overview

Greptile Summary

This PR updates the Gemini/Vertex message transformation so that accumulated tool/function responses are wrapped in a ContentType with role="user" before being appended to contents. This aligns tool result turns with the role expectations of the Google APIs and avoids multi-turn function-calling failures caused by missing roles.

The change is localized to _gemini_convert_messages_with_history() in litellm/llms/vertex_ai/gemini/transformation.py, affecting only the payload constructed for /generateContent requests.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk.
  • The change is a narrow, mechanical adjustment to request payload construction (adding an explicit role on tool response content) with no side effects outside Gemini/Vertex message transformation. It fixes a concrete API contract issue and does not alter control flow or data beyond the required field.
  • No files require special attention

Important Files Changed

Filename Overview
litellm/llms/vertex_ai/gemini/transformation.py Adds explicit role="user" on ContentType entries created for accumulated tool/function responses to satisfy Gemini/Vertex multi-turn function-calling requirements.

Sequence Diagram

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

  Client->>Tx: _transform_request_body(messages, model,...)
  Tx->>Tx: _gemini_convert_messages_with_history(messages)
  loop iterate messages
    Tx->>Tx: convert_to_gemini_tool_call_invoke(assistant_msg)
    Tx->>Tx: convert_to_gemini_tool_call_result(tool/function msg)
    Tx->>Tx: contents.append(ContentType(role="user", parts=tool_call_responses))
  end
  Tx->>Gemini: POST /generateContent { contents: [...] }
  Gemini-->>Client: 200 response (no INVALID_ARGUMENT on missing role)

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, no comments

Edit Code Review Agent Settings | Greptile

@krrishdholakia
Copy link
Member

@Mr-Neutr0n please add a unit test for this

@Mr-Neutr0n
Copy link
Author

I]recheck

@Mr-Neutr0n
Copy link
Author

recheck

Mr-Neutr0n and others added 2 commits February 8, 2026 17:22
Add missing role="user" when constructing ContentType for function
responses in Gemini/Vertex AI. Without this role, multi-turn function
calling fails with INVALID_ARGUMENT (400) from the Google API.

Fixes BerriAI#20690

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add two tests to verify that function_response ContentType entries
have role='user' as required by the Gemini API:

- test_function_response_has_user_role: Tests single tool response
- test_multiple_function_responses_have_user_role: Tests parallel tool calls

Relates to BerriAI#20690
@Mr-Neutr0n Mr-Neutr0n force-pushed the fix/gemini-function-response-role branch from 8dcb8b6 to f3dcc25 Compare February 8, 2026 11:52
@Mr-Neutr0n
Copy link
Author

recheck

@Mr-Neutr0n
Copy link
Author

@krrishdholakia Added unit tests in the second commit (f3dcc25). The tests verify that:

  1. function_response ContentType entries have role="user" when converting tool messages
  2. Multiple consecutive tool responses are correctly grouped with role="user"
  3. Non-tool messages are unaffected

All CI checks pass (lint, unit-test, MCP tests, Helm tests). The mock test suite is running.

@Mr-Neutr0n
Copy link
Author

recheck

@Mr-Neutr0n
Copy link
Author

@krrishdholakia Are we safe to merge?

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.

[Bug]: Gemini function_response missing role="user" causes INVALID_ARGUMENT

3 participants