Unverified Commit b66a7464 authored by Umut Polat's avatar Umut Polat Committed by GitHub
Browse files

[Bugfix] Replace assert with ValueError for response_format validation in...


[Bugfix] Replace assert with ValueError for response_format validation in completions endpoint (#35456)
Signed-off-by: default avatarumut-polat <52835619+umut-polat@users.noreply.github.com>
parent 07bdabef
...@@ -221,6 +221,19 @@ async def test_completion_error_stream(): ...@@ -221,6 +221,19 @@ async def test_completion_error_stream():
assert chunks[-1] == "data: [DONE]\n\n" assert chunks[-1] == "data: [DONE]\n\n"
def test_json_schema_response_format_missing_schema():
"""When response_format type is 'json_schema' but the json_schema field
is not provided, request construction should raise a validation error
so the API returns 400 instead of 500."""
with pytest.raises(Exception, match="json_schema.*must be provided"):
CompletionRequest(
model=MODEL_NAME,
prompt="Test prompt",
max_tokens=10,
response_format={"type": "json_schema"},
)
def test_negative_prompt_token_ids_nested(): def test_negative_prompt_token_ids_nested():
"""Negative token IDs in prompt (nested list) should raise validation error.""" """Negative token IDs in prompt (nested list) should raise validation error."""
with pytest.raises(Exception, match="greater than or equal to 0"): with pytest.raises(Exception, match="greater than or equal to 0"):
......
...@@ -259,7 +259,7 @@ class CompletionRequest(OpenAIBaseModel): ...@@ -259,7 +259,7 @@ class CompletionRequest(OpenAIBaseModel):
structured_outputs_kwargs["json"] = json_schema.json_schema structured_outputs_kwargs["json"] = json_schema.json_schema
elif response_format.type == "structural_tag": elif response_format.type == "structural_tag":
structural_tag = response_format structural_tag = response_format
assert structural_tag is not None and isinstance( assert isinstance(
structural_tag, structural_tag,
( (
LegacyStructuralTagResponseFormat, LegacyStructuralTagResponseFormat,
...@@ -313,6 +313,34 @@ class CompletionRequest(OpenAIBaseModel): ...@@ -313,6 +313,34 @@ class CompletionRequest(OpenAIBaseModel):
skip_clone=True, # Created fresh per request, safe to skip clone skip_clone=True, # Created fresh per request, safe to skip clone
) )
@model_validator(mode="before")
@classmethod
def validate_response_format(cls, data):
response_format = data.get("response_format")
if response_format is None:
return data
rf_type = (
response_format.get("type")
if isinstance(response_format, dict)
else getattr(response_format, "type", None)
)
if rf_type == "json_schema":
json_schema = (
response_format.get("json_schema")
if isinstance(response_format, dict)
else getattr(response_format, "json_schema", None)
)
if json_schema is None:
raise VLLMValidationError(
"When response_format type is 'json_schema', the "
"'json_schema' field must be provided.",
parameter="response_format",
)
return data
@model_validator(mode="before") @model_validator(mode="before")
@classmethod @classmethod
def check_structured_outputs_count(cls, data): def check_structured_outputs_count(cls, data):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment