fix(anthropic): return 400 for a content list holding non-dict items

Building the Anthropic beta headers walks each message's content list and
membership-tests or indexes every item, assuming a dict. A list holding a bare
string, a null or a number therefore raised TypeError from inside header
construction, which the caller saw as a 500 with no mention of their payload

Three helpers on the validate_environment path each failed on a different
input, so guarding one only moves the crash: a string containing "type" broke
is_pdf_used, a string without it broke get_file_ids_from_messages, and a null
broke is_cache_control_set. Skip non-dict items in all three, which lets the
request reach the normal transformation and come back as a 400

A bare-string content list is not valid per the OpenAI message schema, so
rejecting it is right; doing it as an internal error was not
This commit is contained in:
Pawan-Shahane 2026-09-20 11:58:13 +05:30
parent 58065d46fd
commit 9527fe931d
3 changed files with 64 additions and 3 deletions

View file

@ -1668,7 +1668,7 @@ def get_file_ids_from_messages(messages: list[AllMessageValues]) -> list[str]:
if isinstance(content, str):
continue
for c in content:
if c["type"] == "file":
if isinstance(c, dict) and c["type"] == "file":
file_object = cast(ChatCompletionFileObject, c)
file_object_file_field = file_object.get("file")
if not isinstance(file_object_file_field, dict):

View file

@ -291,7 +291,7 @@ class AnthropicModelInfo(BaseLLMModelInfo):
_message_content = message.get("content")
if _message_content is not None and isinstance(_message_content, list):
for content in _message_content:
if "cache_control" in content:
if isinstance(content, dict) and "cache_control" in content:
return True
return False
@ -336,7 +336,7 @@ class AnthropicModelInfo(BaseLLMModelInfo):
for message in messages:
if "content" in message and message["content"] is not None and isinstance(message["content"], list):
for content in message["content"]:
if "type" in content and content["type"] != "text":
if isinstance(content, dict) and "type" in content and content["type"] != "text":
return True
return False

View file

@ -2275,3 +2275,64 @@ def test_create_anthropic_model_list_response_lists_ids_as_told():
assert (gpt["id"], gpt["display_name"], gpt["max_input_tokens"]) == ("claude-router-gpt-4o[1m]", "GPT 4o", 1000000)
assert (haiku["id"], haiku["display_name"]) == ("claude-haiku-4-5", "claude-haiku-4-5")
assert (response["first_id"], response["last_id"]) == ("claude-router-gpt-4o[1m]", "claude-haiku-4-5")
class TestMalformedContentListItems:
"""A `content` list holding bare strings is not valid per the OpenAI message
schema, but it reaches the Anthropic beta-header helpers before any validation
runs. Those helpers membership-test each item, so a non-dict item turned into a
substring test and then an unhandled TypeError, surfacing as a 500."""
@pytest.mark.parametrize(
"content",
[
pytest.param(["what type of file is this?"], id="string_containing_type"),
pytest.param(["how do I set cache_control?"], id="string_containing_cache_control"),
pytest.param([None], id="none_item"),
pytest.param([5], id="int_item"),
pytest.param([["nested"]], id="list_item"),
],
)
def test_beta_headers_resolve_for_non_dict_content_items(self, content):
"""The header path completes instead of raising, so the caller gets the
provider's own validation error rather than an internal failure."""
from litellm.llms.anthropic.common_utils import AnthropicModelInfo
config = AnthropicModelInfo()
messages = [{"role": "user", "content": content}]
headers = config.validate_environment(
headers={},
model="claude-sonnet-4-5",
messages=messages,
optional_params={},
litellm_params={},
api_key=FAKE_REGULAR_KEY,
)
assert headers["x-api-key"] == FAKE_REGULAR_KEY
def test_real_content_parts_still_set_their_beta_headers(self):
"""The guard skips malformed items only; a genuine media part must still be
detected, otherwise the pdf beta header would silently stop being sent."""
from litellm.llms.anthropic.common_utils import AnthropicModelInfo
config = AnthropicModelInfo()
assert config.is_pdf_used([{"role": "user", "content": [{"type": "image", "source": {}}]}]) is True
assert config.is_pdf_used([{"role": "user", "content": [{"type": "text", "text": "hi"}]}]) is False
assert (
config.is_cache_control_set(
[{"role": "user", "content": [{"type": "text", "text": "hi", "cache_control": {"type": "ephemeral"}}]}]
)
is True
)
def test_mixed_list_keeps_detecting_the_valid_part(self):
"""A malformed item earlier in the list must not mask a real media part after it."""
from litellm.llms.anthropic.common_utils import AnthropicModelInfo
config = AnthropicModelInfo()
messages = [{"role": "user", "content": ["what type of file is this?", {"type": "image", "source": {}}]}]
assert config.is_pdf_used(messages) is True