From 8eba5e380f1252df29c3a16d275c5693a2a7a47d 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:20:11 +0800 Subject: [PATCH] test: add unit test for the fix --- .../test_anthropic_chat_transformation.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py index 071cd277c67..c42f62007b9 100644 --- a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py +++ b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py @@ -1808,6 +1808,42 @@ def test_translate_system_message_preserves_cache_control(): assert result[0]["cache_control"] == {"type": "ephemeral"} +def test_anthropic_messages_pt_skips_empty_user_text_blocks(): + """ + Test that anthropic_messages_pt filters out empty text blocks in user messages. + + Fixes: Vertex AI Anthropic API error "messages: text content blocks must be non-empty" + when user messages contain list content with empty text blocks. + """ + from litellm.litellm_core_utils.prompt_templates.factory import anthropic_messages_pt + + messages = [ + { + "role": "user", + "content": [ + {"type": "text", "text": ""}, + {"type": "text", "text": "Hello"}, + {"type": "text", "text": ""}, + ], + } + ] + + result = anthropic_messages_pt( + messages=messages, + model="claude-3-5-sonnet-20241022", + llm_provider="anthropic", + ) + + assert len(result) == 1 + assert result[0]["role"] == "user" + content = result[0]["content"] + assert isinstance(content, list) + # Only the non-empty block should remain + assert len(content) == 1 + assert content[0]["type"] == "text" + assert content[0]["text"] == "Hello" + + # ============ Dynamic max_tokens Tests ============