Rust builders for xgrammar-compatible structural_tag tool-calling constraints.
This crate generates the JSON string that a serving frontend can pass to an xgrammar structural-tag backend. It has no runtime dependency on xgrammar and does not compile grammars or parse model output.
llamakimikimi_k3deepseek_r1deepseek_v3_1qwen_3_5qwen_3_codermimoqwen_3harmonydeepseek_v3_2minimaxminimax_m3glm_4_7gemma_4deepseek_v4deepseek_v4_1cohereexaonehermeshy_v3
use serde_json::json;
use xgrammar_structural_tag::{
FunctionDefinition, FunctionToolParam, Model, ToolChoice, ToolParam,
build_structural_tag,
builders::StructuralTagOptions,
};
fn main() -> xgrammar_structural_tag::Result<()> {
let tools = vec![ToolParam::Function(FunctionToolParam::new(
FunctionDefinition::new("get_weather").with_parameters(json!({
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
})),
))];
let tag = build_structural_tag(
Model::Qwen35,
&tools,
ToolChoice::required(),
StructuralTagOptions::default(),
)?;
let structural_tag_json = tag.to_json_string()?;
assert!(structural_tag_json.contains("structural_tag"));
Ok(())
}Model is the built-in catalog. Callers can also pass a concrete
builder such as builders::Qwen35Builder, Model::Qwen35.builder(), or their
own builders::StructuralTagBuilder implementation.
For serving request lowering, use build_optional_structural_tag. It
returns Ok(None) for empty tools or tool_choice=none.
See examples/custom_builder.rs for a custom StructuralTagBuilder
implementation.
StructuralTagOptions supports ReasoningMode::{Enabled, Disabled, Auto}.
Enabled mode continues a reasoning block opened by the prompt; adaptive mode
allows a complete optional reasoning block before the visible output. Harmony
uses the mode to select whether its analysis channel is available. Options
default to enabled reasoning for every builder, including MiniMax M3; set
Auto explicitly for its adaptive prompt.
use xgrammar_structural_tag::builders::{ReasoningMode, StructuralTagOptions};
let options = StructuralTagOptions::default()
.with_reasoning(ReasoningMode::Auto)
.with_parallel_tool_calls(false);With parallel_tool_calls(false), a response contains at most one call, and
the tool-call envelope closes the constrained output. Harmony can still emit
analysis messages before that call. with_reasoning(true/false) remains
available as a convenience for enabled/disabled mode.
Advanced callers can bound a free region directly in the format AST:
use xgrammar_structural_tag::format::{AnyTextFormat, Format, StructuralTag};
let grammar = StructuralTag::new(Format::tag(
"",
Format::AnyText(AnyTextFormat {
max_tokens: Some(128),
..Default::default()
}),
"</think>",
));AnyTextFormat supports token and Unicode-codepoint budgets; AnyTokensFormat
supports token budgets. None is unbounded and serialized as null; Some(0) forces an immediate
region end. Token limits take effect during mask-driven generation; direct
string acceptance enforces character limits. Limits must fit 0..=i32::MAX.
MiniMax M3 requires constrained argument schemas: XGrammar rejects the
unconstrained schemas produced by missing parameters or strict=false.
The Kimi K3 builder follows upstream's response-then-tools channel template;
serving frontends still own prompt-state alignment and grammar activation.
The 0.3 API changes StructuralTagOptions::reasoning from bool to
ReasoningMode and adds budget fields to AnyTextFormat and AnyTokensFormat.
Existing with_reasoning(bool) calls continue to work; use ..Default::default()
with direct free-region struct literals.
0.5 adds GrammarFormat::syntax (GrammarSyntax::{Ebnf, Lark}, serialized as
"syntax"; an omitted field deserializes as EBNF): build grammar leaves with
Format::grammar(grammar, syntax) (or GrammarFormat::new, GrammarFormat::ebnf,
GrammarFormat::lark) instead of a struct literal.
StructuralTagOptions::with_token_markers(true) matches the control markers of
builders that declare StructuralTagBuilder::marker_tokens (GLM 4.7 and the
Qwen 3 family) as their dedicated tokens. The new gemma_4 model uses
JsonSchemaStyle::Gemma, and DeepSeek V4/V4.1 auto output now ends after one
calls block regardless of parallel_tool_calls. Generated JSON follows
XGrammar 0.2.9.
cargo fmt --all --check
cargo test
cargo run --example custom_builder
RUSTDOCFLAGS="-D warnings" cargo doc --no-depsRefresh Rust-generated golden fixtures:
cargo run --example write_golden -- --writeRefresh upstream fixtures with the Python xgrammar version pinned in the script's uv metadata:
uv run scripts/generate_python_golden.pyCheck upstream parity and compile/replay the Rust fixtures with XGrammar:
uv run scripts/generate_python_golden.py --check
uv run scripts/test_python_compat.pyThe crate version build metadata records the xgrammar source version used for
the structural tag templates. The crate also includes extra model templates for
hermes and hy_v3.
This crate ports the structural_tag builders and schema shapes from
XGrammar (Apache-2.0), with a more typed
Rust API and additional local model templates. See NOTICE.