POPL 2027
Sun 10 - Sat 16 January 2027 Mexico City, Mexico

Constrained decoding ensures certain correctness properties of the generated code by large language models (LLMs) without scaling up or fine-tuning the models. When ensuring semantic correctness, the key of constrained decoding is to perform static analysis on intermediate partially generated code to check if it can be extended to semantically correct and complete code. However, in prior work on constrained decoding for semantic correctness, what to check, when to check, and how to check are tightly coupled in a single monolithic design, limiting their applicability and flexibility. In this work, we propose attribute-guided constrained decoding that cleanly separates what to check, when to check, and how to check. The key idea is to use attributes propagated along with the parse stack during decoding to indicate when to invoke static analysis and what part of the generated code snippet to analyze. By separating how to check from what and when to check, our approach can directly use the queries that existing static analyzers answer on incomplete code, so custom analysis is needed only for queries they lack. We implement our approach atop a tool named GenCor for preventing name resolution errors in Python and OCaml and type errors in TypeScript, and show that it effectively prevents those errors across various LLMs with moderate overhead. It matches a state-of-the-art type-constrained decoder and outperforms a programmable constrained-decoding framework and rejection sampling in most settings.