fix: escape unrecognized block types in errors

This commit is contained in:
Deepanshu Pal 2026-09-19 02:05:46 +05:30
parent acf253ef7a
commit a5ee60ec9e
2 changed files with 10 additions and 1 deletions

View file

@ -591,7 +591,7 @@ class LiteLLMAnthropicMessagesAdapter:
if isinstance(content, dict) and content.get("type") is not None
}
)
type_list = ", ".join(unrecognized_block_types) or "unknown"
type_list = ", ".join(json.dumps(block_type) for block_type in unrecognized_block_types) or "unknown"
raise litellm.BadRequestError(
message=f"Anthropic pass-through: every message's content blocks were of unrecognized types ({type_list}), so translation produced an empty conversation. Refusing to dispatch a request with no messages.",
model=model,

View file

@ -4890,6 +4890,15 @@ def test_translate_anthropic_to_openai_rejects_all_unrecognized_content_blocks()
with pytest.raises(litellm.BadRequestError, match="not_a_real_block"):
LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(request)
control_chars = {
**request,
"messages": [{"role": "user", "content": [{"type": "forged\nERROR: injected"}]}],
}
with pytest.raises(litellm.BadRequestError) as exc_info:
LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(control_chars)
assert "forged\\nERROR: injected" in str(exc_info.value)
assert "forged\nERROR: injected" not in str(exc_info.value)
with_system = {
**request,
"messages": [