fix: reject untranslated conversations with system messages

This commit is contained in:
Deepanshu Pal 2026-09-14 22:21:22 +05:30
parent eab82b6f35
commit 33e13fafa2
2 changed files with 12 additions and 3 deletions

View file

@ -580,7 +580,8 @@ class LiteLLMAnthropicMessagesAdapter:
new_messages.append(assistant_message)
has_conversational_input = any(m.get("role") != "system" for m in replayable_messages)
if has_conversational_input and len(new_messages) == 0:
has_translated_conversation = any(m.get("role") != "system" for m in new_messages)
if has_conversational_input and not has_translated_conversation:
raise litellm.BadRequestError(
message="Anthropic pass-through: every message's content blocks were of unrecognized types, so translation produced an empty conversation. Refusing to dispatch a request with no messages.",
model=model,

View file

@ -4874,7 +4874,6 @@ def test_redacted_thinking_blocks_never_carry_cache_control():
def test_translate_anthropic_to_openai_rejects_all_unrecognized_content_blocks():
"""A request whose blocks are all unknown must not dispatch an empty conversation (#41091)."""
import pytest
import litellm
@ -4891,7 +4890,16 @@ def test_translate_anthropic_to_openai_rejects_all_unrecognized_content_blocks()
with pytest.raises(litellm.BadRequestError):
LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(request)
# A recognized block alongside an unrecognized one still translates normally.
with_system = {
**request,
"messages": [
{"role": "system", "content": "keep this"},
*request["messages"],
],
}
with pytest.raises(litellm.BadRequestError):
LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(with_system)
mixed = {
"model": "openai/gpt-4o",
"max_tokens": 1024,