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.
-
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.
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.
-
(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 | |
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
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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
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: