A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 7, 15:15 UTC
[Bugfix] Replace assert with ValueError for response_format validation in completions endpoint (#35456)
Signed-off-by: umut-polat <52835619+umut-polat@users.noreply.github.com>
Umut Polat committed
Feb 27, 2026 at 11:01 UTC
b66a74649e40022c6b1180b98fbb1b6e4b4af74a
2 files changed
+42
-1
tests/entrypoints/openai/test_completion_error.py
+13
@@ -221,6 +221,19 @@ async def test_completion_error_stream():
221
assert chunks[-1] == "data: [DONE]\n\n"
222
223
224
+def test_json_schema_response_format_missing_schema():
225
+ """When response_format type is 'json_schema' but the json_schema field
226
+ is not provided, request construction should raise a validation error
227
+ so the API returns 400 instead of 500."""
228
+ with pytest.raises(Exception, match="json_schema.*must be provided"):
229
+ CompletionRequest(
230
+ model=MODEL_NAME,
231
+ prompt="Test prompt",
232
+ max_tokens=10,
233
+ response_format={"type": "json_schema"},
234
+ )
235
+
236
+
237
def test_negative_prompt_token_ids_nested():
238
"""Negative token IDs in prompt (nested list) should raise validation error."""
239
with pytest.raises(Exception, match="greater than or equal to 0"):
vllm/entrypoints/openai/completion/protocol.py
+29
-1
@@ -259,7 +259,7 @@ class CompletionRequest(OpenAIBaseModel):
259
structured_outputs_kwargs["json"] = json_schema.json_schema
260
elif response_format.type == "structural_tag":
261
structural_tag = response_format
262
- assert structural_tag is not None and isinstance(
262
+ assert isinstance(
263
structural_tag,
264
(
265
LegacyStructuralTagResponseFormat,
@@ -313,6 +313,34 @@ class CompletionRequest(OpenAIBaseModel):
313
skip_clone=True, # Created fresh per request, safe to skip clone
314
)
315
316
+ @model_validator(mode="before")
317
+ @classmethod
318
+ def validate_response_format(cls, data):
319
+ response_format = data.get("response_format")
320
+ if response_format is None:
321
+ return data
322
+
323
+ rf_type = (
324
+ response_format.get("type")
325
+ if isinstance(response_format, dict)
326
+ else getattr(response_format, "type", None)
327
+ )
328
+
329
+ if rf_type == "json_schema":
330
+ json_schema = (
331
+ response_format.get("json_schema")
332
+ if isinstance(response_format, dict)
333
+ else getattr(response_format, "json_schema", None)
334
+ )
335
+ if json_schema is None:
336
+ raise VLLMValidationError(
337
+ "When response_format type is 'json_schema', the "
338
+ "'json_schema' field must be provided.",
339
+ parameter="response_format",
340
+ )
341
+
342
+ return data
343
+
344
@model_validator(mode="before")
345
@classmethod
346
def check_structured_outputs_count(cls, data):