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

Text-to-SQL Error Correction with Language Models of Code

Ziru Chen, Shijie Chen, Michael White, Raymond J. Mooney, Ali Payani, Jayanth Srinivasa, Yu Su, Huan Sun

Introduction

Text-to-SQL parsing—the conversion of natural language questions into executable SQL code—is essential for making databases accessible to non-experts. However, state-of-the-art semantic parsers frequently produce incorrect queries, particularly when faced with complex logic or unfamiliar domains. When these systems fail, users are often forced to manually debug the SQL, a process that is time-consuming and requires significant technical expertise.

Traditional approaches to automatic error correction typically operate at the token level, attempting to replace specific words within a string. This paper identifies two primary flaws in such methods: ambiguity in identifying which specific token to change (e.g., when a column name appears multiple times) and a domain mismatch between SQL syntax and the pre-training data of large Language Models of Code (LMs). Most powerful LMs, such as CodeT5, are trained predominantly on Python or Java, making SQL's structure relatively unfamiliar.

Methodology: PyDict and Program-Based Edits

To address these challenges, we propose a novel error-correction framework that aligns SQL structures with the data formats LMs are most proficient in. Our method introduces two key innovations:

  1. PyDict Representation: Instead of treating a SQL query as a raw string, it is parsed into an Abstract Syntax Tree (AST) and converted into a Python Dictionary. For instance, the SELECT clause is mapped to a value for the key "select".
  2. Program-Based Edits: Rather than using specialized edit tokens, corrections are formatted as executable Python snippets. To modify an ORDER BY clause, the model generates a line of code such as sql["orderBy"] = "order by tweets.createdate".

By framing error correction as Python dictionary manipulation, the task mirrors the code-centric patterns the models encountered millions of times during pre-training.

Comparison of SQL and PyDict query representations alongside token-level, clause-level, and program-based edit formats.

Experimental Setup

The study utilizes the Spider dataset, a large-scale, cross-domain Text-to-SQL benchmark. To train the error correction model, we synthesized a dataset of "natural" errors by collecting incorrect predictions from three base parsers: CodeT5, BRIDGEv2, and SmBoP. This resulted in a specialized training set of thousands of erroneous queries paired with their correct "gold" versions.

Key Insights: Aligning with Model Pre-training

The most significant finding of this research is the impact of representation on model understanding. The zero-shot perplexity—a metric indicating how "surprised" a model is by a sequence—of the PyDict + Program format was found to be three orders of magnitude lower than that of standard SQL formats. This confirms that LMs "understand" SQL significantly better when it is represented as a Python data structure.

Zero-shot perplexity of different query and edit representations, showing a significant reduction for the PyDict + Program format.

Furthermore, clause-level edits proved superior to token-level edits by providing necessary context. By replacing an entire clause rather than a single word, the model avoids the ambiguity inherent in repeated tokens. Analysis of error types revealed that while the model is highly effective at fixing "Database Grounding" errors (e.g., incorrect column names), "Structural Errors" involving complex joins or logic remain a significant challenge.

Results and Performance

The proposed method consistently outperformed strong baselines across all tested base parsers. Key results include:

  • Accuracy Improvements: The framework improved the Exact Set Match (EM) accuracy of base parsers by 2.4 to 6.5 absolute points.
  • Representation Advantage: The PyDict + Program representation outperformed the previous state-of-the-art edit format (CoditT5) by up to 4.3 points, even when using the same underlying model.
  • Interactive Potential: In simulated interactive settings where a user selects from a list of model suggestions, accuracy gains were even more pronounced (e.g., BRIDGEv2 accuracy rose from 72.5% to 76.6%).

Conclusion and Future Work

Current limitations include the assumption that every input query requires correction; a real-world implementation would require a reliable "error detector" to trigger the model. Additionally, the current PyDict format lacks detailed schema information, such as column types or foreign keys, which could further improve grounding accuracy. Future research will explore multi-step edits and more sophisticated human-in-the-loop collaboration frameworks for complex query fixes.

Summary updated

BibTeX
@inproceedings{chen2023text,
  author = {Ziru Chen and Shijie Chen and Michael White and Raymond J. Mooney and Ali Payani and Jayanth Srinivasa and Yu Su and Huan Sun},
  title = {{Text-to-SQL Error Correction with Language Models of Code}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-short.117},
}
Figure