1
1
# SPDX-License-Identifier: Apache-2.0
2
+ import copy
2
3
import json
3
4
from re import escape as regex_escape
4
5
10
11
from vllm .sampling_params import GuidedDecodingParams
11
12
12
13
14
+ def _walk_json_for_additional_properties (data : object ):
15
+ if isinstance (data , dict ):
16
+ for value in data .values ():
17
+ _walk_json_for_additional_properties (value )
18
+ if 'additionalProperties' not in data and \
19
+ ('properties' in data or 'patternProperties' in data ):
20
+ data ['additionalProperties' ] = False
21
+ elif isinstance (data , list ):
22
+ for item in data :
23
+ _walk_json_for_additional_properties (item )
24
+
25
+
13
26
def get_local_guidance_guided_decoding_logits_processor (
14
27
guided_params : GuidedDecodingParams ,
15
28
tokenizer : PreTrainedTokenizerBase ) -> GuidanceLogitsProcessor :
@@ -28,8 +41,10 @@ def get_local_guidance_guided_decoding_logits_processor(
28
41
if 'no-additional-properties' in guided_params .backend_options ():
29
42
if isinstance (guide_json , str ):
30
43
guide_json = json .loads (guide_json )
31
- if 'additionalProperties' not in guide_json :
32
- guide_json ['additionalProperties' ] = False
44
+ else :
45
+ # copy for modifications
46
+ guide_json = copy .deepcopy (guide_json )
47
+ _walk_json_for_additional_properties (guide_json )
33
48
34
49
grm = llguidance .LLMatcher .grammar_from_json_schema (
35
50
guide_json ,
0 commit comments