Source-linked AI summary

Before Generation, Align it! A Novel and Effective Strategy for Mitigating Hallucinations in Text-to-SQL Generation

Ge Qu, Jinyang Li, Bowen Li, Bowen Qin, Nan Huo, Chenhao Ma, Reynold Cheng

arXiv:2405.15307v1cs.CL

TL;DR

LLM text-to-SQL systems can hallucinate during schema linking and logical synthesis, limiting reliable automatic parsing. The paper proposes Task Alignment, implemented in TA-SQL’s TASL and TALOG modules, to draw on familiar tasks. TA-SQL reports a 21.23% relative improvement in GPT4 baseline Execution Accuracy on BIRD and improvements across multiple datasets and models.

  • Problem

    Hallucinations in schema linking and logical synthesis remain a barrier to reliable automatic text-to-SQL parsing.

    Source: §1 · pp. 1–2
  • Method

    Task Alignment uses experience from related pretrained tasks, and TA-SQL applies it through TASL and TALOG to address hallucinations at both stages.

    Source: §1 · p. 2, §3.1 · p. 4
  • Results

    21.23% relative improvement in Execution Accuracy (EX) over the GPT4 baseline on BIRD is reported, alongside gains across four datasets and applicability to closed- and open-source LLMs.

    Source: §1 · p. 3
  • Takeaways & Limitations

    The paper’s analysis presents hallucination research in text-to-SQL and data science as important and suggests directions for future work.

    Source: §6 · p. 9

    Task selection for each phase is manual, and the authors note direct SQL generation can be more effective when questions are clear and evidence is sufficient.

    Source: §7 · p. 10

Abstract

from arXiv · show

Large Language Models (LLMs) driven by In-Context Learning (ICL) have significantly improved the performance of text-to-SQL. Previous methods generally employ a two-stage reasoning framework, namely 1) schema linking and 2) logical synthesis, making the framework not only effective but also interpretable. Despite these advancements, the inherent bad nature of the generalization of LLMs often results in hallucinations, which limits the full potential of LLMs. In this work, we first identify and categorize the common types of hallucinations at each stage in text-to-SQL. We then introduce a novel strategy, Task Alignment (TA), designed to mitigate hallucinations at each stage. TA encourages LLMs to take advantage of experiences from similar tasks rather than starting the tasks from scratch. This can help LLMs reduce the burden of generalization, thereby mitigating hallucinations effectively. We further propose TA-SQL, a text-to-SQL framework based on this strategy. The experimental results and comprehensive analysis demonstrate the effectiveness and robustness of our framework. Specifically, it enhances the performance of the GPT-4 baseline by 21.23% relatively on BIRD dev and it yields significant improvements across six models and four mainstream, complex text-to-SQL benchmarks.

1 Introduction

LLM text-to-SQL commonly separates schema linking from logical synthesis, but hallucinations at both stages threaten reliable parsing. The paper proposes Task Alignment and TA-SQL to address these errors, reporting improvements across benchmarks.

1 Introduction

  • LLM text-to-SQL frameworks generally use schema linking to map queries to schema entities, followed by logical synthesis to generate SQL from query logic and database structure.The paper describes this two-stage approach as effective and interpretable.Source: §1 · p. 1
  • The paper identifies schema-based and logic-based hallucinations, including schema misidentification and errors in joins, SQL clauses, nested queries, or computational reasoning.These errors are presented as barriers to reliable automatic text-to-SQL parsing.Source: §1 · pp. 1–2
  • Task Alignment uses experience from previously trained, familiar tasks to reduce reliance on generalization from scratch and mitigate hallucinations.TA-SQL applies it through Task-Aligned Schema Linking (TASL) and Task-Aligned LOGical synthesis (TALOG).Source: §1 · p. 2
  • 21.23% relative improvement in Execution Accuracy (EX) over the GPT4 baseline on BIRD and 14.86% on SPIDER are reported for TA-SQL.The framework is also described as applicable to closed-source and open-source LLMs.Source: §1 · p. 3

2 Preliminaries

The preliminaries frame text-to-SQL as generating an SQL query from a problem and database schema. They distinguish few-shot prompting with demonstrations from zero-shot prompting based on pretrained knowledge.

2 Preliminaries

  • Text-to-SQL generates a corresponding SQL query from an input problem, with database schema represented through columns and tables.The passage names the column and table collections C and T and their sizes |C| and |T|.Source: §2 · p. 3
  • Few-shot prompting supplies task instructions and input-output demonstrations to help an LLM solve an input problem belonging to a task.The demonstrations are represented as pairs of inputs and outputs.Source: §2 · p. 3
  • Zero-shot prompting removes the input-output demonstrations and expects the model to complete the task using pretrained knowledge.The passage describes this as the transition when I/O pairs are no longer provided.Source: §2 · p. 3

3 Methodology

Task Alignment (TA) uses experience from familiar pre-trained tasks to approach unfamiliar tasks, and TA-SQL applies this strategy across schema linking and logical synthesis while preserving interpretability.

3.1 Task Alignment

  • TA retrieves a related pre-trained task and uses its experience to reconstruct the representation of a novel task, reducing reliance on from-scratch generalization.In this study, the related task is selected manually.Source: §3.1 · p. 4

3.2 TA-SQL

  • TA-SQL applies task alignment to both schema linking and logical synthesis, maintaining the two-stage framework's interpretability.The framework applies TA to each module rather than treating each as an entirely new task to generalize from scratch.Source: §3.2 · p. 4

3.2.1 Task-Aligned Schema Linking Module (TASL)

  • TASL generates a dummy SQL query and extracts schema entities from it, using experience with SQL generation to support schema linking.The dummy query is not intended to be executable; it leverages successful schema-entity selection during generation.Source: §3.2.1 · p. 4

3.2.2 Task-aligned Logical Synthesis Module (TALOG)

  • TALOG models logical synthesis as step-by-step data-analysis operations, then uses symbolic functions resembling SQL keywords to represent SQL syntax and structures.Its symbolic representation includes reasoning processes and is designed to address logic involving SQL syntax, external knowledge, and computation.Source: §3.2.2 · p. 4

4 Experiments

The experiments evaluate TA-SQL across four benchmarks and multiple backends, then test its two-stage design, stage-specific task alignment, and illustrative hallucinations. Results report improved GPT-4 execution accuracy and show that hallucinations can be observed and mitigated before final SQL generation.

4.1 Experiment Settings

  • Experiments cover four cross-domain benchmarks, use Execution Accuracy, test closed- and open-source backends, and compare with DIN-SQL and DAIL-SQL on BIRD and SPIDER.BIRD evaluation uses its development set because its test set is unreleased.Source: §4.1 · p. 5

4.2 Main Results

  • 21.23% relative BIRD-development EX improvement for the GPT4 baseline, plus 14. 86%, 11. 80%, and 8. 31% on SPIDER, DK, and REALISTIC.The SPIDER, DK, and REALISTIC improvements span queries of varying difficulty.Source: Table 2 · p. 5, §4.2 · pp. 5–6, Table 3 · p. 6
  • TA-SQL works with closed-source and open-source models, though weaker models show relatively limited gains on challenging queries.The paper attributes weaker-model limits to constrained generalization and instruction-following.Source: §4.2 · p. 6

4.3 Imperative of Two-stage Paradigm

  • On the BIRD development set, removing schema linking caused an absolute 6.42% total performance decrease, while removing logical synthesis decreased performance by 1.55% on simple, 10.68% on moderate, and 3.74% on challenging queries.More accurate schema linking facilitates subsequent logical synthesis; logical synthesis determines the framework’s performance upper bound.Source: Table 5 · p. 6, §4.3 · pp. 6–7

4.4 Ablation Study

  • Zero-shot TASL is competitive with DIN-SQL few-shot schema linking without human examples, while TALOG outperforms six-shot NatLOG across difficulty levels.The paper attributes the NatLOG gap to custom symbolic rules that models must learn from scratch.Source: §4.4 · p. 7, Figure 2 · p. 7, Figure 3 · p. 7

4.5 Fine-grained Case Study

  • Case studies show TASL and TALOG addressing schema-based hallucinations and erroneous multi-step computational logic in illustrative BIRD-dev examples.The examples include relative EX increases of 59.05%, 43.1%, and 32.13% on three databases.Source: §4.5 · pp. 7–8, Table 6 · p. 8

4.6 Discussion about Hallucinations in Text-to-SQL Systems

  • The paper defines intrinsic hallucinations as SQL contradictions with the natural-language query, database schema, or SQL syntax, and distinguishes them from execution errors.Unlike errors, hallucinations can be observed and mitigated during schema linking or logical synthesis, before final SQL output and execution.Source: §4.6 · p. 8

5 Related Work

Prior text-to-SQL work spans representation learning, pretrained sequence-to-sequence models, and LLM-based solutions. Hallucination is also recognized as a source of erroneous or nonsensical SQL.

Text-to-SQL

  • Text-to-SQL systems have advanced through relational graph neural networks, pretrained sequence-to-sequence models, constrained decoding, schema alignment, and multi-hop reasoning.RESDSQL also decouples schema linking and skeleton parsing.Source: §5 · pp. 8–9
  • LLM-based methods such as DIN-SQL and DAIL-SQL use few-shot demonstrations, while language agents have also been applied to text-to-SQL.Source: §5 · p. 9

Hallucination

  • Hallucination—generating information absent from or not inferred from the input—can produce erroneous or nonsensical SQL queries.Prior work also identifies factual consistency problems in dialog generation.Source: §5 · p. 9

6 Conclusion

The paper classifies common text-to-SQL hallucinations and proposes Task Alignment (TA) and TA-SQL to mitigate them. Its analysis presents hallucination research as a promising direction for text-to-SQL and data science.

6 Conclusion

  • The paper systematically identifies and classifies common hallucination types in text-to-SQL.Source: §6 · p. 9
  • Task Alignment (TA) is proposed to mitigate LLM hallucinations, and TA-SQL applies the strategy at each stage of text-to-SQL.Source: §6 · p. 9
  • The experimental results and analysis suggest promising directions for future hallucination research in text-to-SQL and data science.Source: §6 · p. 9

7 Limitations

The authors qualify TA’s effectiveness by the clarity of questions and sufficiency of knowledge evidence, and identify human-led task selection as an open limitation.

7 Limitations

  • TA is particularly effective on complex cases with explicit BIRD knowledge evidence and unequivocally answerable questions.Source: §7 · p. 10
  • For codebase_community, student_club, and thrombosis_prediction, the authors find direct SQL generation from annotated data more effective when questions are clear and evidence is sufficient.Source: §7 · p. 10
  • Human prior knowledge currently selects familiar tasks for each phase; automated identification and retrieval of relevant tasks remains future work.Source: §7 · p. 10

9 Ethical Statement

The ethical statement describes publicly accessible datasets, SQL-formatted outputs, and review for sensitive or biased material. The appendix says TA-SQL uses Pythonic prompts to represent structures and express constraints concisely.

9 Ethical Statement

  • The team examines generated SQL outputs for politically sensitive or biased material, and reports that the datasets are publicly accessible.Source: §9 · p. 10

A.1 Pythonic Prompt

  • TA-SQL uses Pythonic prompts because they represent data structures clearly and express constraints and requirements concisely.Source: §A.1 · p. 14

A.2 TASL Module

TASL represents schema linking by generating a dummy SQL query and extracting its related schema entities. It prepares compact column descriptions for the schema dictionary to limit prompt verbosity.

A.2 TASL Module

  • TASL generates a dummy SQL query, then extracts related schema entities as the final schema-linking output.The module uses a zero-shot prompt for dummy SQL generation.Source: §A.2 · p. 14
  • A Python dictionary represents the database schema, with table_name.column_name entities as keys and column descriptions as values.The example key format is account.account_id.Source: §A.2 · p. 14
  • To avoid lengthy prompts that could confuse LLMs, TASL first generates succinct descriptions for each column from related information.These descriptions become the values for the columns in the schema dictionary.Source: §A.2 · p. 14

A.3 TALOG Module

TALOG uses pandas APIs to guide step-by-step logical reasoning, then substitutes symbolic functions resembling SQL keywords to bridge the gap between pandas and SQL.

A.3 TALOG Module

  • TALOG uses pandas APIs to guide LLMs through step-by-step logical reasoning.The module addresses the gap between conventional pandas APIs and SQL by replacing those functions with symbolic functions resembling SQL keywords.Source: §A.3 · pp. 14–15
  • TALOG provides six demonstrations to help the model learn to generate the desired symbolic representations.Figure 7 presents the prompts used to generate these representations.Source: §A.3 · p. 15

B Details of Evaluation Metrics for Schema Linking

The schema-linking evaluation defines recall, precision, and F1; the implementation details name the software used and how DeepSeek and CodeLlama were accessed.

B Details of Evaluation Metrics for Schema Linking

C Implement Details

Loading 2405.15307v1…