mirror of
https://github.com/tiennm99/litellm.git
synced 2026-07-04 15:07:22 +00:00
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
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user