Merge pull request #21630 from Chesars/fix/empty-system-message-anthropic

fix(anthropic): empty system messages in translate_system_message
This commit is contained in:
Sameer Kankute 2026-02-20 09:32:50 +05:30 committed by GitHub
commit fb75a7130f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 3 additions and 5 deletions

View file

@ -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)

View file

@ -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():