From 5e1e25925dc41e9deab537cbca8dd487d2cf4c7d Mon Sep 17 00:00:00 2001 From: HootRock Date: Fri, 23 Oct 2026 09:45:43 +0800 Subject: [PATCH 1/2] fix: classify malformed completion inputs as bad requests --- litellm/main.py | 6 ++++-- litellm/utils.py | 6 ++++-- tests/unit/test_main.py | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/litellm/main.py b/litellm/main.py index 27700cf9def..9d0662f97d8 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -7945,8 +7945,10 @@ def text_completion( # Support for token IDs as prompt (list of integers or list of lists of integers) messages = [{"role": "user", "content": prompt}] else: - raise Exception( - f"Unmapped prompt format. Your prompt is neither a list of strings nor a string. prompt={prompt}. File an issue - https://github.com/BerriAI/litellm/issues" + raise litellm.BadRequestError( + message=f"Unmapped prompt format. Your prompt is neither a list of strings nor a string. prompt={prompt}. File an issue - https://github.com/BerriAI/litellm/issues", + model=model or "", + llm_provider=custom_llm_provider or "", ) kwargs.pop("prompt", None) diff --git a/litellm/utils.py b/litellm/utils.py index 7de7ce6188d..d089af20854 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -9027,8 +9027,10 @@ def validate_chat_completion_user_messages(messages: list[AllMessageValues]): f"Invalid message at index {idx}. Please ensure all messages are valid OpenAI chat completion messages." ) if "invalid content type" in str(e): - raise Exception( - f"Invalid user message at index {idx}. Please ensure all user messages are valid OpenAI chat completion messages." + raise BadRequestError( + message=f"Invalid user message at index {idx}. Please ensure all user messages are valid OpenAI chat completion messages.", + model="", + llm_provider="", ) else: raise e diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index c3d3575b8b3..e5aaa419a3a 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -9814,3 +9814,48 @@ async def test_converse_fallback_prices_the_current_attempt( else: client.close() configuration.reset_rust_configuration() + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.asyncio +async def test_unknown_chat_content_is_a_non_retryable_bad_request(asynchronous: bool) -> None: + from litellm.proxy.common_request_processing import proxy_exception_from_route_error + + messages: Final = [{"role": "user", "content": [{"type": "text", "text": "hi"}, {"type": "future_block"}]}] + with pytest.raises(litellm.BadRequestError, match="Invalid user message at index 0") as raised: + if asynchronous: + await litellm.acompletion(model="hosted_vllm/local-qa", messages=messages, api_key="local-fixture") + else: + litellm.completion(model="hosted_vllm/local-qa", messages=messages, api_key="local-fixture") + + assert raised.value.status_code == 400 + assert not litellm.should_retry(raised.value.status_code) + mapped: Final = proxy_exception_from_route_error(raised.value) + assert mapped.type == "invalid_request_error" + assert mapped.code == "400" + with pytest.raises(litellm.BadRequestError) as router_error: + litellm.Router().should_retry_this_error(raised.value, healthy_deployments=[{}]) + assert router_error.value is raised.value + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.asyncio +async def test_object_text_prompt_is_a_non_retryable_bad_request(asynchronous: bool) -> None: + from litellm.proxy.common_request_processing import proxy_exception_from_route_error + + with pytest.raises(litellm.BadRequestError, match="Unmapped prompt format") as raised: + if asynchronous: + await litellm.atext_completion( + model="hosted_vllm/local-qa", prompt={"not": "a prompt"}, api_key="local-fixture" + ) + else: + litellm.text_completion(model="hosted_vllm/local-qa", prompt={"not": "a prompt"}, api_key="local-fixture") + + assert raised.value.status_code == 400 + assert not litellm.should_retry(raised.value.status_code) + mapped: Final = proxy_exception_from_route_error(raised.value) + assert mapped.type == "invalid_request_error" + assert mapped.code == "400" + with pytest.raises(litellm.BadRequestError) as router_error: + litellm.Router().should_retry_this_error(raised.value, healthy_deployments=[{}]) + assert router_error.value is raised.value From 16db0190c8b8ba8dd4b807fe39321e4266506c42 Mon Sep 17 00:00:00 2001 From: HootRock Date: Fri, 23 Oct 2026 09:50:56 +0800 Subject: [PATCH 2/2] fix: retain validation error type and field metadata --- litellm/main.py | 1 + litellm/utils.py | 1 + tests/unit/test_main.py | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/litellm/main.py b/litellm/main.py index 9d0662f97d8..45fe2a7aef4 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -7949,6 +7949,7 @@ def text_completion( message=f"Unmapped prompt format. Your prompt is neither a list of strings nor a string. prompt={prompt}. File an issue - https://github.com/BerriAI/litellm/issues", model=model or "", llm_provider=custom_llm_provider or "", + body={"type": "invalid_request_error", "param": "prompt"}, ) kwargs.pop("prompt", None) diff --git a/litellm/utils.py b/litellm/utils.py index d089af20854..65e4d18dfd7 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -9031,6 +9031,7 @@ def validate_chat_completion_user_messages(messages: list[AllMessageValues]): message=f"Invalid user message at index {idx}. Please ensure all user messages are valid OpenAI chat completion messages.", model="", llm_provider="", + body={"type": "invalid_request_error", "param": f"messages[{idx}].content"}, ) else: raise e diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index e5aaa419a3a..6515d5dc9c4 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -9822,13 +9822,16 @@ async def test_unknown_chat_content_is_a_non_retryable_bad_request(asynchronous: from litellm.proxy.common_request_processing import proxy_exception_from_route_error messages: Final = [{"role": "user", "content": [{"type": "text", "text": "hi"}, {"type": "future_block"}]}] - with pytest.raises(litellm.BadRequestError, match="Invalid user message at index 0") as raised: - if asynchronous: + if asynchronous: + with pytest.raises(litellm.BadRequestError, match="Invalid user message at index 0") as raised: await litellm.acompletion(model="hosted_vllm/local-qa", messages=messages, api_key="local-fixture") - else: + else: + with pytest.raises(litellm.BadRequestError, match="Invalid user message at index 0") as raised: litellm.completion(model="hosted_vllm/local-qa", messages=messages, api_key="local-fixture") assert raised.value.status_code == 400 + assert raised.value.type == "invalid_request_error" + assert raised.value.param == "messages[0].content" assert not litellm.should_retry(raised.value.status_code) mapped: Final = proxy_exception_from_route_error(raised.value) assert mapped.type == "invalid_request_error" @@ -9843,15 +9846,18 @@ async def test_unknown_chat_content_is_a_non_retryable_bad_request(asynchronous: async def test_object_text_prompt_is_a_non_retryable_bad_request(asynchronous: bool) -> None: from litellm.proxy.common_request_processing import proxy_exception_from_route_error - with pytest.raises(litellm.BadRequestError, match="Unmapped prompt format") as raised: - if asynchronous: + if asynchronous: + with pytest.raises(litellm.BadRequestError, match="Unmapped prompt format") as raised: await litellm.atext_completion( model="hosted_vllm/local-qa", prompt={"not": "a prompt"}, api_key="local-fixture" ) - else: + else: + with pytest.raises(litellm.BadRequestError, match="Unmapped prompt format") as raised: litellm.text_completion(model="hosted_vllm/local-qa", prompt={"not": "a prompt"}, api_key="local-fixture") assert raised.value.status_code == 400 + assert raised.value.type == "invalid_request_error" + assert raised.value.param == "prompt" assert not litellm.should_retry(raised.value.status_code) mapped: Final = proxy_exception_from_route_error(raised.value) assert mapped.type == "invalid_request_error"