mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +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:
parent
2d39825868
commit
56386969b5
2 changed files with 3 additions and 5 deletions
|
|
@ -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():
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue