The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(anthropic): remove empty system messages from message list
Chesars56386969b5e0
diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.pyindex 364126d822e..a9b1a2561a3 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 = Trueelif 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.pyindex 4b15d30ccec..addaded9ec9 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 blocksassert 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():