NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Synchromesh: Reliable Code Generation from Pre-trained Language Models

Gabriel Poesia, Alex Polozov, Vu Le, Ashish Tiwari, Gustavo Soares, Christopher Meek, Sumit Gulwani

Large Language Models (LLMs) like GPT-3 and Codex have revolutionized how we think about programming. We can now describe a task in plain English and get a snippet of code in return. However, users frequently encounter a common frustration: the model often produces code that looks right but fails to execute because of a missing bracket, a made-up function name, or a complete misunderstanding of the database schema.

In their ICLR 2022 paper, researchers from Stanford and Microsoft introduce SYNCHROMESH, a framework designed to turn these "hallucinating" models into reliable code generators by enforcing correctness at every step of the process.

TL;DR

SYNCHROMESH improves the reliability of LLM code generation by combining two novel techniques: Target Similarity Tuning (TST), which selects better few-shot examples for prompts based on program structure, and Constrained Semantic Decoding (CSD), which forces the model to only sample tokens that result in syntactically and semantically valid code.

What Problem Does This Paper Address?

The authors identify two primary types of failures in LLM-generated code:

  1. Conceptual Errors: The model misunderstands the user's intent (e.g., swapping the first and last elements instead of reversing the whole array).
  2. Implementation Errors: The model generates code that is fundamentally broken—syntax errors, type mismatches, or references to variables that don't exist.

While scaling models to hundreds of billions of parameters helps, it doesn't solve the "ungroundedness" of LLMs. They learn from text, but they don't inherently "understand" the rigid execution rules of a programming language. SYNCHROMESH aims to bridge this gap without requiring expensive retraining of the underlying LLM.

Methods

SYNCHROMESH operates as a two-stage pipeline:

Overview of the Synchromesh framework.

1. Target Similarity Tuning (TST)

LLMs are highly sensitive to the "few-shot" examples provided in their prompt. Standard practice is to retrieve examples with similar natural language descriptions. TST goes deeper: it fine-tunes an embedding model (like Sentence-BERT) to recognize when two natural language utterances describe similar program structures, even if the words used are very different. This ensures the LLM sees examples that "look" like the logic it needs to generate.

2. Constrained Semantic Decoding (CSD)

This forms the core of the framework. CSD acts as a "clutch" between the LLM and the programming language.

CSD prevents schema hallucinations by constraining the output to valid SQL.
  • Completion Engines (CE): For a given language (like SQL), a CE knows the grammar and the context (e.g., the database schema). At any point in the code generation, the CE provides a regular expression of all valid next tokens.
  • Handling Token Misalignment: A major technical hurdle is that LLM tokens (like Byte-Pair Encodings) rarely align perfectly with programming language tokens (keywords, identifiers). One LLM token might contain the end of a variable name and the start of a bracket. CSD uses Brzozowski language derivatives to efficiently determine if a partial LLM token can eventually be completed into something valid according to the CE.

Datasets

The authors evaluated SYNCHROMESH across three distinct, real-world domains:

  • Spider: A large-scale benchmark for complex natural language to SQL tasks.
  • NLV Corpus: A dataset for generating Vega-Lite visualizations (JSON-based).
  • SMCalFlow: A task-oriented dialogue dataset involving complex calendar and task API calls.

Key Insights and Ideas

  • Structure over Surface: TST proves that for code generation, retrieving examples based on "how the code should look" is far more effective than "how the user's question sounds."
  • Zero-Shot Constraints: CSD works on top of frozen models. You don't need to fine-tune Codex to make it follow SQL rules; you just need to restrict its choices during the "sampling" phase.
  • The Power of "No": By simply preventing the model from ever picking an invalid token, SYNCHROMESH forces the model's probability mass toward valid alternatives, often leading it to the correct answer it might have otherwise ignored.

Key Results

The results show that SYNCHROMESH provides massive gains, especially for smaller or less specialized models:

  • Validity: In Vega-Lite, SYNCHROMESH improved the "validity" of GPT-3 13B's output from 55% to 100%. In SQL, it eliminated execution errors for nearly 30% of queries.
  • Accuracy: On the SMCalFlow task, combining TST and CSD boosted GPT-3 13B's accuracy from 38% to 63%, nearly matching the performance of much larger models.
  • Efficiency: Unlike "generate-then-test" methods (which sample many programs and throw away the broken ones), CSD is highly efficient, adding only about 8% overhead to the decoding process.

Limitations and Future Work

While SYNCHROMESH is a major step forward, the authors note a few limitations:

  • The "Intent" Ceiling: CSD can guarantee that code is valid, but it can't guarantee it's what the user wanted. If the model is fundamentally confused about the logic, CSD might just force it to generate a "valid" but incorrect program.
  • Domain Expertise Required: To use SYNCHROMESH, you need to build a "Completion Engine" for your target language. While the authors provide a way to do this automatically from a grammar (using ANTLR), semantic constraints (like checking if a column exists in a specific database) still require manual implementation.
  • Turing-Completeness: The study focused on domain-specific languages. Extending this to general-purpose, Turing-complete languages like Python or C++ remains a significant future challenge due to their much more complex scoping and type-checking rules.

Summary updated

BibTeX
@inproceedings{poesia2022synchromesh,
  author = {Gabriel Poesia and Alex Polozov and Vu Le and Ashish Tiwari and Gustavo Soares and Christopher Meek and Sumit Gulwani},
  title = {{Synchromesh: Reliable Code Generation from Pre-trained Language Models}},
  booktitle = {International Conference on Learning Representations},
  year = {2022},
  url = {https://openreview.net/forum?id=hWlGUjxGyY},
}
Figure