From 4c5f077bd3e808637ad343ffc536a1959b743065 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Wed, 19 Jun 2024 18:47:08 -0700 Subject: [PATCH] fix(vertex_ai.py): check if message length > 0 before merging --- litellm/llms/vertex_ai.py | 7 +++++-- litellm/tests/test_amazing_vertex_completion.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/litellm/llms/vertex_ai.py b/litellm/llms/vertex_ai.py index 392be24a857..1dbd93048d3 100644 --- a/litellm/llms/vertex_ai.py +++ b/litellm/llms/vertex_ai.py @@ -337,7 +337,7 @@ def _gemini_convert_messages_with_history(messages: list) -> List[ContentType]: _parts: List[PartType] = [] for element in messages[msg_i]["content"]: if isinstance(element, dict): - if element["type"] == "text": + if element["type"] == "text" and len(element["text"]) > 0: _part = PartType(text=element["text"]) _parts.append(_part) elif element["type"] == "image_url": @@ -345,7 +345,10 @@ def _gemini_convert_messages_with_history(messages: list) -> List[ContentType]: _part = _process_gemini_image(image_url=image_url) _parts.append(_part) # type: ignore user_content.extend(_parts) - else: + elif ( + isinstance(messages[msg_i]["content"], str) + and len(messages[msg_i]["content"]) > 0 + ): _part = PartType(text=messages[msg_i]["content"]) user_content.append(_part) diff --git a/litellm/tests/test_amazing_vertex_completion.py b/litellm/tests/test_amazing_vertex_completion.py index 4edc580a9b6..742e0388019 100644 --- a/litellm/tests/test_amazing_vertex_completion.py +++ b/litellm/tests/test_amazing_vertex_completion.py @@ -570,7 +570,6 @@ async def test_gemini_pro_vision(provider, sync_mode): # Google counts the prompt tokens for us, we should ensure we use the tokens from the orignal response assert prompt_tokens == 263 # the gemini api returns 263 to us - assert False except litellm.RateLimitError as e: pass except Exception as e: @@ -1164,6 +1163,7 @@ def test_gemini_pro_vision_async(): resp = await litellm.acompletion( model="vertex_ai/gemini-pro-vision", messages=[ + {"role": "system", "content": ""}, { "role": "user", "content": [ @@ -1175,7 +1175,7 @@ def test_gemini_pro_vision_async(): }, }, ], - } + }, ], ) print("async response gemini pro vision")