Advanced Topics of the Structural Tag¶
Built-in Structural Tag: get_builtin_structural_tag¶
get_builtin_structural_tag generates a StructuralTag for the given model type with the specified tools and options. The returned structural tag can be used with Grammar.from_structural_tag or GrammarCompiler.compile_structural_tag to obtain a grammar that matches the function-calling format in the corresponding model’s style within any-form text.
Use it when you need to constrain the model to output in a fixed pattern such as “tool name + parameter JSON”, e.g. for Llama, Qwen, Kimi, DeepSeek, or OpenAI Harmony, etc.
Parameters¶
model (
BuiltinModels): The model type. Supported values:"llama": Llama-style (e.g. Llama 3, Llama 4)"qwen": Qwen-style (e.g. Qwen3)"qwen_coder": Qwen Coder-style (e.g. Qwen3-Coder, Qwen3-Coder-Next)"kimi": Kimi-style (e.g. Kimi-K2, Kimi-K2.5)"deepseek_r1": DeepSeek-style (e.g. DeepSeek-V3.1, DeepSeek-R1, DeepSeek-V3.2-exp)"harmony": OpenAI Harmony Response Format (e.g. gpt-oss)"deepseek_v3_2": DeepSeek-V3.2-style (e.g. DeepSeek-V3.2)"minimax": MiniMax-style (e.g. MiniMax-M2)"glm47": GLM-style (e.g. GLM-5, GLM-4.7)
reasoning (
bool, optional): Whether to enable reasoning mode (<think>/</think>tags). DefaultTrue.tools (
List[Dict[str, Any]], optional): List of tools; each item is a dict with a"function"key. The"function"dict must contain a"name"(string), and may contain:"parameters": JSON Schema, which can be:a dict (regular JSON Schema object), for example
{"type": "object", "properties": {...}}a bool:
Truemeans “any JSON value is accepted”,Falsemeans “no value is accepted”. If"parameters"is missing, the no constraint will be applied.
"strict":bool. Controls whether the parameter constraints are applied. WhenFalse, only the function name will be enforced, but the parameters is unconstrained. Default:True. Default value is[].
builtin_tools (
List[Dict[str, Any]], optional): List of built-in tools (used only for"harmony"); each element has the same structure as items intools. Default[].force_empty_reasoning (
bool, optional): When reasoning is on, whether to force empty thinking content at the beginning. DefaultFalse.
Passing an unsupported model raises ValueError.
Returns¶
StructuralTag: The structural tag for the given model’s function-calling format.
Example¶
from xgrammar import Grammar, get_builtin_structural_tag
tools = [
{"function": {"name": "get_weather", "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}}},
{"function": {"name": "get_time", "parameters": {"type": "object", "properties": {}}}},
]
# Get the Llama-style structural tag and build a grammar
structural_tag = get_builtin_structural_tag("llama", tools=tools)
grammar = Grammar.from_structural_tag(structural_tag)
For the Harmony format you must provide both tools and builtin_tools:
structural_tag = get_builtin_structural_tag(
"harmony",
tools=[
# User tool in strict mode, with a full JSON Schema
{
"function": {
"name": "user_tool",
"parameters": {"type": "object", "properties": {"q": {"type": "string"}}},
}
},
# User tool in non-strict mode: name only, arguments unconstrained (equivalent to parameters=True)
{"function": {"name": "user_tool_untyped", "strict": False}},
],
builtin_tools=[
# Built-in tools support the same strict / parameters combinations
{
"function": {
"name": "builtin_tool",
"parameters": {"type": "object", "properties": {}},
}
},
],
)
grammar = Grammar.from_structural_tag(structural_tag)
For formats that support reasoning (like Qwen3, Deepseek-R1, Kimi-k2-thinking ), pass reasoning to enable/disable the reasoning mode:
structural_tag = get_builtin_structural_tag("qwen", tools=tools, reasoning=True)
grammar = Grammar.from_structural_tag(structural_tag)
If reasoning is not passed, reasoning mode is enabled by default. Besides, when reasoning is True, you can also set force_empty_reasoning to constrain the reasoning content.
Supported models: get_builtin_structural_tag_supported_models¶
get_builtin_structural_tag_supported_models returns the supported model list for each built-in structural tag function. Call it with no args to get Dict[str, List[str]] (style → models), or pass a style name (e.g. "llama", "qwen") to get List[str] for that style. Use it to confirm which style a model uses before calling get_builtin_structural_tag.
Automatic End Detection¶
Certain “unlimited” formats — any_text, any_tokens, triggered_tags, and token_triggered_tags — can consume an unbounded amount of output. To know when to stop, the structural tag compiler automatically detects the end condition of the enclosing tag and adds it to the format’s internal exclude set.
The detection works by walking up to the nearest enclosing tag:
If the tag’s
endis a string (or list of strings), the end strings are added to the exclude set of string-level unlimited formats (any_text,triggered_tags).If the tag’s
endis atokenformat, the end token ID is added to the exclude set of token-level unlimited formats (any_tokens,token_triggered_tags,exclude_token).
Currently only string–string and token–token pairs are detected. Cross-level detection (e.g. a token end for a string-level format) is not supported. If you need additional exclusions beyond what automatic detection provides, specify them explicitly via the format’s own exclude field (excludes for any_text, exclude_tokens for exclude_token/any_tokens/token_triggered_tags).