Type-Constrained Code Generation with Language Models
Niels Mündler, Jingxuan He, Hao Wang, Koushik Sen, Dawn Song, Martin T. Vechev
Abstract
Large language models (LLMs) have achieved notable success in code generation. However, they still frequently produce uncompilable output because their next-token inference procedure does not model formal aspects of code. Although constrained decoding is a promising approach to alleviate this issue, it has only been applied to handle either domain-specific languages or syntactic features of general-purpose programming languages. However, LLMs frequently generate code with typing errors, which are beyond the domain of syntax and generally hard to adequately constrain. To address this challenge, we introduce a type-constrained decoding approach that leverages type systems to guide code generation. For this purpose, we develop novel prefix automata and a search over inhabitable types, forming a sound approach to enforce well-typedness on LLM-generated code. We formalize our approach on a foundational simply-typed language and extend it to TypeScript to demonstrate practicality. Our evaluation on the HumanEval and MBPP datasets shows that our approach reduces compilation errors by more than half and significantly increases functional correctness in code synthesis, translation, and repair tasks across LLMs of various sizes and model families, including state-of-the-art open-weight models with more than 30B parameters. The results demonstrate the generality and effectiveness of our approach in constraining LLM code generation with formal rules of type systems.
Ask Lune
Know this paper in minutes, not an afternoon.
Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.
14 top-tier papers have cited it since. Know what they found before your reviewers do.
Lune can draw on
the full paper · the top-tier papers that cite it · related work
Free to start. No credit card required.
Lineage
- ChopChop: A Programmable Framework for Semantically Constraining the Output of Language Models cites Synchromesh: Reliable Code Generation from Pre-trained Language Models
- Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions cites Language Models are Few-Shot Learners
- Equality Saturation Theory Exploration à la Carte cites ChopChop: A Programmable Framework for Semantically Constraining the Output of Language Models
- Constrained Decoding of Diffusion LLMs with Context-Free Grammars cites SWT-Bench: Testing and Validating Real-World Bug-Fixes with Code Agents
- Constrained Decoding of Diffusion LLMs with Context-Free Grammars cites Language Models are Few-Shot Learners
- Constrained Decoding of Diffusion LLMs with Context-Free Grammars cites Synchromesh: Reliable Code Generation from Pre-trained Language Models
- RefineStat: Efficient Exploration for Probabilistic Program Synthesis cites Synchromesh: Reliable Code Generation from Pre-trained Language Models
- Synchromesh: Reliable Code Generation from Pre-trained Language Models cites Language Models are Few-Shot Learners
- Prompting Is Programming: A Query Language for Large Language Models cites Language Models are Few-Shot Learners
- Prompting Is Programming: A Query Language for Large Language Models cites Synchromesh: Reliable Code Generation from Pre-trained Language Models
Cited by top-tier papers14
- Unveiling the Potential of Diffusion Large Language Model in Controllable GenerationZhen Xiong, Yujun Cai, Zhecheng Li, Yiwei WangICLR 2026 · 19 citations
- Constrained Decoding of Diffusion LLMs with Context-Free GrammarsNiels Mündler, Jasper Dekoninck, Martin VechevICLR 2026 · 18 citations
- Equality Saturation Theory Exploration à la CarteAnjali Pal, Brett Saiki, Ryan Tjoa, Cynthia Richey et al.OOPSLA 2023 · 11 citations
- RefineStat: Efficient Exploration for Probabilistic Program SynthesisMadhav Kanda, Shubham Ugare, Sasa MisailovicICLR 2026 · 5 citations
- ChopChop: A Programmable Framework for Semantically Constraining the Output of Language ModelsShaan Nagy, Timothy Zhou, Nadia Polikarpova, Loris D'AntoniPOPL 2026 · 1 citation
Builds on16
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah et al.NeurIPS 2020 · 64,255 citations
- Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code ContributionsHammond Pearce, Baleegh Ahmad, Benjamin Tan, Brendan Dolan-Gavitt et al.S&P 2022 · 725 citations
- Synchromesh: Reliable Code Generation from Pre-trained Language ModelsGabriel Poesia, Alex Polozov, Vu Le, Ashish Tiwari et al.ICLR 2022 · 200 citations
- SWT-Bench: Testing and Validating Real-World Bug-Fixes with Code AgentsNiels Mündler, Mark Niklas Müller, Jingxuan He, Martin T. VechevNeurIPS 2024 · 172 citations
- Prompting Is Programming: A Query Language for Large Language ModelsLuca Beurer-Kellner, Marc Fischer, Martin T. VechevPLDI 2023 · 114 citations
Related papers
- Towards Understanding the Characteristics of Code Generation Errors Made by Large Language ModelsZhijie Wang, Zijie Zhou, Da Song, Yuheng Huang et al.ICSE 2025 · 12 citations
- IntentCoding: Amplifying User Intent in Code GenerationZheng Fang, Yihong Dong, Lili Mou, Dongming Jin et al.ACL Findings 2026
- CodeT5+: Open Code Large Language Models for Code Understanding and GenerationYue Wang, Hung Le, Akhilesh Gotmare, Nghi D. Q. Bui et al.EMNLP 2023 · 339 citations
- ContractEval: A Benchmark for Evaluating Contract-Satisfying Assertions in Code GenerationSoohan Lim, Joonghyuk Hahn, Hyunwoo Park, Sang-Ki Ko et al.ACL Findings 2026
- HumanEval Pro and MBPP Pro: Evaluating Large Language Models on Self-invoking Code Generation TaskZhaojian Yu, Yilun Zhao, Arman Cohan, Xiaoping ZhangACL Findings 2025 · 13 citations
