diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index d46714a0c6b..0f613ceb504 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -1064,7 +1064,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 @@ -1084,7 +1084,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 @@ -1108,10 +1107,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 357a3bf42ea..8ebdfcb47bc 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():