From 73bfc299f758a86b0c46d4ecf211b3cd74763e8b Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Wed, 17 Jul 2024 11:26:30 -0700 Subject: [PATCH] fix(factory.py): check if empty text block passed in (anthropic_messages_pt) --- litellm/llms/prompt_templates/factory.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/litellm/llms/prompt_templates/factory.py b/litellm/llms/prompt_templates/factory.py index 0299f37d530..fb37dacefc5 100644 --- a/litellm/llms/prompt_templates/factory.py +++ b/litellm/llms/prompt_templates/factory.py @@ -1288,13 +1288,18 @@ def anthropic_messages_pt( ): for m in messages[msg_i]["content"]: # handle text - if m.get("type", "") == "text": + if ( + m.get("type", "") == "text" and len(m.get("text", "")) > 0 + ): # don't pass empty text blocks. anthropic api raises errors. anthropic_message = AnthropicMessagesTextParam( type="text", text=m.get("text") ) assistant_content.append(anthropic_message) - elif "content" in messages[msg_i] and isinstance( - messages[msg_i]["content"], str + elif ( + "content" in messages[msg_i] + and isinstance(messages[msg_i]["content"], str) + and len(messages[msg_i]["content"]) + > 0 # don't pass empty text blocks. anthropic api raises errors. ): assistant_content.append( {"type": "text", "text": messages[msg_i]["content"]}