vllm.v1.structured_output.utils ¶
Classes:
-
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.
-
convert_lark_to_ebnf–Convert a Lark grammar string to EBNF format.
-
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.
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.
-
(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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 | |
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
convert_lark_to_ebnf(grammar_str) ¶
Convert a Lark grammar string to EBNF format.
EBNF reference: https://github.com/ggerganov/llama.cpp/blob/master/grammars/README.md Lark grammar reference: https://lark-parser.readthedocs.io/en/latest/grammar.html
Parameters:
Returns:
-
str(str) –Converted grammar in EBNF format
Examples:
Source code in vllm/v1/structured_output/utils.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
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: