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.4-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (44.8 MB view details)

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

xgrammar-0.2.4-cp312-cp312-macosx_11_0_arm64.whl (23.6 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

xgrammar-0.2.4-cp312-cp312-macosx_10_14_x86_64.whl (23.7 MB view details)

Uploaded CPython 3.12macOS 10.14+ x86-64

xgrammar-0.2.4-cp311-cp311-win_amd64.whl (16.0 MB view details)

Uploaded CPython 3.11Windows x86-64

xgrammar-0.2.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (45.3 MB view details)

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

xgrammar-0.2.4-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (44.8 MB view details)

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

xgrammar-0.2.4-cp311-cp311-macosx_11_0_arm64.whl (23.6 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

xgrammar-0.2.4-cp311-cp311-macosx_10_14_x86_64.whl (23.7 MB view details)

Uploaded CPython 3.11macOS 10.14+ x86-64

xgrammar-0.2.4-cp310-cp310-win_amd64.whl (16.0 MB view details)

Uploaded CPython 3.10Windows x86-64

xgrammar-0.2.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (45.3 MB view details)

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

xgrammar-0.2.4-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (44.8 MB view details)

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

xgrammar-0.2.4-cp310-cp310-macosx_11_0_arm64.whl (23.6 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

xgrammar-0.2.4-cp310-cp310-macosx_10_14_x86_64.whl (23.7 MB view details)

Uploaded CPython 3.10macOS 10.14+ x86-64

File details

Details for the file xgrammar-0.2.4-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b1d2bef13aec8122dfa6b96e3ece2cc0a42a732685f1d44f6248c305274e8eae
MD5 bbcb017674fde520edaa735ecf02cea3
BLAKE2b-256 1810baaac6c9ab3633bc5870d7cad893e6353b8345338610491679d1b61b5cfe

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-cp312-cp312-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.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bb13bda414b04caffb852a5203aa2519665668c7bb4868f1221612a7b9152945
MD5 1c22acb4deb419c2dcf701f02158d70f
BLAKE2b-256 99c461c56f87eea9d49df76cf563099bc1136da10ba899507d2c32a02450d59c

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-cp312-cp312-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.4-cp312-cp312-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2502403e179a73daccc7ea480d63f3a1a636d4b59738bb4dff0469135cc6e523
MD5 cfd561576208d59b4da41d06b550d179
BLAKE2b-256 fb17de9c2d0f2f37d78d4898ded27525bdb9d86817529c1c0dbd5b5ca9cc853f

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-cp312-cp312-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.

File details

Details for the file xgrammar-0.2.4-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: xgrammar-0.2.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 16.0 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xgrammar-0.2.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 93df7b8c8becc410ee2364374d7c8bc5bfd3a7de588598a2ecefb8f46eb8435a
MD5 0bea199b6ba5aebad1a5e84f7a72d40d
BLAKE2b-256 7a374c7e113f493e43086aeefe5fb6b0d9388fb3bcc5e9827d50a27151570a10

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-cp311-cp311-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.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 132a03c1e049b15e6ad146bc152468fadd2e89a4f60ad714055aa1738d577fe7
MD5 eb8735da268419f8c24fc117b1d5d876
BLAKE2b-256 66366f204bbe381c0f511b579ba14671f692f9fedf6c9f3cec2c8f70108a276e

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-cp311-cp311-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.4-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 58f93d721cefbe39bc4fad8d5f238c83cc31b18b931bada4c7a0d91ad6101f47
MD5 55526f09cbc32214e442baffbf742043
BLAKE2b-256 a0b951f0c3a14a552d11223219f70a46e0a0c28fd8c4f7438a306740e3c92edb

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-cp311-cp311-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.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 82a38f7e6857b4f76fc64b57c2bf674ff332a292c23e1f1cefa36981fd844dcd
MD5 3721f4c7459848de9bc15a7598c27c2b
BLAKE2b-256 e72efcf8295075ea3468b8df299da5ef7c62243ffeb2cf6ecba0c9fd86072a6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-cp311-cp311-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.4-cp311-cp311-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp311-cp311-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 22a552d7cac3802339a28b0eec9ca3bcc93148171d288cb0534b3cf6339b30a3
MD5 2b383956ea30b9da4f9bacb7dcab0d11
BLAKE2b-256 92ce0c2a535690044279af195126bb4ddf360d2ad2b7eeb783c2a19f73607e1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-cp311-cp311-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.

File details

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

File metadata

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

File hashes

Hashes for xgrammar-0.2.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 65bf95226d4994fc3646be40a695ed580093d5670308edb8c12a4a665d13ac8d
MD5 46969b80c0d1031fc042f1cf00100192
BLAKE2b-256 ae1142647fb77b8249f0647ac73f5fa92bbfd5b20ca19f09868204f09ac8d244

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-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.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eba1253b26f9d3fa94ee846ea0336d35f6fcd658de7218cb2e92536474985ca1
MD5 d9737c35935fb8fa1168067ef4d99219
BLAKE2b-256 c8e7be9bdcce19f42dd5a1ac3a05f0eea7c52d0c3c742a59216f795df14e9329

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-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.4-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5b1955daa6207f612aaec336dda5085d2d866e70d93c26f0049ac0bf9e5db7f3
MD5 a1c71f30872e04e5fe8ba2d0ce858406
BLAKE2b-256 ffb7bdeb55b996c64672e78abf282bc3e1c9f836c0811d29e8fe2105432a9ef9

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-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.4-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 faa5756997edfa3e96ac5c254fb8348274e5831ec166a3a19bd9ed4956dfa5ab
MD5 f8fb8c2f83bd02b0430e402bb031929a
BLAKE2b-256 f4e7cf1690d674547a7f4326fe6c7cba93d67bdf3714149e7d8f543b4fcaef4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-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.4-cp310-cp310-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for xgrammar-0.2.4-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 3ef0bb14629e5f202b80c0ad22c62513d904f67519c0b58a685fc72f0b2270b1
MD5 93adeec3447b65a0a03c7a586dd10805
BLAKE2b-256 7b78937e3b96bd60d09075971f71f59829e0c0c38e4af54f6a10c8fe11d121b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgrammar-0.2.4-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