We use a few cookies

Essential cookies keep the library working. With your permission, we'd also like to use analytics to see what's useful and improve the experience. Read our Privacy Policy or Cookie Policy.

Skip to main content
Home/e-Resources/Mathematics/SynCode: LLM Generation with Grammar Augmentation
PeriodicalsMathematics

SynCode: LLM Generation with Grammar Augmentation

Checking your access to this record.

Abstract & Overview

LLMs are widely used in complex AI applications. These applications underscore the need for LLM outputs to adhere to a specific format, for their integration with other components in the systems. Typically the format rules e.g., for data serialization formats such as JSON, YAML, or Code in Programming Language are expressed as context-free grammar (CFG). Due to the hallucinations and unreliability of LLMs, instructing LLMs to adhere to specified syntax becomes an increasingly important challenge. We present SynCode, a novel framework for efficient and general syntactical decoding with LLMs, to address this challenge. SynCode ensures soundness and completeness with respect to the CFG of a formal language, effectively retaining valid tokens while filtering out invalid ones. SynCode uses an offline-constructed, efficient lookup table, the DFA mask store, derived from the DFA of the language's grammar for efficient generation. SynCode seamlessly integrates with any language defined by CFG, as evidenced by experiments focusing on generating JSON, Python, and Go outputs. Our experiments evaluating the effectiveness of SynCode for JSON generation demonstrate that SynCode eliminates all syntax errors and significantly outperforms state-of-the-art baselines. Furthermore, our results underscore how SynCode significantly reduces 96.07% of syntax errors in generated Python and Go code, showcasing its substantial impact on enhancing syntactical precision in LLM generation. Our code is available at https://github.com/uiuc-focal-lab/syncode

Cite this record

Shubham Ugare, Tarun Suresh, Kang, Hangoo, Saša Misailovíc, Gagandeep Singh (2025). SynCode: LLM Generation with Grammar Augmentation. Transactions on machine learning research. https://doi.org/10.48550/arxiv.2403.01632

Copy in another style

Ugare, S., Suresh, T., Hangoo, K., Misailovíc, S., & Singh, G. (2025). SynCode: LLM Generation with Grammar Augmentation. Transactions on machine learning research. https://doi.org/10.48550/arxiv.2403.01632
Author names are used as deposited, so check their order before you submit.

Metadata is harvested from open-access indexes and can be incomplete. Check a citation against the published version before relying on it — see our open access & attribution policy.