Skip to content

vllm.parser.response_template_grammar

Structural tags for tool calls and answers, derived from a response template.

Functions:

_closers(field)

Return literal closers, an empty list if absent, or None if underivable.

_continuing(literals, anchors, opener)

Keep anchor continuations only where the parser finds the opener.

_json_format(schema, closers)

Use JSON unless a closer is ambiguous with its syntax or the schema is unsupported. Strings hold no closer, as the parser ends the call there.

_literals(pattern, *, message_start=False, lookahead=False)

Expand literal alternatives, allowing ^ only at the message start and a final lookahead only with lookahead.

_may_write(pattern, chars)

Whether a match of pattern may hold one of chars, as any pattern may that Python cannot parse.

_nonempty_start(closers)

The start of text that is not empty and does not begin with one of the closers. Each branch leaves every closer's spelling before completing it, on a character that starts no closer, so the text after it cannot hide a closer's start and a closer written as one token cannot pass for the text.

_openers(field, message_start=None)

Derive field openers, including continuations of message-start anchors.

_other_names(pattern, names)

A regex for the names pattern matches besides names, or None if pattern is no negatable character class repeated.

_reply_format(answer, message_start, content)

The answer field holding content, or free text if not given, or None if it cannot be derived.

_spells_closer(closers, pieces, *, repeats=False) cached

Check whether argument framing can spell a call closer across pieces.

_split_group(pattern, name)

Split a named regex group into its prefix, body and suffix.

_string_regex(schema, excludes)

Hold the format or pattern of a raw string. Like XGrammar, a format overrides a pattern, either one drops length bounds, and the pattern matches the whole string.

_structural_tag(output, thinking, message_start, requires_reasoning=False)

Frame output after reasoning, from the first token at the message_start. The reasoning is optional unless requires_reasoning makes the message open with non-empty reasoning.

_text_format(schema, excludes)

Constrain raw text, which the excludes end, without changing its parsed value.

_thinking_format(thinking, message_start, nonempty=False)

The reasoning, which holds at least a character if nonempty.

_typed_by_parser(schema)

Whether the parser reads schema values with their JSON type.

_value_format(schema, parameters, json_values, closers)

Use JSON for decoded values and raw text for string values.

answer_rest_structural_tag(answer)

The rest of an answer that is already open, or None if underivable.

message_start_anchors(template)

The strings the assistant message starts after, if literal.

tool_call_structural_tag(field, functions, *, parallel_tool_calls, thinking=None, answer=None, answer_format=None, message_start=None, requires_reasoning=False, sorted_arguments=False, strict_arguments=False)

Derive native calls to functions in the format of the tool call field, respecting strict arguments, count and reasoning.

functions are OpenAI function definitions. strict_arguments enforces the schema of every function's arguments where the template can. Strict xml-inline arguments come in declared order, or also alphabetical with sorted_arguments. An answer field lets the model answer instead, in answer_format if given; without functions, only the answer is derived. When the model may answer, calls may also name any other function the template can spell, with any arguments, so a harness can tell the model the function does not exist. At the message_start, the tag covers the output from its first token, which opens with non-empty reasoning if requires_reasoning. Returns None if the template format cannot be derived, and raises ValueError for names it cannot encode.