7 unstable releases (3 breaking)

Uses new Rust 2024

new 0.4.2 Apr 18, 2026
0.4.1 Apr 18, 2026
0.3.3 Mar 31, 2026
0.3.0 Jan 9, 2026
0.1.3 Oct 10, 2025

#1012 in Filesystem

Download history 225/week @ 2025-12-27 854/week @ 2026-01-03 793/week @ 2026-01-10 1108/week @ 2026-01-17 820/week @ 2026-01-24 705/week @ 2026-01-31 437/week @ 2026-02-07 4136/week @ 2026-02-14 62674/week @ 2026-02-21 71410/week @ 2026-02-28 87959/week @ 2026-03-07 59946/week @ 2026-03-14 46347/week @ 2026-03-21 48208/week @ 2026-03-28 68998/week @ 2026-04-04 49578/week @ 2026-04-11

220,455 downloads per month

Apache-2.0

4.5MB
88K SLoC

C++ 64K SLoC // 0.2% comments Python 20K SLoC // 0.3% comments TypeScript 1.5K SLoC // 0.3% comments Rust 1K SLoC // 0.1% comments Bazel 1K SLoC // 0.2% comments Shell 381 SLoC // 0.2% comments CUDA 237 SLoC // 0.1% comments JavaScript 77 SLoC Batch 72 SLoC // 0.1% comments C 40 SLoC // 0.0% comments PowerShell 25 SLoC

xgrammar-rs

This project uses xgrammar v0.1.33 as a submodule.

CI Deploy Docs

This project provides safe and idiomatic Rust bindings for the xgrammar C++ library. By wrapping the C++ implementation, this crate leverages Rust's memory safety andguarantees while providing access to xgrammar's high-performance and features for constraint decoding.

Highlights

  • Grammar, GrammarCompiler, CompiledGrammar, TokenizerInfo — core API for compiling grammars (BNF, JSON schema, regex, structural tags) against a tokenizer.
  • GrammarMatcher — token-by-token constrained decoding, including is_completed() (root-rule match without stop token) and fork() for speculative / branching decoding.
  • BatchGrammarMatcher — batched helpers operating on a slice of matchers: batch_fill_next_token_bitmask (parallel, thread-pool-backed), batch_accept_token, batch_accept_string, batch_rollback (sequential static helpers).
  • Linux x86_64 and aarch64 (arm64) are both supported.

See the rustdoc for detailed method-level documentation, including when a BatchGrammarMatcher instance is required vs when associated functions can be called directly.

Prerequisites

Before building the project, ensure you have the following dependencies installed:

  • Rust toolchain: Install via rustup.
  • CMake: Required to build the underlying C++ xgrammar library.
  • C++ compiler: A modern C++ compiler that supports C++17 (Clang is highly recommended.)

Build

The C++ xgrammar library is included as a submodule and will be compiled automatically as part of the build process.

To build the project, run the following command:

cargo build --release

This will create a release build in the target/release directory.

Test

This project uses cargo-nextest for running tests. To execute all tests, run:

make test

You can also pass arguments to cargo nextest via the TEST_ARGS variable. For example, to run a specific test suite:

make test TEST_ARGS="--test test_grammar"

Dependencies

~21–38MB
~492K SLoC