1616from vllm .sampling_params import GuidedDecodingParams , SamplingParams
1717
1818MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
19- GUIDED_DECODING_BACKENDS = [
19+
20+ # Separate backends which support grammars vs ones
21+ # which only support regex based constraints in tests.
22+ GRAMMAR_DECODING_BACKENDS = [
2023 # (backend, disable_any_whitespace),
21- ("outlines" , False ),
2224 ("lm-format-enforcer" , False ),
2325 ("xgrammar" , True ),
2426 ("guidance" , True ),
2527]
2628
29+ ALL_DECODING_BACKENDS = ([("outlines" , False )] + GRAMMAR_DECODING_BACKENDS )
30+
2731
2832@pytest .fixture (scope = "module" )
2933def llm ():
@@ -39,7 +43,7 @@ def llm():
3943
4044@pytest .mark .skip_global_cleanup
4145@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
42- GUIDED_DECODING_BACKENDS )
46+ ALL_DECODING_BACKENDS )
4347def test_guided_regex (sample_regex , llm , guided_decoding_backend : str ,
4448 disable_any_whitespace : bool ):
4549 sampling_params = SamplingParams (
@@ -49,6 +53,7 @@ def test_guided_regex(sample_regex, llm, guided_decoding_backend: str,
4953 regex = sample_regex ,
5054 backend = guided_decoding_backend ,
5155 disable_any_whitespace = disable_any_whitespace ))
56+
5257 outputs = llm .generate (prompts = [
5358 f"Give an example IPv4 address with this regex: { sample_regex } "
5459 ] * 2 ,
@@ -69,7 +74,7 @@ def test_guided_regex(sample_regex, llm, guided_decoding_backend: str,
6974
7075@pytest .mark .skip_global_cleanup
7176@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
72- GUIDED_DECODING_BACKENDS )
77+ ALL_DECODING_BACKENDS )
7378def test_guided_json_completion (sample_json_schema , llm ,
7479 guided_decoding_backend : str ,
7580 disable_any_whitespace : bool ):
@@ -103,7 +108,7 @@ def test_guided_json_completion(sample_json_schema, llm,
103108
104109@pytest .mark .skip_global_cleanup
105110@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
106- GUIDED_DECODING_BACKENDS )
111+ ALL_DECODING_BACKENDS )
107112def test_guided_complex_json_completion (sample_complex_json_schema , llm ,
108113 guided_decoding_backend : str ,
109114 disable_any_whitespace : bool ):
@@ -138,7 +143,7 @@ def test_guided_complex_json_completion(sample_complex_json_schema, llm,
138143
139144@pytest .mark .skip_global_cleanup
140145@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
141- GUIDED_DECODING_BACKENDS )
146+ ALL_DECODING_BACKENDS )
142147def test_guided_definition_json_completion (sample_definition_json_schema , llm ,
143148 guided_decoding_backend : str ,
144149 disable_any_whitespace : bool ):
@@ -173,7 +178,7 @@ def test_guided_definition_json_completion(sample_definition_json_schema, llm,
173178
174179@pytest .mark .skip_global_cleanup
175180@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
176- GUIDED_DECODING_BACKENDS )
181+ ALL_DECODING_BACKENDS )
177182def test_guided_enum_json_completion (sample_enum_json_schema , llm ,
178183 guided_decoding_backend : str ,
179184 disable_any_whitespace : bool ):
@@ -218,7 +223,7 @@ def test_guided_enum_json_completion(sample_enum_json_schema, llm,
218223
219224@pytest .mark .skip_global_cleanup
220225@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
221- GUIDED_DECODING_BACKENDS )
226+ ALL_DECODING_BACKENDS )
222227def test_guided_choice_completion (sample_guided_choice , llm ,
223228 guided_decoding_backend : str ,
224229 disable_any_whitespace : bool ):
@@ -248,7 +253,7 @@ def test_guided_choice_completion(sample_guided_choice, llm,
248253
249254@pytest .mark .skip_global_cleanup
250255@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
251- GUIDED_DECODING_BACKENDS )
256+ GRAMMAR_DECODING_BACKENDS )
252257def test_guided_grammar (sample_sql_statements , llm ,
253258 guided_decoding_backend : str ,
254259 disable_any_whitespace : bool ):
@@ -344,7 +349,7 @@ def test_disable_guided_decoding_fallback(sample_regex, llm):
344349
345350@pytest .mark .skip_global_cleanup
346351@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
347- GUIDED_DECODING_BACKENDS )
352+ GRAMMAR_DECODING_BACKENDS )
348353def test_guided_json_object (llm , guided_decoding_backend : str ,
349354 disable_any_whitespace : bool ):
350355 sampling_params = SamplingParams (
@@ -377,7 +382,9 @@ def test_guided_json_object(llm, guided_decoding_backend: str,
377382
378383 # Parse to verify it is valid JSON
379384 parsed_json = json .loads (generated_text )
380- assert isinstance (parsed_json , dict )
385+ # A list is not what was intended, but is still valid
386+ # json.
387+ assert isinstance (parsed_json , (dict , list ))
381388
382389
383390class CarType (str , Enum ):
@@ -395,7 +402,7 @@ class CarDescription(BaseModel):
395402
396403@pytest .mark .skip_global_cleanup
397404@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
398- GUIDED_DECODING_BACKENDS )
405+ ALL_DECODING_BACKENDS )
399406def test_guided_json_completion_with_enum (llm , guided_decoding_backend : str ,
400407 disable_any_whitespace : bool ):
401408 json_schema = CarDescription .model_json_schema ()
@@ -427,7 +434,7 @@ def test_guided_json_completion_with_enum(llm, guided_decoding_backend: str,
427434
428435@pytest .mark .skip_global_cleanup
429436@pytest .mark .parametrize ("guided_decoding_backend,disable_any_whitespace" ,
430- GUIDED_DECODING_BACKENDS )
437+ ALL_DECODING_BACKENDS )
431438def test_guided_number_range_json_completion (llm , guided_decoding_backend : str ,
432439 disable_any_whitespace : bool ):
433440 sample_output_schema = {
0 commit comments