Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
3b00ff91
Unverified
Commit
3b00ff91
authored
Mar 28, 2025
by
Chauncey
Committed by
GitHub
Mar 28, 2025
Browse files
[Bugfix][v1] xgrammar structured output supports Enum. (#15594)
Signed-off-by:
chaunceyjiang
<
chaunceyjiang@gmail.com
>
parent
91276c57
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
4 deletions
+53
-4
tests/v1/entrypoints/llm/test_struct_output_generate.py
tests/v1/entrypoints/llm/test_struct_output_generate.py
+53
-0
vllm/v1/structured_output/utils.py
vllm/v1/structured_output/utils.py
+0
-4
No files found.
tests/v1/entrypoints/llm/test_struct_output_generate.py
View file @
3b00ff91
...
@@ -4,10 +4,12 @@ from __future__ import annotations
...
@@ -4,10 +4,12 @@ from __future__ import annotations
import
json
import
json
import
re
import
re
from
enum
import
Enum
from
typing
import
Any
from
typing
import
Any
import
jsonschema
import
jsonschema
import
pytest
import
pytest
from
pydantic
import
BaseModel
from
vllm.entrypoints.llm
import
LLM
from
vllm.entrypoints.llm
import
LLM
from
vllm.outputs
import
RequestOutput
from
vllm.outputs
import
RequestOutput
...
@@ -390,3 +392,54 @@ def test_guided_choice_completion(
...
@@ -390,3 +392,54 @@ def test_guided_choice_completion(
assert
generated_text
is
not
None
assert
generated_text
is
not
None
assert
generated_text
in
sample_guided_choice
assert
generated_text
in
sample_guided_choice
print
(
f
"Prompt:
{
prompt
!
r
}
, Generated text:
{
generated_text
!
r
}
"
)
print
(
f
"Prompt:
{
prompt
!
r
}
, Generated text:
{
generated_text
!
r
}
"
)
class
CarType
(
str
,
Enum
):
sedan
=
"sedan"
suv
=
"SUV"
truck
=
"Truck"
coupe
=
"Coupe"
class
CarDescription
(
BaseModel
):
brand
:
str
model
:
str
car_type
:
CarType
@
pytest
.
mark
.
skip_global_cleanup
@
pytest
.
mark
.
parametrize
(
"guided_decoding_backend"
,
GUIDED_DECODING_BACKENDS_V1
)
@
pytest
.
mark
.
parametrize
(
"model_name"
,
MODELS_TO_TEST
)
def
test_guided_json_completion_with_enum
(
monkeypatch
:
pytest
.
MonkeyPatch
,
guided_decoding_backend
:
str
,
model_name
:
str
,
):
monkeypatch
.
setenv
(
"VLLM_USE_V1"
,
"1"
)
llm
=
LLM
(
model
=
model_name
,
max_model_len
=
1024
,
guided_decoding_backend
=
guided_decoding_backend
)
json_schema
=
CarDescription
.
model_json_schema
()
sampling_params
=
SamplingParams
(
temperature
=
1.0
,
max_tokens
=
1000
,
guided_decoding
=
GuidedDecodingParams
(
json
=
json_schema
))
outputs
=
llm
.
generate
(
prompts
=
"Generate a JSON with the brand, model and car_type of"
"the most iconic car from the 90's"
,
sampling_params
=
sampling_params
,
use_tqdm
=
True
)
assert
outputs
is
not
None
for
output
in
outputs
:
assert
output
is
not
None
assert
isinstance
(
output
,
RequestOutput
)
prompt
=
output
.
prompt
generated_text
=
output
.
outputs
[
0
].
text
assert
generated_text
is
not
None
print
(
f
"Prompt:
{
prompt
!
r
}
, Generated text:
{
generated_text
!
r
}
"
)
output_json
=
json
.
loads
(
generated_text
)
jsonschema
.
validate
(
instance
=
output_json
,
schema
=
json_schema
)
vllm/v1/structured_output/utils.py
View file @
3b00ff91
...
@@ -26,10 +26,6 @@ def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool:
...
@@ -26,10 +26,6 @@ def has_xgrammar_unsupported_json_features(schema: dict[str, Any]) -> bool:
if
"pattern"
in
obj
:
if
"pattern"
in
obj
:
return
True
return
True
# Check for enum restrictions
if
"enum"
in
obj
:
return
True
# Check for numeric ranges
# Check for numeric ranges
if
obj
.
get
(
"type"
)
in
(
"integer"
,
"number"
)
and
any
(
if
obj
.
get
(
"type"
)
in
(
"integer"
,
"number"
)
and
any
(
key
in
obj
key
in
obj
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment