[Bugfix][Frontend] Guard remaining before-validators against non-object JSON bodies (#52528)
Signed-off-by: Kaif <kaifkohari10@gmail.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
|||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||||
|
|
||||||
|
"""Non-object JSON bodies must fail validation cleanly (4xx), not AttributeError (500).
|
||||||
|
|
||||||
|
mode=before validators that call data.get(...) without an isinstance(data, dict)
|
||||||
|
guard raise AttributeError for string/list/scalar bodies and surface as HTTP 500.
|
||||||
|
|
||||||
|
This extends the chat completion coverage added in #51654 to the remaining
|
||||||
|
request models whose before-validators were missing the same guard.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from pydantic import ValidationError
|
||||||
|
|
||||||
|
from vllm.entrypoints.openai.completion.protocol import CompletionRequest
|
||||||
|
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
|
||||||
|
from vllm.entrypoints.pooling.classify.protocol import ClassificationChatRequest
|
||||||
|
from vllm.entrypoints.pooling.embed.protocol import EmbeddingChatRequest
|
||||||
|
from vllm.entrypoints.pooling.pooling.protocol import PoolingChatRequest
|
||||||
|
from vllm.entrypoints.serve.tokenize.protocol import TokenizeChatRequest
|
||||||
|
from vllm.entrypoints.speech_to_text.transcription.protocol import TranscriptionRequest
|
||||||
|
from vllm.entrypoints.speech_to_text.translation.protocol import TranslationRequest
|
||||||
|
from vllm.exceptions import VLLMValidationError
|
||||||
|
|
||||||
|
pytestmark = pytest.mark.skip_global_cleanup
|
||||||
|
|
||||||
|
REQUEST_MODELS = [
|
||||||
|
CompletionRequest,
|
||||||
|
ResponsesRequest,
|
||||||
|
EmbeddingChatRequest,
|
||||||
|
ClassificationChatRequest,
|
||||||
|
PoolingChatRequest,
|
||||||
|
TokenizeChatRequest,
|
||||||
|
TranscriptionRequest,
|
||||||
|
TranslationRequest,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("request_model", REQUEST_MODELS, ids=lambda m: m.__name__)
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"payload",
|
||||||
|
[
|
||||||
|
"this is not valid json{{{",
|
||||||
|
["not", "an", "object"],
|
||||||
|
42,
|
||||||
|
None,
|
||||||
|
True,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_request_models_reject_non_object_body(request_model, payload):
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
request_model.model_validate(payload)
|
||||||
|
|
||||||
|
|
||||||
|
def test_completion_request_still_validates_dict_bodies():
|
||||||
|
"""The guard must not swallow real field-level errors on object bodies."""
|
||||||
|
with pytest.raises(VLLMValidationError, match="prompt"):
|
||||||
|
CompletionRequest.model_validate({"model": "qwen", "prompt": ""})
|
||||||
|
|
||||||
|
|
||||||
|
def test_tokenize_chat_request_still_validates_dict_bodies():
|
||||||
|
with pytest.raises(VLLMValidationError, match="add_generation_prompt"):
|
||||||
|
TokenizeChatRequest.model_validate(
|
||||||
|
{
|
||||||
|
"model": "qwen",
|
||||||
|
"messages": [{"role": "user", "content": "hello"}],
|
||||||
|
"continue_final_message": True,
|
||||||
|
"add_generation_prompt": True,
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -413,6 +413,8 @@ class CompletionRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_response_format(cls, data):
|
def validate_response_format(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
response_format = data.get("response_format")
|
response_format = data.get("response_format")
|
||||||
if response_format is None:
|
if response_format is None:
|
||||||
return data
|
return data
|
||||||
@@ -444,6 +446,8 @@ class CompletionRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def check_structured_outputs_count(cls, data):
|
def check_structured_outputs_count(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
if data.get("structured_outputs", None) is None:
|
if data.get("structured_outputs", None) is None:
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@@ -472,6 +476,8 @@ class CompletionRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def check_logprobs(cls, data):
|
def check_logprobs(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
if data.get("logprob_token_ids") and data.get("use_beam_search"):
|
if data.get("logprob_token_ids") and data.get("use_beam_search"):
|
||||||
raise VLLMValidationError(
|
raise VLLMValidationError(
|
||||||
"`logprob_token_ids` is not supported with beam search.",
|
"`logprob_token_ids` is not supported with beam search.",
|
||||||
@@ -532,6 +538,8 @@ class CompletionRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_stream_options(cls, data):
|
def validate_stream_options(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
if data.get("stream_options") and not data.get("stream"):
|
if data.get("stream_options") and not data.get("stream"):
|
||||||
raise VLLMValidationError(
|
raise VLLMValidationError(
|
||||||
"Stream options can only be defined when `stream=True`.",
|
"Stream options can only be defined when `stream=True`.",
|
||||||
@@ -543,6 +551,8 @@ class CompletionRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_prompt_and_prompt_embeds(cls, data):
|
def validate_prompt_and_prompt_embeds(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
prompt = data.get("prompt")
|
prompt = data.get("prompt")
|
||||||
prompt_embeds = data.get("prompt_embeds")
|
prompt_embeds = data.get("prompt_embeds")
|
||||||
|
|
||||||
@@ -562,6 +572,8 @@ class CompletionRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_prompt_list_length(cls, data):
|
def validate_prompt_list_length(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
max_prompts = envs.VLLM_MAX_COMPLETION_PROMPTS
|
max_prompts = envs.VLLM_MAX_COMPLETION_PROMPTS
|
||||||
|
|
||||||
prompt = data.get("prompt")
|
prompt = data.get("prompt")
|
||||||
|
|||||||
@@ -467,6 +467,8 @@ class ResponsesRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_background(cls, data):
|
def validate_background(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
if not data.get("background"):
|
if not data.get("background"):
|
||||||
return data
|
return data
|
||||||
if not data.get("store", True):
|
if not data.get("store", True):
|
||||||
@@ -479,6 +481,8 @@ class ResponsesRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_prompt(cls, data):
|
def validate_prompt(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
if data.get("prompt") is not None:
|
if data.get("prompt") is not None:
|
||||||
raise VLLMValidationError(
|
raise VLLMValidationError(
|
||||||
"prompt template is not supported", parameter="prompt"
|
"prompt template is not supported", parameter="prompt"
|
||||||
@@ -499,6 +503,8 @@ class ResponsesRequest(OpenAIBaseModel):
|
|||||||
|
|
||||||
Invalid structures are left for Pydantic to reject.
|
Invalid structures are left for Pydantic to reject.
|
||||||
"""
|
"""
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
input_data = data.get("input")
|
input_data = data.get("input")
|
||||||
|
|
||||||
# Early return for None, strings, or bytes
|
# Early return for None, strings, or bytes
|
||||||
|
|||||||
@@ -239,6 +239,8 @@ class ChatRequestOptionsMixin(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def check_generation_prompt(cls, data):
|
def check_generation_prompt(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
if data.get("continue_final_message") and data.get("add_generation_prompt"):
|
if data.get("continue_final_message") and data.get("add_generation_prompt"):
|
||||||
raise VLLMValidationError(
|
raise VLLMValidationError(
|
||||||
"Cannot set both `continue_final_message` and "
|
"Cannot set both `continue_final_message` and "
|
||||||
|
|||||||
@@ -120,6 +120,8 @@ class TokenizeChatRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def check_generation_prompt(cls, data):
|
def check_generation_prompt(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
if data.get("continue_final_message") and data.get("add_generation_prompt"):
|
if data.get("continue_final_message") and data.get("add_generation_prompt"):
|
||||||
raise VLLMValidationError(
|
raise VLLMValidationError(
|
||||||
"Cannot set both `continue_final_message` and "
|
"Cannot set both `continue_final_message` and "
|
||||||
|
|||||||
@@ -285,6 +285,8 @@ class TranscriptionRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_transcription_request(cls, data):
|
def validate_transcription_request(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
if isinstance(data.get("file"), str):
|
if isinstance(data.get("file"), str):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
|
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
|
||||||
|
|||||||
@@ -271,6 +271,8 @@ class TranslationRequest(OpenAIBaseModel):
|
|||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_stream_options(cls, data):
|
def validate_stream_options(cls, data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
stream_opts = ["stream_include_usage", "stream_continuous_usage_stats"]
|
stream_opts = ["stream_include_usage", "stream_continuous_usage_stats"]
|
||||||
stream = data.get("stream", False)
|
stream = data.get("stream", False)
|
||||||
if any(bool(data.get(so, False)) for so in stream_opts) and not stream:
|
if any(bool(data.get(so, False)) for so in stream_opts) and not stream:
|
||||||
|
|||||||
Reference in New Issue
Block a user