From 68ae8c87b4b3b69925fbfd26052541311dd2710f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=94=E6=B3=BD=E5=B0=94?= <3411596361@qq.com> Date: Sat, 28 Feb 2026 18:15:48 +0800 Subject: [PATCH] fix: add empty blcok in user messages --- litellm/litellm_core_utils/prompt_templates/factory.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index 796223ff8e1..45a85db1de2 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -2366,10 +2366,13 @@ def anthropic_messages_pt( # noqa: PLR0915 user_content.append(_anthropic_content_element) elif m.get("type", "") == "text": m = cast(ChatCompletionTextObject, m) + text_value = cast(str, m.get("text", "")) + if not text_value: # don't pass empty text blocks. anthropic api raises errors. + continue _anthropic_text_content_element = ( AnthropicMessagesTextParam( type="text", - text=m["text"], + text=text_value, ) ) _content_element = add_cache_control_to_content(