Skip to content

Commit 1a87c02

Browse files
authored
fix empty user message error (#15784)
1 parent d13d176 commit 1a87c02

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

litellm/llms/vertex_ai/common_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ def _check_text_in_content(parts: List[PartType]) -> bool:
239239
"""
240240
check that user_content has 'text' parameter.
241241
- Known Vertex Error: Unable to submit request because it must have a text parameter.
242-
- 'text' param needs to be len > 0
242+
- 'text' param needs to be present (empty strings are valid)
243243
- Relevant Issue: https://github.com/BerriAI/litellm/issues/5515
244244
"""
245245
has_text_param = False
246246
for part in parts:
247-
if "text" in part and part.get("text"):
247+
if "text" in part and part.get("text") is not None:
248248
has_text_param = True
249249

250250
return has_text_param

litellm/llms/vertex_ai/gemini/transformation.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ def _gemini_convert_messages_with_history( # noqa: PLR0915
263263
elif (
264264
_message_content is not None
265265
and isinstance(_message_content, str)
266-
and len(_message_content) > 0
267266
):
268267
_part = PartType(text=_message_content)
269268
user_content.append(_part)
@@ -335,9 +334,8 @@ def _gemini_convert_messages_with_history( # noqa: PLR0915
335334
elif (
336335
_message_content is not None
337336
and isinstance(_message_content, str)
338-
and _message_content
339337
):
340-
assistant_text = _message_content # either string or none
338+
assistant_text = _message_content
341339
assistant_content.append(PartType(text=assistant_text)) # type: ignore
342340

343341
## HANDLE ASSISTANT FUNCTION CALL

tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from litellm.llms.vertex_ai.gemini.transformation import (
22
check_if_part_exists_in_parts,
33
_transform_request_body,
4+
_gemini_convert_messages_with_history,
45
)
56

67

@@ -155,3 +156,23 @@ def test_metadata_to_labels_vertex_only():
155156
)
156157
assert "labels" in result
157158
assert result["labels"] == {"user": "john_doe", "project": "test-project"}
159+
160+
161+
def test_empty_content_handling():
162+
"""Test that empty content strings are properly handled in Gemini message transformation"""
163+
# Test with empty content in user message
164+
messages = [
165+
{
166+
"content": "",
167+
"role": "user"
168+
}
169+
]
170+
171+
contents = _gemini_convert_messages_with_history(messages=messages)
172+
173+
# Verify that the content was properly transformed
174+
assert len(contents) == 1
175+
assert contents[0]["role"] == "user"
176+
assert len(contents[0]["parts"]) == 1
177+
assert "text" in contents[0]["parts"][0]
178+
assert contents[0]["parts"][0]["text"] == ""

0 commit comments

Comments
 (0)