fix(gemini): add missing role="user" to function response content blocks#22046
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR fixes a bug where
Confidence Score: 5/5
|
| 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)
Last reviewed commit: 4566a02
tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
Outdated
Show resolved
Hide resolved
Review1. Does this PR fix the issue it describes? The root cause: The fix adds 2. Has this issue already been solved elsewhere? ✅ LGTM — minimal, correct fix with test coverage and live API verification. |
|
@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
a07fb8f to
2a5ceec
Compare
Rebased 👍 |
CI Failure: PLR0915 lint errorsThese 3 PLR0915 "too many statements" errors are pre-existing on main and unrelated to this PR. Fix PR: #22328 |
|
Thanks @jquinter |
55c0fbf
into
BerriAI:litellm_oss_staging_02_28_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
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitType
🐛 Bug Fix
Changes
Gemini API only accepts two roles:
"user"and"model". Function responsesmust be sent with
role: "user"per theGemini API spec.
_gemini_convert_messages_with_history()was appending function responseContentTypeblocks without arolefield (2 call sites), causing Gemini toreject multi-turn tool-calling conversations with:
"Please use a valid role: user, model."([Bug]: gemini-3 multi-turn function calling error #22003)INVALID_ARGUMENT400 ([Bug]: Gemini function_response missing role="user" causes INVALID_ARGUMENT #20690)The fix adds
role="user"to bothContentType(parts=tool_call_responses)call sites in
transformation.py.Verified against the live Gemini API with a multi-turn function calling
conversation.