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.
llamakimideepseek_r1deepseek_v3_1qwen_3_5qwen_3_coderqwen_3harmonydeepseek_v3_2minimaxglm_4_7deepseek_v4hermeshy_v3
use serde_json::json;
use xgrammar_structural_tag::{
FunctionDefinition, FunctionToolParam, Model, ToolChoice, ToolParam,
build_structural_tag,
};
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(),
true,
)?;
let structural_tag_json = tag.to_json_string()?;
# Ok::<(), xgrammar_structural_tag::Error>(())For serving request lowering, use build_optional_structural_tag. It
returns Ok(None) for empty tools or tool_choice=none.
cargo fmt --all --check
cargo test
RUSTDOCFLAGS="-D warnings" cargo doc --no-depsRefresh Rust-generated golden fixtures:
cargo run --example write_golden -- --writeRefresh fixtures from Python xgrammar when an installed xgrammar build is available:
python3 scripts/generate_python_golden.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.