XGrammar 2: Dynamic and Efficient Structured Generation Engine for Agentic LLMs

Linzhang Li, Yixin Dong, Guanjie Wang, Ziyi Xu, Alexander Jiang +1 more
1/7/2026
cs.AI

No confident code implementation found for this paper. 5 possible matches can be shown below.

Abstract

Modern LLM agents are required to handle increasingly complex structured generation tasks, such as tool calling and conditional structured generation. These tasks are significantly more dynamic than predefined structures, posing new challenges to the current structured generation engines. In this paper, we propose XGrammar 2, a highly optimized structured generation engine for agentic LLMs. XGrammar 2 accelerates the mask generation for these dynamic structured generation tasks through a new dynamic dispatching semantics: TagDispatch. We further introduce a just-in-time (JIT) compilation method to reduce compilation time and a cross-grammar caching mechanism to leverage the common sub-structures across different grammars. Additionally, we extend the previous PDA-based mask generation algorithm to the Earley-parser-based one and design a repetition compression algorithm to handle repetition structures in grammars. Evaluation results show that XGrammar 2 can achieve more than 6x speedup over the existing structured generation engines. Integrated with an LLM inference engine, XGrammar 2 can handle dynamic structured generation tasks with near-zero overhead.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. 5 weaker matches are hidden by default — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{li2026xgrammar,
  title  = {XGrammar 2: Dynamic and Efficient Structured Generation Engine for Agentic LLMs},
  author = {Linzhang Li and Yixin Dong and Guanjie Wang and Ziyi Xu and Alexander Jiang and Tianqi Chen},
  year   = {2026},
  eprint = {2601.04426},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2601.04426v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2601.04426.svg)](https://codeofpaper.com/paper/2601.04426)

Discussion