Integrating Reasoning and Generalization in Text-to-SQL via Self-Enhanced Fine-Tuning

Feng Lyu, Jinfeng Cen, Sijing Duan, Hao Wu, Shucheng Li, Weixu ZhangarXiv 2026 · arXiv

TL;DR

CoTE-SQL bridges the gap between reasoning and generalization in Text-to-SQL systems through an iterative, self-enhanced fine-tuning framework. By combining self-generated reasoning traces, structured modular prompting, and real-time execution feedback for self-debugging, it achieves state-of-the-art performance on complex benchmarks using open-source LLMs.

What Problem Does This Paper Address?

Translating natural language questions into executable SQL queries (Text-to-SQL) is key to democratizing data access for non-experts. While Large Language Models (LLMs) have advanced this field, existing approaches generally fall into two flawed paradigms:

  1. In-Context Learning (Prompting): These methods rely on static, handcrafted templates. They often struggle to generalize when faced with complex, multi-step queries or unfamiliar database schemas.
  2. Fine-Tuning: While fine-tuning helps align models to specific domains, high-quality Chain-of-Thought (CoT) reasoning annotations are scarce. Without explicit supervision over intermediate reasoning steps, fine-tuned models are highly prone to logical errors on complex tasks.

The authors introduce CoTE-SQL to resolve this trade-off, creating a system that systematically learns to reason through database schemas and query structures without requiring expensive human-annotated reasoning paths.

Figure 1: Limitations of existing text-to-SQL approaches. (i) In-context learning methods can be categorized into two types. The first, referred to as shallow prompting, demonstrates SQL generation via examples but fails to fully exploit the reasoning capabilities of LLMs. The second, CoT-based prompting, builds on this with CoT reasoning, but often suffers from poor generalization and struggles with complex SQL tasks due to limited reasoning diversity. (ii) Fine-tuning-based methods are prone to logical errors, constrained mainly by the quantity and quality of annotated data. Moreover, these approaches often lack explicit supervision over intermediate reasoning steps, resulting in suboptimal performance on complex queries.

Methods

CoTE-SQL introduces a unified framework featuring three core innovations:

  • Iterative Self-Enhanced Fine-Tuning: Instead of relying on human annotations, the framework prompts a pre-trained LLM to generate its own intermediate reasoning traces aligned with correct ("gold") SQL answers. Correct traces are filtered via execution equivalence and added to the training set. If a trace is incorrect, a retry mechanism injects corrective hints to help the model find the right path, preventing error amplification.
  • Structured CoT Prompting with Modular Decomposition: The text-to-SQL task is broken down into flexible, interpretable subtasks:
  • Schema Selection via Entity Alignment: Explicitly maps natural language entities to specific database tables and columns to filter out schema noise.
  • Progressive SQL Generation: Guides the model through human-like query planning (e.g., identifying intent, selecting operations, determining joins/subqueries). This is further augmented with BM25 retrieval-based exemplars to boost in-context learning.

  • Error-Aware Revision: During inference, if a generated SQL query fails to execute, the system feeds the database engine's error message back into the LLM alongside a specialized debugging prompt. This allows the model to self-reflect, localize the fault, and issue a logical or syntactic fix.

Figure 3: The proposed iterative self-enhanced fine-tuning framework. Note that in practical use, CoTE-SQL evaluates the correctness of the selected schema and the generated SQL separately. If one is incorrect, only the corresponding module needs to be retried. We plot them together for simplicity.

Datasets

The framework was evaluated on two major text-to-SQL benchmarks:

  • Spider: A large-scale, cross-domain benchmark containing 10,181 questions paired with 5,693 complex SQL queries across 200 databases. It is primarily used to evaluate schema generalization capabilities.
  • BIRD: A highly challenging, real-world dataset comprising 12,751 question-SQL pairs across 95 large databases. It emphasizes handling massive database contents and optimizing execution efficiency.

Key Insights and Ideas

  • Execution Engines as Hard Filters: By using database execution feedback to validate self-generated CoT traces, the authors turn the database engine into an automated, high-fidelity supervisor, bypassing the need for human annotation.
  • Scaffolded Free-Form Reasoning: Rather than forcing the LLM into rigid, template-based outputs that degrade performance on smaller models, CoTE-SQL provides structural guidance (Schema Selection $\rightarrow$ SQL Generation) while preserving the model's natural generative freedom.
  • Errors are Features, Not Bugs: Treating runtime execution errors as weak supervision signals allows the model to internalize patterns of failure and repair. This self-debugging loop significantly increases robustness at inference time.
  • Asymmetric Task Difficulty: The authors observed that SQL generation is inherently harder than schema selection. Allocating a larger proportion of training data to the generation module prevents the model from bottlenecking on complex query logic.

Key Results

CoTE-SQL was evaluated against more than 15 baselines using Llama-3.1-8B and Qwen2.5-7B as backbone models:

  • State-of-the-Art on BIRD: Built on Qwen2.5-7B, CoTE-SQL achieved 53.39% Execution Accuracy (EX) and a 59.02 Valid Efficiency Score (VES), outperforming all open-source baselines of comparable size.
  • Strong Performance on Spider: It achieved 79.60% EX and 77.19% VES using Llama-3.1-8B. Notably, it showed a massive 7.2% EX improvement on Spider's highly challenging "Extra Hard" query category.
  • High Computational Efficiency: Multi-stage prompting baselines like DEA-SQL and DAC often consume over 11,000 tokens per query due to fragmented reasoning steps. CoTE-SQL unified reasoning and generation into a streamlined pipeline, averaging just 2,242 tokens and 10.9 seconds per query while delivering superior accuracy.

Limitations and Future Work

  • Cross-Dataset Annotation Dependency: Because the BIRD dataset lacks explicit entity-linking annotations, the authors had to train the Schema Selection module exclusively on Spider data. While the module generalized well, performance could be further improved with native domain annotations.
  • Broader Applications: The authors note that the core paradigm of CoTE-SQL—combining self-enhanced training, modular task decomposition, and execution-time feedback loops—is highly transferable. Future work includes applying this framework to other multi-step decision-making tasks, such as general tool use or robotic planning.

Summary updated

Abstract

Text-to-SQL aims to translate natural language questions into executable SQL queries over structured databases, enabling non-expert users to access data intuitively. While recent advances in large language models (LLMs) have shown promise in this task, existing LLM-based approaches often struggle to strike a balance between strong reasoning capabilities and robust generalization. To address these limitations, we propose CoTE-SQL to enhance the LLM-based text-to-SQL generation with three key innovations: (i) self-enhanced reasoning traces distilled from LLMs without human annotation, (ii) structured chain-of-thought (CoT) prompting with modular decomposition and examples retrieval, and (iii) error-aware revision based on SQL execution feedback. Extensive experiments on the Spider and Bird benchmarks demonstrate that CoTE-SQL achieves new state-of-the-art performance among methods built on open-source LLMs with comparable model sizes on Bird (53.39% EX / 59.02 VES) and strong results on Spider (79.60% EX / 77.19 VES), with especially significant gains on complex queries. Results highlight the effectiveness of combining self-enhancement, structured reasoning, and execution-time feedback within an LLM-based framework for text-to-SQL design.
BibTeX
@misc{lyu2026integrating,
  author = {Feng Lyu and Jinfeng Cen and Sijing Duan and Hao Wu and Shucheng Li and Weixu Zhang and Haolun Wu},
  title = {{Integrating Reasoning and Generalization in Text-to-SQL via Self-Enhanced Fine-Tuning}},
  year = {2026},
  eprint = {2606.15598},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2606.15598},
}

Loading related content...

Figure