llguidance
Library Featured
LLM Token Trie library
- Downloads
- 182,570
- Dependents
- 22
- Stars
- 909
- Forks
- 109
- Contributors
- 48
- Commits
- 1,597
- Latest release
- 1.9.1 ·
About
Low-level Guidance (llguidance)
Performance results from MaskBench
- 2025-06-23 llguidance is now deemed v1.0.0
- 2025-06-11 Making Structured Outputs Go Brrr blog post released
- 2025-05-20 LLGuidance shipped in OpenAI for JSON Schema
- 2025-04-11 integration merged into Chromium
- 2025-03-25 integration merged into vLLM (v0.8.2)
- 2025-02-26 integration merged into SGLang (v0.4.4)
- 2025-02-01 integration merged into llama.cpp (b4613)
- 2025-01-21 JSONSchemaBench released, including paper and MaskBench
- 2025-01-07 Guidance v0.2.0 released, using llguidance as the grammar engine
About
This library implements constrained decoding (also called constrained sampling or structured outputs) for Large Language Models (LLMs). It can enforce arbitrary context-free grammar on the output of LLM and is fast - on the order of 50μs of CPU time per token (for 128k tokenizer) with negligible startup costs.
Following grammar formats are supported:
- a large subset of JSON schemas
- regular expressions
- context-free grammars in a variation of Lark format; with embedded JSON schemas and regular expressions
llguidance- internal (JSON-based) format; slowly being deprecated in favor of the Lark-like format
Read the full README
The internal format is most powerful (though Lark-like format is catching up, and there are plans to convert the libraries to use it) and can be generated by the following libraries:
- Guidance (Python)
- guidance.ts (TypeScript)
- hopefully more to come!
The library can be used from:
Integrations
The library is currently integrated in:
- Guidance - library for interacting with LLMs
- OpenAI models - LLGuidance powers Structured Output (JSON Schema only)
- llama.cpp -
available via
-DLLAMA_LLGUIDANCE=ONoption forcmake; llama.cpp can be also used Guidance Python package - Chromium - merged,
to be used for JSON Schema enforcement for
window.aiin Chromium-based browsers - SGLang -
use
--grammar-backend llguidance; when passing Lark grammar make sure to prefix them with%llguidance {}, just as in llama.cpp - vLLM - V0 PR and V1 PR
- LLGTRT - OpenAI-compatible REST server using NVIDIA's TensorRT-LLM
- mistral.rs
- onnxruntime-genai
Technical details
See Making Structured Outputs Go Brrr for an overview of the library, including the design decisions, performance, and how it compares to other approaches.
Given a context-free grammar, a tokenizer, and a prefix of tokens, llguidance computes a token mask - a set of tokens from the tokenizer - that, when added to the current token prefix, can lead to a valid string in the language defined by the grammar. Mask computation takes approximately 50μs of single-core CPU time for a tokenizer with 128k tokens. While this timing depends on the exact grammar, it holds, for example, for grammars derived from JSON schemas. There is no significant startup cost.
The library implements a context-free grammar parser using Earley’s algorithm on top of a lexer based on derivatives of regular expressions. Mask computation is achieved by traversing the prefix tree (trie) of all possible tokens, leveraging highly optimized code.
Grammars can be also used to speed up decode via fast-forward tokens.
Comparison and performance
See MaskBench in JSON Schema Bench for detailed performance comparisons.
LM-format-enforcer and llama.cpp grammars are similar to llguidance in that they dynamically build token masks for every step of the decoding process. Both are significantly slower - the former due to clean Python code and the latter due to the lack of a lexer and use of a backtracking parser, which, while elegant, is inefficient.
Outlines builds an automaton from constraints and then pre-computes token masks for all automaton states, potentially making sampling fast but inherently limiting constraint complexity and introducing significant startup cost and memory overhead. Llguidance computes token masks on the fly and has essentially no startup cost. The lexer’s automata in llguidance are built lazily and are typically much smaller, as the context-free grammar imposes the top-level structure.
XGrammar follows an approach similar to llama.cpp (explicit stack-based, character-level parser) with additional pre-computation of certain token masks, similar to Outlines. The pre-computation often runs into seconds, and sometimes minutes. If the pre-computation works well for a given input, the masks are computed quickly (under 8μs in half of masks we tested), however if it doesn't fit the particular input, the mask computation times can run to tens or hundreds of milliseconds.
In llguidance, the full mask computation for a typical JSON schema takes about 1.5ms (for 128k tokenizer). However, very often the "slicer" optimization applies, and thus the average mask computation in JSON Schema Bench (2.5M tokens, 10k schemas) is under 50μs, with less than 1% of masks taking longer than 1ms, and 0.001% taking longer than 10ms (but still shorter than 30ms). The optimization doesn't involve any significant pre-computation.
Thus, with 16 cores and a 10ms forward pass, llguidance can handle batch sizes up to 3200 without slowing down the model. (Note that a 10ms forward pass for small batch sizes typically increases to 20ms+ for batch sizes of 100-200.)
Building
- install rust; 1.87 or later
If you just need the C or Rust library (llguidance),
check the parser directory.
For Python bindings:
- install python 3.11 or later; very likely you'll need a virtual env/conda
- run
./scripts/install-deps.sh - to build and after any changes, run
./scripts/test-guidance.sh
This builds the Python bindings for the library and runs the tests (which mostly live in the Guidance repo - it will clone it).
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Trademarks
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.
Compatibility
- Runs on
- GPU support unknown
What shows it
- no dependency or feature matches a backend row
- Native code
- None found
- No native code found
- Python bindings
- No Python binding crate found
- License
- MIT
Compare with similar projects
Order: strongest tier first, then most downloads, then name, with unmeasured values last. A library counts downloads over 30 days. An app counts binary downloads per release. An app with none counts its crate downloads over 30 days. ML Rust runs this catalogue, so its closest project always gets a row.
| Project | Downloads | Dependents | Stars | Latest release | Runs on (GPU) | Native code | License |
|---|---|---|---|---|---|---|---|
|
LLM Token Trie library |
182,570 downloads over 30 days | 22 | 909 | 1.9.1 | GPU support unknown | No native code found | MIT |
|
rten Featured
Tensor library for the RTen machine learning runtime Also in Run and serve models. |
496,924 downloads over 30 days | 29 | 360 | 0.27.0 | GPU support unknown | No native code found | Apache-2.0 OR MIT |
|
Fast, flexible LLM inference. Python bindings Also in Build LLM apps and agents. |
1,498 binary downloads per release | Not applicable | 7,819 | 0.8.1 | CUDA, Metal | Binds native code: accelerate-src (feature accelerate-src), intel-mkl-src (feature intel-mkl-src) | MIT |
|
Token samplers for large language models Also in Run and serve models. |
4,242 downloads over 30 days | 6 | 18 | 0.0.7 | GPU support unknown | No native code found | MIT |
|
llama-gguf Listed
A high-performance Rust implementation of llama.cpp - LLM inference engine with full GGUF support Also in Build LLM apps and agents. |
1,787 downloads over 30 days | 3 | 21 | 0.14.0 | CUDA, Metal, Vulkan | Binds native code: hailort-sys (feature hailo), libsqlite3-sys (feature rag-sqlite) | Apache-2.0 OR MIT |
|
Distributed AI/LLM for the people. Share compute privately or publicly to power your agents and chat. Also in Build LLM apps and agents. |
1,411 binary downloads per release | Not applicable | 3,993 | 0.78.1 | GPU support unknown | Binds native code: libsqlite3-sys, netlink-sys, ring, secp256k1-sys | Apache-2.0 OR MIT |
|
Inference server for LLMs, speech and embeddings: OpenAI-compatible routes with streaming, plus speech synthesis (TTS) and transcription (ASR). Serves Llama, Mistral, Qwen, Mamba2, MLA+MoE and hybrid models from SafeTensors, AWQ, GPTQ and GGUF. Also in Build LLM apps and agents. |
8 binary downloads per release | Not applicable | 7 | 0.7.1 | CUDA, WebGPU | Binds native code: ring (feature distributed) | Apache-2.0 |
All projects in Build LLM apps and agentsAll projects in Run and serve models
Why FeaturedIt passes every Featured gate.
What it meets
- It has 15 active dependents and 15 new dependents in 365 days. Featured needs 11 active dependents or 9 new dependents in 365 days.
- Active dependents are at 100% of their peak. Featured needs at least 50%.
- Docs are present. The release commit or a manifest ties the crate to the repository.
- No vulnerability advisory stays unpatched past the grace window. At least one version is neither a prerelease nor 0.0.x.
- The last activity is on 6 Nov 2026, 3 days ago. The rules expect an update within 1200 days. Dormancy rule
Exact rules: Featured library. The check on applied thresholds version 9.
Every rule outcome, as the check records it
expected-interval: version 1.9.1 base 300, doubled for 182570 downloads per month, doubled for 15 active users: E = 1200 daysdormancy: last activity 2026-11-06, 3 days ago, E = 1200: activefeatured: featured: active_users 15, new_adopters 15, former_glory 1
Crates4 crates
Measurements and sources
Each signal row shows the raw value, its source and the fetch date. The Sources list gathers every distinct source on this page. Movement needs more history.
Sources6 sources
- Description: the crates.io description
- crates.io database dump, fetched
- RustSec advisory database, fetched
- GitHub repository, fetched
- crates.io package, fetched
- GitHub contributors, fetched
Adoption15 active dependents, 15 new dependents in 365 days1 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Active dependents | 15 | crates.io database dump | Yes | |
| New dependents (365 days) | 15 | crates.io database dump | Yes | |
| Peak active dependents | 14 | crates.io database dump | Yes | |
| Active dependents as a share of the peak | 1 | crates.io database dump | Yes | |
| Binary downloads per release | Not measured | Yes |
MaintenanceLatest human commit: 3 days ago3 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Latest human commit | 2026-11-06 | GitHub repository | Yes | |
| Latest release | 2026-09-30 | crates.io database dump | Yes | |
| First release | 2025-02-06 | crates.io database dump | Yes | |
| Latest version | 1.9.1 | crates.io database dump | Yes | |
| Days the oldest outside issue waits for a reply | 473 | GitHub repository | Yes | |
| Maintainer replies over 365 days | 2 | GitHub repository | Yes | |
| Repository archived | false | GitHub repository | Yes | |
| Repository gone | false | GitHub repository | Yes | |
| Release commit or manifest in the repo | true | crates.io package | Yes | |
| Published docs | true | crates.io database dump | Yes | |
| Latest release fails cargo check | Not measured | Yes | ||
| Build check date | Not measured | Yes | ||
| Build check toolchain | Not measured | Yes |
SecurityUnpatched vulnerabilities: none
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Unpatched vulnerabilities past the grace window | none | RustSec advisory database | Yes | |
| RustSec unmaintained advisory | none | RustSec advisory database | Yes | |
| Deprecation notice | none | crates.io database dump | Yes | |
| Deprecated requirement | none | crates.io database dump | Yes |
Distribution66 releases, 182,570 downloads over 30 days5 not measured
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| Releases | 66 | crates.io database dump | Yes | |
| Downloads over 30 days | 182570 | crates.io database dump | Yes | |
| Binary downloads over the last 3 releases | Not measured | Yes | ||
| Homebrew installs on request over 365 days | Not measured | Yes | ||
| Repology repository families | Not measured | Yes | ||
| Docker pull growth over 90 days | Not measured | Yes | ||
| Docker pulls at the growth baseline | Not measured | Yes | ||
| Every version a prerelease | false | crates.io database dump | Yes | |
| Every version 0.0.x | false | crates.io database dump | Yes | |
| Every version yanked | false | crates.io database dump | Yes | |
| Crate deleted from crates.io | false | crates.io database dump | Yes |
Repository909 stars, 109 forks, 48 contributors, 1,597 commits. Stars alone never qualify a project.
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| GitHub stars | 909 | GitHub repository | Yes — one part of the repository route | |
| GitHub forks | 109 | GitHub repository | Yes — one part of the repository route | |
| Contributors with a GitHub account | 48 | GitHub contributors | Yes — one part of the repository route | |
| Commits on the default branch | 1597 | GitHub repository | No | |
| Repository's main crate left out of the entry | llguidance | crates.io database dump | Yes |
Facts that never count
| Signal | Value | Source | Fetched | Counts |
|---|---|---|---|---|
| License | MIT | crates.io database dump | No | |
| Runs on, from listed crates and their direct dependencies | GPU support unknown | crates.io database dump | No | |
| What shows each backend | no dependency or feature matches a backend row | crates.io database dump | No | |
| Native code, from listed crates and their dependencies | No native code found | crates.io database dump | No | |
| Python bindings, from direct dependencies of listed crates | No Python binding crate found | crates.io database dump | No | |
| Crates that depend on a listed crate directly | 22 | crates.io database dump | No |
Corrections and review history
No correction applies to this entry.
Judgments
A judgment enters only through an approved issue and a merged pull request.
No judgment applies to this entry.