Skip to content

vllm.parser.chat_parsing.response_templates

Template loading and validation for response_template dicts.

Classes:

ResponseTemplate dataclass

Methods:

truncate_past_last_anchor(text)

When parsing responses, we can't just parse the tokens generated by the model. This is because chat templates often include early parts of the message such as or tokens in the prefill. Therefore, we take the whole chat as input, but truncate to the start of the most recent assistant message, which can include tokens from the template as well as the output.

_build_field(name, field)

Validate a single field spec and compile it into a ResponseTemplateField.

_compile_anchor(scope, field, literal_key, pattern_key)

Compile a region's start/end anchor. An anchor can be:

  • a regex (pattern_key),
  • a single literal string (literal_key), or
  • a list of literal strings (literal_key), in which case any of these match

All forms compile to a regex for matching, but we keep the literal strings around too: when present, the parser can emit regions faster, since it knows exactly how many trailing bytes might be a partial-match prefix (and that a finished literal match can never be extended by future bytes, except when a literal is a strict prefix of another in the same list).

Returns (compiled_re, literals, can_extend):

  • compiled_re: the compiled pattern.
  • literals: the literal strings, or None for a regex anchor.
  • can_extend: True iff one literal is a strict prefix of another, in which case a match at the buffer edge could still be lengthened by future input.

_validate_template_shape(spec)

Validate the top-level response_template structure: it must be a dict of the expected version with only known keys, a dict of defaults, and a non-empty dict of fields. Per-field specs are checked separately in _build_field.