vllm.v1.structured_output.utils
¶
Classes:
-
OutlinesDiskCache–SQLite-backed cache for outlines_core.Index objects.
-
OutlinesVocabulary–Wrapper class for
outlines_core.Vocabulary,
Functions:
-
apply_grammar_bitmask–Apply grammar bitmask to output logits of the model with xgrammar function.
-
compile_regex_with_timeout–Run a regex compilation callable with a timeout.
-
get_outlines_cache–Get the Cache instance to be used for index caching.
-
get_outlines_cache_path–Get the context object that contains previously-computed return values.
-
get_outlines_vocabulary–Get the
Vocabularyobject for a given tokenizer. -
grammar_is_likely_lark–Check if grammar appears to use Lark syntax.
-
strip_speculative_padding–Drop speculative-decoding padding from a token block.
OutlinesDiskCache
¶
SQLite-backed cache for outlines_core.Index objects.
Uses outlines_core's native binary serialization (via Rust serde) instead of pickle, eliminating arbitrary code execution risk on deserialization.
Source code in vllm/v1/structured_output/utils.py
OutlinesVocabulary
¶
Wrapper class for outlines_core.Vocabulary,
which allows us to store a hash with the vocabulary
Source code in vllm/v1/structured_output/utils.py
_reduced_vocabulary(tokenizer)
¶
Create a map from vocabulary tokens to lists of equivalent token ids.
Returns:
Source code in vllm/v1/structured_output/utils.py
apply_grammar_bitmask(scheduler_output, grammar_output, input_batch, logits)
¶
Apply grammar bitmask to output logits of the model with xgrammar function.
Parameters:
-
(scheduler_output¶SchedulerOutput) –The result of engine scheduling.
-
(grammar_output¶GrammarOutput) –The grammar bitmask to apply.
-
(input_batch¶InputBatch) –The input of model runner.
-
(logits¶Tensor) –The output logits of model forward.
Source code in vllm/v1/structured_output/utils.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
compile_regex_with_timeout(fn, pattern)
¶
Run a regex compilation callable with a timeout.
Prevents ReDoS attacks where adversarial regex patterns (e.g. nested
quantifiers like (a+)+b) cause exponential DFA state-space explosion,
hanging the inference worker indefinitely.
Parameters:
-
(fn¶Callable[[str], _T]) –Single-argument callable that takes the pattern and performs the regex compilation.
-
(pattern¶str) –The regex pattern string, passed to fn and included in timeout error messages.
Raises:
-
ValueError–If compilation exceeds the configured timeout.
Source code in vllm/v1/structured_output/utils.py
get_outlines_cache()
¶
Get the Cache instance to be used for index caching.
Source code in vllm/v1/structured_output/utils.py
get_outlines_cache_path()
¶
Get the context object that contains previously-computed return values.
Source code in vllm/v1/structured_output/utils.py
get_outlines_vocabulary(tokenizer)
¶
Get the Vocabulary object for a given tokenizer.
Source code in vllm/v1/structured_output/utils.py
grammar_is_likely_lark(grammar_str)
¶
Check if grammar appears to use Lark syntax.
Parameters:
Returns:
-
bool(bool) –True if grammar appears to be in Lark format, False otherwise
Examples:
Source code in vllm/v1/structured_output/utils.py
strip_speculative_padding(token_ids)
¶
Drop speculative-decoding padding from a token block.
ngram and other speculative backends pad rejected draft positions with a -1 sentinel. Structured-output grammars treat every entry as a real token id, so the sentinels (and everything after the first one) are removed here, before tokens reach any backend, rather than inside a single backend.