From f2a6765f21212912d2ca14c8764519950b1311c7 Mon Sep 17 00:00:00 2001 From: Pawan-Shahane Date: Sun, 20 Sep 2026 16:38:34 +0530 Subject: [PATCH] refactor(vertex_ai): make the system message part helper total The role guard was unreachable, since only the leading run of system messages ever reaches the helper, and returning None silently dropped that message from the request instead of leaving it in place. Returning an empty part for content that is neither a string nor a list covers both, and removes the two lines the patch coverage report flagged. --- .../llms/vertex_ai/gemini/transformation.py | 16 +++++++------- .../test_vertex_ai_gemini_transformation.py | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/litellm/llms/vertex_ai/gemini/transformation.py b/litellm/llms/vertex_ai/gemini/transformation.py index 5504e8d50f4..7567afd8a34 100644 --- a/litellm/llms/vertex_ai/gemini/transformation.py +++ b/litellm/llms/vertex_ai/gemini/transformation.py @@ -8,7 +8,6 @@ import json import os import re from collections.abc import Mapping -from itertools import takewhile from typing import TYPE_CHECKING, Any, Final, Literal, cast from urllib.parse import quote @@ -43,6 +42,7 @@ from litellm.types.llms.openai import ( ChatCompletionAudioObject, ChatCompletionFileObject, ChatCompletionImageObject, + ChatCompletionSystemMessage, ChatCompletionTextObject, ChatCompletionUserMessage, ) @@ -1406,15 +1406,13 @@ def _default_user_message_when_system_message_passed() -> ChatCompletionUserMess return ChatCompletionUserMessage(content=".", role="user") -def _system_message_to_part(message: AllMessageValues) -> PartType | None: - if message["role"] != "system": - return None +def _system_message_to_part(message: ChatCompletionSystemMessage) -> PartType: content: Final = message["content"] if isinstance(content, str): return PartType(text=content) if isinstance(content, list): return PartType(text="".join(block.get("text") or "" for block in content)) - return None + return PartType(text="") def _transform_system_message( @@ -1435,9 +1433,11 @@ def _transform_system_message( if supports_system_message is not True: return None, messages - leading_system_messages: Final = tuple(takewhile(lambda message: message["role"] == "system", messages)) + leading_count: Final = next( + (index for index, message in enumerate(messages) if message["role"] != "system"), len(messages) + ) system_content_parts: Final = tuple( - part for part in map(_system_message_to_part, leading_system_messages) if part is not None + _system_message_to_part(message) for message in messages[:leading_count] if message["role"] == "system" ) if len(system_content_parts) == 0: return None, messages @@ -1446,5 +1446,5 @@ def _transform_system_message( # If no messages are left, add a blank user message # Relevant Issue - https://github.com/BerriAI/litellm/issues/13769 ######################################################### - remaining: Final = messages[len(leading_system_messages) :] or (_default_user_message_when_system_message_passed(),) + remaining: Final = messages[leading_count:] or (_default_user_message_when_system_message_passed(),) return SystemInstructions(parts=list(system_content_parts)), list(remaining) diff --git a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py index 1f8b143d618..8c5eeea6a60 100644 --- a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py +++ b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py @@ -2873,3 +2873,24 @@ def test_mid_conversation_system_after_tool_result_keeps_tool_ordering(): if {"text": "Reminder: reply in one short sentence."} in content["parts"] ) assert tool_result_index < reminder_index + + +def test_leading_system_message_with_malformed_content_does_not_drop_the_conversation(): + """A system message whose content is neither text nor a content list must not raise or swallow the user turn.""" + body = _gemini_request_body( + [ + {"role": "system", "content": None}, + {"role": "user", "content": "hello"}, + ] + ) + + assert body["contents"] == [{"role": "user", "parts": [{"text": "hello"}]}] + + +def test_system_only_request_still_sends_a_user_turn(): + """Gemini requires contents, so a request carrying only system messages keeps a placeholder user turn.""" + body = _gemini_request_body([{"role": "system", "content": "You are helpful"}]) + + assert body["system_instruction"]["parts"] == [{"text": "You are helpful"}] + assert len(body["contents"]) == 1 + assert body["contents"][0]["role"] == "user"