Skip to content

fix(gemini): add missing role="user" to function response content blocks#22046

Merged
Chesars merged 2 commits intoBerriAI:litellm_oss_staging_02_28_2026from
Chesars:fix/gemini-function-response-role
Feb 28, 2026
Merged

fix(gemini): add missing role="user" to function response content blocks#22046
Chesars merged 2 commits intoBerriAI:litellm_oss_staging_02_28_2026from
Chesars:fix/gemini-function-response-role

Conversation

@Chesars
Copy link
Collaborator

@Chesars Chesars commented Feb 25, 2026

Relevant issues

Fixes #22003
Fixes #20690
Ref #17949

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

Type

🐛 Bug Fix

Changes

Gemini API only accepts two roles: "user" and "model". Function responses
must be sent with role: "user" per the
Gemini API spec.

_gemini_convert_messages_with_history() was appending function response
ContentType blocks without a role field (2 call sites), causing Gemini to
reject multi-turn tool-calling conversations with:

The fix adds role="user" to both ContentType(parts=tool_call_responses)
call sites in transformation.py.

Verified against the live Gemini API with a multi-turn function calling
conversation.

@vercel
Copy link

vercel bot commented Feb 25, 2026

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

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 28, 2026 4:14pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 25, 2026

Greptile Summary

This PR fixes a bug where _gemini_convert_messages_with_history() was constructing ContentType blocks for tool/function responses without a role field. The Gemini API requires every content block to have role: "user" or role: "model", and function responses must use role: "user" per the Gemini API spec. The missing role caused 400 INVALID_ARGUMENT errors during multi-turn tool-calling conversations.

Confidence Score: 5/5

  • This PR is safe to merge — it's a minimal, well-scoped bug fix with clear evidence of correctness.
  • The change is a two-line fix adding a required field (role="user") to two call sites, matching the Gemini API spec and the existing pattern used by all other ContentType usages in the same function. The fix is well-tested with two mock-only unit tests, verified against the live API per the PR description, and no regressions are likely since the field was simply missing before.
  • No files require special attention

Important Files Changed

Filename Overview
litellm/llms/vertex_ai/gemini/transformation.py Adds role="user" to both ContentType(parts=tool_call_responses) call sites (lines 503 and 513), fixing Gemini API 400 errors on multi-turn tool-calling conversations. The fix is minimal, correct, and consistent with the Gemini API spec.
tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py Adds two well-structured mock-only tests covering single-turn and multi-turn function calling scenarios, asserting that all function response content blocks have role="user". Minor: missing trailing newline.

Sequence Diagram

sequenceDiagram
    participant User as User Message
    participant LiteLLM as _gemini_convert_messages_with_history()
    participant Gemini as Gemini API

    User->>LiteLLM: messages (OpenAI format with tool results)
    Note over LiteLLM: Convert user content → ContentType(role="user")
    Note over LiteLLM: Convert assistant content → ContentType(role="model")
    Note over LiteLLM: Convert tool responses → ContentType(role="user") ← FIX
    LiteLLM->>Gemini: contents[] with all roles set
    Gemini-->>LiteLLM: Valid response (no 400 error)
Loading

Last reviewed commit: 4566a02

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.

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@shin-bot-litellm
Copy link
Contributor

Review

1. Does this PR fix the issue it describes?
Yes. Fixes #22003 and #20690 — Gemini was rejecting multi-turn tool-calling conversations with "Please use a valid role: user, model" errors.

The root cause: ContentType(parts=tool_call_responses) was created without a role field. Per Gemini API spec, function responses must have role="user".

The fix adds role="user" to both callsites in transformation.py. Verified against live Gemini API.

2. Has this issue already been solved elsewhere?
No. Searched for other Gemini function response handling — this is the correct place to fix it. The two issues (#22003, #20690) had been open without resolution.

✅ LGTM — minimal, correct fix with test coverage and live API verification.

@jquinter
Copy link
Contributor

@Chesars can you rebase with main? most of tests failed

Gemini API only accepts "user" and "model" roles. Function responses were
being sent without a role field, causing 400 errors on multi-turn tool
calling conversations.

Fixes BerriAI#22003
Fixes BerriAI#20690
@Chesars Chesars force-pushed the fix/gemini-function-response-role branch from a07fb8f to 2a5ceec Compare February 28, 2026 16:12
@Chesars
Copy link
Collaborator Author

Chesars commented Feb 28, 2026

@Chesars can you rebase with main? most of tests failed

Rebased 👍

@jquinter
Copy link
Contributor

CI Failure: PLR0915 lint errors

These 3 PLR0915 "too many statements" errors are pre-existing on main and unrelated to this PR.

Fix PR: #22328

@Chesars Chesars changed the base branch from main to litellm_oss_staging_02_28_2026 February 28, 2026 18:49
@Chesars
Copy link
Collaborator Author

Chesars commented Feb 28, 2026

Thanks @jquinter

@Chesars Chesars merged commit 55c0fbf into BerriAI:litellm_oss_staging_02_28_2026 Feb 28, 2026
25 of 30 checks passed
@Chesars Chesars deleted the fix/gemini-function-response-role branch February 28, 2026 18:49
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-3 multi-turn function calling error [Bug]: Gemini function_response missing role="user" causes INVALID_ARGUMENT

3 participants