Skip to content

vllm.v1.structured_output.utils

Classes:

Functions:

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.

OutlinesVocabulary

Wrapper class for outlines_core.Vocabulary, which allows us to store a hash with the vocabulary

_reduced_vocabulary(tokenizer)

Create a map from vocabulary tokens to lists of equivalent token ids.

Returns:

allocate_token_bitmask(num_rows, vocab_size)

Allocate a CPU token bitmask for structured output grammars.

Parameters:

  • num_rows

    (int) –

    Number of grammar-mask rows to allocate.

  • vocab_size

    (int) –

    Model/logit vocabulary width.

Returns:

  • Tensor –

    A contiguous, int32 CPU tensor initialized to allow every token.

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.

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(tokenizer)

Get the Vocabulary object for a given tokenizer.

grammar_is_likely_lark(grammar_str)

Check if grammar appears to use Lark syntax.

Parameters:

  • grammar_str

    (str) –

    Input grammar string

Returns:

  • bool ( bool ) –

    True if grammar appears to be in Lark format, False otherwise

Examples:

>>> grammar_is_likely_lark("rule: 'abc'")
True
>>> grammar_is_likely_lark("rule ::= 'abc'")
False

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.