From a5ee60ec9e7e39cf53e288d576bec615dcd5a3de Mon Sep 17 00:00:00 2001 From: Deepanshu Pal <40927968+DeepanshuPal@users.noreply.github.com> Date: Sat, 19 Sep 2026 02:05:46 +0530 Subject: [PATCH] fix: escape unrecognized block types in errors --- .../experimental_pass_through/adapters/transformation.py | 2 +- ..._experimental_pass_through_adapters_transformation.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index 3e1fd097074..4ded2aa4343 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -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, diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py index bb205762dab..faf4ec3734d 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py @@ -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": [