From 56386969b5e032fc2ecf1921077fe65bca6bed2d Mon Sep 17 00:00:00 2001 From: Chesars Date: Thu, 19 Feb 2026 21:46:17 -0300 Subject: [PATCH] fix(anthropic): remove empty system messages from message list Empty system messages were skipped for Anthropic's system param but not removed from the messages list, causing BadRequestError when anthropic_messages_pt encountered the unsupported "system" role. Fixes #21622 --- litellm/llms/anthropic/chat/transformation.py | 6 +----- .../anthropic/chat/test_anthropic_chat_transformation.py | 2 ++ 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 364126d822..a9b1a2561a 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -1048,7 +1048,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): anthropic_system_message_list: List[AnthropicSystemMessageContent] = [] for idx, message in enumerate(messages): if message["role"] == "system": - valid_content: bool = False + system_prompt_indices.append(idx) system_message_block = ChatCompletionSystemMessage(**message) if isinstance(system_message_block["content"], str): # Skip empty text blocks - Anthropic API raises errors for empty text @@ -1068,7 +1068,6 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): anthropic_system_message_list.append( anthropic_system_message_content ) - valid_content = True elif isinstance(message["content"], list): for _content in message["content"]: # Skip empty text blocks - Anthropic API raises errors for empty text @@ -1092,10 +1091,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): anthropic_system_message_list.append( anthropic_system_message_content ) - valid_content = True - if valid_content: - system_prompt_indices.append(idx) if len(system_prompt_indices) > 0: for idx in reversed(system_prompt_indices): messages.pop(idx) 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 4b15d30cce..addaded9ec 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 @@ -1739,6 +1739,8 @@ def test_translate_system_message_skips_empty_string_content(): # Empty system message should produce no anthropic content blocks assert len(result) == 0 + # System message must be removed from messages so it doesn't reach anthropic_messages_pt + assert all(m["role"] != "system" for m in messages) def test_translate_system_message_skips_empty_list_content():