[Bugfix][Structured Output] Cap JSON schema nesting to prevent 500s and API-server stalls (#60036)
Signed-off-by: Mazukiri <maihoangduc04@gmail.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
co-authored by
Flora Feng
parent
fa423b614c
commit
5cd8a2626b
@@ -1,6 +1,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
@@ -642,7 +643,15 @@ def test_batch_structural_tag_response_format_invalid(format_value):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("structural_tag", ["not json", ""])
|
||||
@pytest.mark.parametrize(
|
||||
"structural_tag",
|
||||
[
|
||||
"not json",
|
||||
"",
|
||||
# json.loads raises a plain ValueError, not JSONDecodeError
|
||||
'{"type": "structural_tag", "x": ' + "1" * 5000 + "}",
|
||||
],
|
||||
)
|
||||
def test_structured_outputs_structural_tag_invalid(structural_tag):
|
||||
"""Malformed direct structured_outputs structural tags should be rejected."""
|
||||
with pytest.raises(
|
||||
@@ -656,6 +665,25 @@ def test_structured_outputs_structural_tag_invalid(structural_tag):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field", ["response_format", "structured_outputs"])
|
||||
def test_deeply_nested_structural_tag_rejected(field):
|
||||
"""Structural tags are compiled during request parsing, so a deeply nested
|
||||
one must be rejected there instead of blocking the server while converting,
|
||||
and reported as too deep rather than malformed."""
|
||||
schema = '{"type": "array", "items": ' * 200 + "{}" + "}" * 200
|
||||
tag = (
|
||||
'{"type": "structural_tag", "format": {"type": "json_schema", '
|
||||
f'"json_schema": {schema}}}}}'
|
||||
)
|
||||
value = json.loads(tag) if field == "response_format" else {"structural_tag": tag}
|
||||
with pytest.raises(VLLMValidationError, match="nested too deeply"):
|
||||
ChatCompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
messages=[{"role": "user", "content": "hello"}],
|
||||
**{field: value},
|
||||
)
|
||||
|
||||
|
||||
@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
|
||||
|
||||
Reference in New Issue
Block a user