Skip to main content

logo

Documentation License PyPI PyPI Downloads Ask DeepWiki

Efficient, Flexible and Portable Structured Generation

Get Started | Documentation | Blogpost | Technical Report

News

  • [2026/5] XGrammar-2 has been released! Check out our blog for more information.
  • [2025/12] XGrammar has been officially integrated into Mirai
  • [2025/09] XGrammar has been officially integrated into OpenVINO GenAI
  • [2025/02] XGrammar has been officially integrated into Modular's MAX
  • [2025/01] XGrammar has been officially integrated into TensorRT-LLM.
  • [2024/12] XGrammar has been officially integrated into vLLM.
  • [2024/12] We presented research talks on XGrammar at CMU, UC Berkeley, MIT, THU, SJTU, Ant Group, LMSys, Qingke AI, Camel AI. The slides can be found here.
  • [2024/11] XGrammar has been officially integrated into SGLang.
  • [2024/11] XGrammar has been officially integrated into MLC-LLM.
  • [2024/11] We officially released XGrammar v0.1.0!

Overview

XGrammar is an open-source library for efficient, flexible, and portable structured generation.

It leverages constrained decoding to ensure 100% structural correctness of the output. It supports general context-free grammar to enable a broad range of structures, including JSON, regex, custom context-free grammar, etc.

XGrammar uses careful optimizations to achieve extremely low overhead in structured generation. It has achieved near-zero overhead in JSON generation, making it one of the fastest structured generation engines available.

XGrammar features universal deployment. It supports:

  • Platforms: Linux, macOS, Windows
  • Hardware: CPU, NVIDIA GPU, AMD GPU, Apple Silicon, TPU, etc.
  • Languages: Python, C++, JavaScript, and Swift APIs
  • Models: Qwen, Llama, DeepSeek, Phi, Gemma, etc.

XGrammar is very easy to integrate with LLM inference engines. It is the default structured generation backend for most LLM inference engines, including vLLM, SGLang, TensorRT-LLM, and MLC-LLM, as well as many other companies. You can also try out their structured generation modes!

Get Started

Install XGrammar:

pip install xgrammar

For use with MPS on Apple Silicon, install with:

pip install "xgrammar[metal]"

Import XGrammar:

import xgrammar as xgr

Please visit our documentation to get started with XGrammar.

Third-Party Bindings

  • Rust: xgrammar-rs — Community Rust bindings for XGrammar.

Collaborators

XGrammar has been widely adopted in industry, open-source projects, and academia. Our collaborators include:

WebLLM

Citation

If you find XGrammar useful in your research, please consider citing our papers:

@article{dong2024xgrammar,
  title={Xgrammar: Flexible and efficient structured generation engine for large language models},
  author={Dong, Yixin and Ruan, Charlie F and Cai, Yaxing and Lai, Ruihang and Xu, Ziyi and Zhao, Yilong and Chen, Tianqi},
  journal={Proceedings of Machine Learning and Systems 7},
  year={2024}
}
@inproceedings{10.1145/3786335.3813124,
  author = {Li, Linzhang and Dong, Yixin and Wang, Guanjie and Xu, Ziyi and Jiang, Alexander and Chen, Tianqi},
  title = {XGrammar-2: Dynamic and Efficient Structured Generation Engine for Agentic LLMs},
  year = {2026},
  isbn = {9798400724152},
  publisher = {Association for Computing Machinery},
  address = {New York, NY, USA},
  url = {https://doi.org/10.1145/3786335.3813124},
  booktitle = {Proceedings of the ACM Conference on AI and Agentic Systems},
  pages = {1009--1022},
  numpages = {14}
}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

xgrammar-0.2.5-cp310-cp310-win_amd64.whl (17.1 MB view details)

Uploaded CPython 3.10Windows x86-64

xgrammar-0.2.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (48.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

xgrammar-0.2.5-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (47.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.26+ ARM64manylinux: glibc 2.28+ ARM64

xgrammar-0.2.5-cp310-cp310-macosx_11_0_arm64.whl (25.7 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

xgrammar-0.2.5-cp310-cp310-macosx_10_14_x86_64.whl (25.7 MB view details)

Uploaded CPython 3.10macOS 10.14+ x86-64

File details

Details for the file xgrammar-0.2.5-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: xgrammar-0.2.5-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 17.1 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for xgrammar-0.2.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a0e49cfd0d447247c72f10505c7d6b84696296f68dd03ca294fc1d961db4e786
MD5 34750971acf667e552afc1ca846f8de2
BLAKE2b-256 9f3c70a3f93e91eb191a122a8533bbe3c3fe82381a0c94faecffef367fa9e012

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.5-cp310-cp310-win_amd64.whl:

Publisher: build_and_release.yaml on mlc-ai/xgrammar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xgrammar-0.2.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 69901bd7469127b59e22668a41efc25e9062a591b4596e5a8877fae18bd10832
MD5 b2451deef18b97b6ab5890e531619603
BLAKE2b-256 15cdac223b0e4855148d662b6f45f3c1b43bef92e87f6bb3c8795bd35bc413d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_and_release.yaml on mlc-ai/xgrammar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xgrammar-0.2.5-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.5-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4833b7a5c267c64fe17e1bac5410f39c0555f7411d9cdc3d09bd07dee872c569
MD5 9a159844a07ca8f6034a512c738cf865
BLAKE2b-256 a7afec5949b40202bda9d286825a0f9741691f2dd6a2499167a51037d2803eef

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.5-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl:

Publisher: build_and_release.yaml on mlc-ai/xgrammar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xgrammar-0.2.5-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5c84032e8c67f5e02ea8e920ee0d12fc1fce47a3f6b69e3165b1e2264e9f2f18
MD5 282471ec4e595b007a50673a93766967
BLAKE2b-256 8aad4c3b60cd0e947916f38254e2948e9858a47ff67f6735080fbb911dde18b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.5-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: build_and_release.yaml on mlc-ai/xgrammar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xgrammar-0.2.5-cp310-cp310-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.5-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7038525ffb1f3f2c5cf2dc200332e79f6f2fd836a106e7147d8c6b9c63d787a4
MD5 7085f04b9e8875a29bbe04aefb885ecc
BLAKE2b-256 209da5f6f154b4ac0b8afe1d283ba8e28e2b2e753059de356d66e98e263b02d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.5-cp310-cp310-macosx_10_14_x86_64.whl:

Publisher: build_and_release.yaml on mlc-ai/xgrammar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page