When humans interact with databases using natural language, a semantic parser must translate their intent into a structured query language like SQL, SPARQL, or Cypher. Large Language Models (LLMs) have dramatically improved this process, but they suffer from a fundamental flaw: they treat query generation primarily as a text-to-text translation task.
Because language models excel at surface-level pattern matching, they frequently generate queries that are syntactically perfect but factually ungrounded. They invent non-existent table names, hallucinate column identifiers, and guess relationship labels that do not exist in the target database. This phenomenon—often called schema hallucination—stems from a lack of environment awareness. The model generates what sounds like a plausible query based on its internal weights, rather than strictly adhering to the rigid, underlying schema of the data store.
The Root Cause: Structural Blindness and Over-Eagerness
To understand why schema hallucination happens, we must look at how language models process database schemas and user queries. Standard text-to-query systems feed a flattened representation of the database schema into the model's prompt. However, as Rai et al. observe in Improving Generalization in Language Model-based Text-to-SQL Semantic Parsing: Two Simple Semantic Boundary-based Techniques, standard tokenizers often fail to respect the semantic boundaries of schema identifiers. When boundaries blur, the model struggles to map natural language entities precisely to specific database columns.
This mapping failure is compounded by two main factors:
- Information Overload: When schemas grow large or contain inconsistent naming conventions, models become overwhelmed by the noise. As Pei et al. point out in Feather-SQL: A Lightweight NL2SQL Framework with Dual-Model Collaboration Paradigm for Small Language Models, this information overload directly triggers the hallucination of incorrect table or column names.
- Over-Eagerness: Language models are trained to be helpful, which makes them inherently "over-eager." As documented by Wang et al. in Know What I don't Know: Handling Ambiguous and Unknown Questions for Text-to-SQL and Dong et al. in PRACTIQ: A Practical Conversational Text-to-SQL dataset with Ambiguous and Unanswerable Queries, when a user asks an ambiguous or unanswerable question, the model rarely signals its confusion. Instead, it fails silently by guessing irrelevant schema elements to force-complete the request.
The result is a query that looks correct to a casual observer but fails during execution or, worse, returns entirely incorrect data.
Closing the Loop with Environmental Feedback
To prevent models from hallucinating in a vacuum, a major direction of research focuses on giving the model "eyes" via environmental feedback. If the model can execute its candidate query against the actual database engine or a compiler during the generation process, it can use the resulting error messages to self-correct.
[User Query] ──> [LLM Generator] ──> [Candidate Query] ──> [Database/Compiler]
│
┌──────────────────────────────────────────────────────────┘
▼
[Execution Success?]
├── YES ──> Return Data
└── NO ──> [Error Message] ──> [Self-Correction Module] ──> (Loop back to LLM)
In QueryAgent: A Reliable and Efficient Reasoning Framework with Environmental Feedback based Self-Correction, Huang et al. introduce a framework called ERASER that intercepts database execution errors and feeds them back to the model. If the model hallucinates a relation, the database rejects it, and the model uses that explicit rejection to rewrite the query. Similarly, Wan et al. demonstrate in Iterative Refinement of Project-Level Code Context for Precise Code Generation with Compiler Feedback that treating a compiler or search index as an interactive guide allows the model to iteratively discover missing context and repair invalid query logic.
To make this interaction efficient, Cheng et al. propose a "lazy evaluation" paradigm in Call Me When Necessary: LLMs can Efficiently and Faithfully Reason over Structured Environments. Instead of verifying every single step interactively, the model generates a full reasoning path upfront and is only "called" to edit the path when a specific grounding failure occurs against the schema.
Intermediate Representations as Stepping Stones
Another powerful strategy for mitigating hallucination is to break the translation task into smaller, more manageable sub-tasks using intermediate representations. Going directly from natural language to a complex, rigid syntax like SQL or SPARQL requires the model to handle abstract logic and strict syntax simultaneously.
Qu et al. address this in Before Generation, Align it! A Novel and Effective Strategy for Mitigating Hallucinations in Text-to-SQL Generation by introducing an alignment framework called TA-SQL. Before generating the final query, the model is forced to generate a "dummy SQL" structure for schema linking, alongside a pandas-like intermediate representation. This decouples schema mapping from final syntax generation.
On a similar note, Eyal et al. introduce Query Plan Language (QPL) in Semantic Decomposition of Question and SQL for Text-to-SQL Parsing. QPL decomposes tangled SQL into a step-by-step execution plan, making it easier for the model to maintain logical consistency without losing track of the schema constraints. By separating high-level reasoning from low-level query generation—a technique also explored by Xu in Memory-augmented Query Reconstruction for LLM-based Knowledge Graph Reasoning—systems can drastically reduce "tool-use hallucinations."
Verification and Hypothesis Re-ranking
When an LLM generates a query, it often outputs subtle structural errors (such as missing join conditions) that look perfectly reasonable during decoding. To catch these before they reach the database, researchers have built "double-check" mechanisms based on back-translation and verification.
In Improving NL-to-Query Systems through Re-ranking of Semantic Hypothesis, von Däniken et al. demonstrate that taking candidate SQL queries, translating them back into natural language, and measuring how well they match the user's original question serves as an excellent filter. If the back-translated text diverges from the user's intent, it reveals that the generated query was grounded in a hallucinated interpretation of the schema.
This multi-hypothesis verification approach is echoed by Xu et al. in TURING: an Accurate and Interpretable Multi-Hypothesis Cross-Domain Natural Language Database Interface, which provides step-by-step natural language explanations of multiple generated query hypotheses. By making the underlying schema mappings transparent, these systems allow either automated verifiers or human users to select the hypothesis that is genuinely grounded in the actual database structure.
Key Foundational Work
TURING: an Accurate and Interpretable Multi-Hypothesis Cross-Domain Natural Language Database Interface (2021) — contributed 1 concept elements to this direction.
Improving NL-to-Query Systems through Re-ranking of Semantic Hypothesis (2022) — contributed 1 concept elements to this direction.