Abstract
We present a practical framework for detecting errors in LLM-generated SQL by estimating uncertainty at the level of individual nodes in the query's abstract syntax tree (AST). Our approach proceeds in two stages. First, we introduce a semantically aware labeling algorithm that, given a generated SQL and a gold reference, assigns node-level correctness without over-penalizing structural containers or alias variation. Second, we represent each node with a rich set of schema-aware and lexical features—capturing identifier validity, alias resolution, type compatibility, ambiguity in scope, and typo signals—and train a supervised classifier to predict per-node error probabilities. Across multiple databases and datasets, our method substantially outperforms token log-probabilities: average AUC improves by +27.44% while maintaining robustness under cross-database evaluation. Together, these results establish node-centric, semantically grounded uncertainty estimation as a strong and interpretable alternative to aggregate sequence-level confidence measures.
Introduction
Large Language Models (LLMs) have significantly advanced the field of Text-to-SQL translation. However, these models frequently produce "semantic errors"—queries that are syntactically valid but logically incorrect, such as selecting the wrong column or applying an incorrect filter. Traditional error detection relies on aggregate confidence measures like token log-probabilities. These measures often suffer from LLM overconfidence and lack the granularity needed to pinpoint specific errors within a complex query.
This research shifts the focus to node-level uncertainty, providing fine-grained diagnostics that can facilitate targeted human-in-the-loop reviews or automated agentic repairs.
Methodology
The proposed framework consists of a two-stage supervised learning pipeline: semantically aware labeling and supervised node classification.
Semantically Aware Labeling
To train an effective error detector, precise ground-truth labels are required. Simple text-based comparison between generated SQL and a gold reference is insufficient due to the flexibility of SQL syntax (e.g., alias naming, operator ordering). The authors developed a two-pass algorithm to align the generated AST ($T_{gen}$) with the gold AST ($T_{gold}$).
The algorithm first performs a recursive alignment pass. A critical innovation is the "global rescue" pass (Steps 13-15), which prevents "over-labeling." If a node is initially marked as an error due to a structural mismatch but actually exists elsewhere in the gold tree, it is relabeled as correct. This ensures that structural containers do not unfairly suppress the correctness of their children.
Supervised Node Classification
Each node in the AST is featurized using 72 distinct signals categorized into:
- Base Structural: Node type and tree position.
- Schema Consistency: Verification of column/table existence and type compatibility.
- Lexical/Typo Signals: Levenshtein distance checks to identify "near-miss" typos of valid schema elements.
A Gradient Boosted Decision Tree (LightGBM) is then trained to predict the probability of error for each node.
Experimental Results
The framework was evaluated using the BIRD and SynSQL-2.5M datasets, with SQL generated by the OmniSQL-7B model.
Performance Gains
The supervised approach demonstrated a massive accuracy boost over standard LLM confidence scores. The average AUC improvement was +27.44% compared to the arithmetic mean of token log-probabilities.
As shown in the results table, the model is particularly effective at identifying errors in Literals (69.59% AUC) and Identifiers (63.91% AUC). In contrast, log-probabilities often perform near random chance (AUC ~50%), highlighting that LLMs are frequently confident even when selecting incorrect schema elements.
Robustness
Cross-database evaluation confirmed that the schema-aware features generalize well. Even when tested on entirely unseen databases, the model maintained its performance advantage, proving that the signals of "uncertainty" in SQL generation are consistent across domains.
Conclusion and Future Directions
Node-level uncertainty estimation provides a more interpretable and accurate alternative to sequence-level confidence. By grounding error detection in the database schema and the AST structure, this framework enables more reliable deployment of Text-to-SQL systems. Future work will focus on "reference-free" estimation to remove the dependency on gold queries and integrating these signals into self-correcting agentic workflows where an AI agent can automatically repair specific high-uncertainty nodes.
Summary updated