vllm.parser.response_template_grammar
¶
Structural tags for tool calls and answers, derived from a response template.
Functions:
-
answer_rest_structural_tag–The rest of an
answerthat is already open, orNoneif underivable. -
message_start_anchors–The strings the assistant message starts after, if literal.
-
tool_call_structural_tag–Derive native calls to
functionsin the format of the tool call
_closers(field)
¶
Return literal closers, an empty list if absent, or None if underivable.
Source code in vllm/parser/response_template_grammar.py
_continuing(literals, anchors, opener)
¶
Keep anchor continuations only where the parser finds the opener.
Source code in vllm/parser/response_template_grammar.py
_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.
Source code in vllm/parser/response_template_grammar.py
_literals(pattern, *, message_start=False, lookahead=False)
¶
Expand literal alternatives, allowing ^ only at the message start and
a final lookahead only with lookahead.
Source code in vllm/parser/response_template_grammar.py
_may_write(pattern, chars)
¶
Whether a match of pattern may hold one of chars, as any pattern may
that Python cannot parse.
Source code in vllm/parser/response_template_grammar.py
_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.
Source code in vllm/parser/response_template_grammar.py
_openers(field, message_start=None)
¶
Derive field openers, including continuations of message-start anchors.
Source code in vllm/parser/response_template_grammar.py
_other_names(pattern, names)
¶
A regex for the names pattern matches besides names, or None if
pattern is no negatable character class repeated.
Source code in vllm/parser/response_template_grammar.py
_reply_format(answer, message_start, content)
¶
The answer field holding content, or free text if not given, or
None if it cannot be derived.
Source code in vllm/parser/response_template_grammar.py
_spells_closer(closers, pieces, *, repeats=False)
cached
¶
Check whether argument framing can spell a call closer across pieces.
Source code in vllm/parser/response_template_grammar.py
_split_group(pattern, name)
¶
Split a named regex group into its prefix, body and suffix.
Source code in vllm/parser/response_template_grammar.py
_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.
Source code in vllm/parser/response_template_grammar.py
_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.
Source code in vllm/parser/response_template_grammar.py
_text_format(schema, excludes)
¶
Constrain raw text, which the excludes end, without changing its
parsed value.
Source code in vllm/parser/response_template_grammar.py
_thinking_format(thinking, message_start, nonempty=False)
¶
The reasoning, which holds at least a character if nonempty.
Source code in vllm/parser/response_template_grammar.py
_typed_by_parser(schema)
¶
Whether the parser reads schema values with their JSON type.
Source code in vllm/parser/response_template_grammar.py
_value_format(schema, parameters, json_values, closers)
¶
Use JSON for decoded values and raw text for string values.
Source code in vllm/parser/response_template_grammar.py
answer_rest_structural_tag(answer)
¶
The rest of an answer that is already open, or None if underivable.
Source code in vllm/parser/response_template_grammar.py
message_start_anchors(template)
¶
The strings the assistant message starts after, if literal.
Source code in vllm/parser/response_template_grammar.py
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.
Source code in vllm/parser/response_template_grammar.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 | |