diff --git a/tests/entrypoints/openai/chat_completion/test_chat_error.py b/tests/entrypoints/openai/chat_completion/test_chat_error.py index 4b6be87ae5..4b42f522e8 100644 --- a/tests/entrypoints/openai/chat_completion/test_chat_error.py +++ b/tests/entrypoints/openai/chat_completion/test_chat_error.py @@ -515,3 +515,15 @@ def test_structured_outputs_structural_tag_invalid(structural_tag): messages=[{"role": "user", "content": "hello"}], structured_outputs={"structural_tag": structural_tag}, ) + + +@pytest.mark.parametrize("field_name", ["prompt_logprobs", "top_logprobs"]) +def test_non_numeric_logprobs_rejected(field_name): + """A non-numeric logprobs value must be a clean 400 validation error, not a + TypeError from the mode='before' comparison (which surfaces as HTTP 500).""" + with pytest.raises(ValidationError, match=f"`{field_name}` must be an integer"): + ChatCompletionRequest( + model=MODEL_NAME, + messages=[{"role": "user", "content": "hello"}], + **{field_name: "2"}, + ) diff --git a/tests/entrypoints/openai/completion/test_completion_error.py b/tests/entrypoints/openai/completion/test_completion_error.py index aa9e9c1d72..818cad738d 100644 --- a/tests/entrypoints/openai/completion/test_completion_error.py +++ b/tests/entrypoints/openai/completion/test_completion_error.py @@ -610,3 +610,16 @@ class TestCompletionPromptListLimit: max_tokens=1, ) assert len(request.prompt_embeds) == 5 + + +@pytest.mark.parametrize("field_name", ["prompt_logprobs", "logprobs"]) +def test_non_numeric_logprobs_rejected(field_name): + """A non-numeric logprobs value must be a clean 400 validation error, not a + TypeError from the mode='before' comparison (which surfaces as HTTP 500).""" + with pytest.raises(ValidationError, match=f"`{field_name}` must be an integer"): + CompletionRequest( + model=MODEL_NAME, + prompt="Test prompt", + max_tokens=10, + **{field_name: "2"}, + ) diff --git a/vllm/entrypoints/openai/chat_completion/protocol.py b/vllm/entrypoints/openai/chat_completion/protocol.py index 8c1694bbf7..af8a19c643 100644 --- a/vllm/entrypoints/openai/chat_completion/protocol.py +++ b/vllm/entrypoints/openai/chat_completion/protocol.py @@ -757,6 +757,18 @@ class ChatCompletionRequest(OpenAIBaseModel): parameter="logprob_token_ids", ) + # These fields are integers, but `mode="before"` runs on the raw + # request data, so a non-numeric value (e.g. a JSON string) would + # reach the comparisons below and raise TypeError -> HTTP 500. Reject + # it here so the client gets a clean 400 instead. + for field_name in ("prompt_logprobs", "top_logprobs"): + field_value = data.get(field_name) + if field_value is not None and not isinstance(field_value, (int, float)): + raise VLLMValidationError( + f"`{field_name}` must be an integer.", + parameter=field_name, + value=field_value, + ) if (prompt_logprobs := data.get("prompt_logprobs")) is not None: if data.get("stream") and (prompt_logprobs > 0 or prompt_logprobs == -1): raise VLLMValidationError( diff --git a/vllm/entrypoints/openai/completion/protocol.py b/vllm/entrypoints/openai/completion/protocol.py index 73677b16af..6a2bf47bb1 100644 --- a/vllm/entrypoints/openai/completion/protocol.py +++ b/vllm/entrypoints/openai/completion/protocol.py @@ -468,6 +468,18 @@ class CompletionRequest(OpenAIBaseModel): parameter="logprob_token_ids", ) + # These fields are integers, but `mode="before"` runs on the raw + # request data, so a non-numeric value (e.g. a JSON string) would + # reach the comparisons below and raise TypeError -> HTTP 500. Reject + # it here so the client gets a clean 400 instead. + for field_name in ("prompt_logprobs", "logprobs"): + field_value = data.get(field_name) + if field_value is not None and not isinstance(field_value, (int, float)): + raise VLLMValidationError( + f"`{field_name}` must be an integer.", + parameter=field_name, + value=field_value, + ) if (prompt_logprobs := data.get("prompt_logprobs")) is not None: if data.get("stream") and (prompt_logprobs > 0 or prompt_logprobs == -1): raise VLLMValidationError(