Reflect-SQL: A Self-Reflection Based Framework for Text-to-SQL
Organizations: International Institute of Information Technology Hyderabad, India
Abstract
Democratizing data access through natural language is a crucial goal for modern enterprises, but the practical adoption of Text-to-SQL is critically hindered by real-world complexities: 1. Obscure and large database schemas, 2. Ineffective retrieval of relevant tables and columns due to structured setting of schemas and vague user query, 3. Generation of syntactically or logically flawed SQL due to a lack of robust validation and correction mechanism. To address these systemic challenges, we introduce Reflect-SQL, a novel framework for Text to SQL, grounded in multi-stage self-reflection approach to develop understanding of obscure schema using a knowledge base, setup a process for effective retrieval and system to generate syntactically/semantically SQL. Instead of a single-pass attempt, our system employs an LLM-as-a-judge driven scoring mechanism within interconnected feedback loops to iteratively refine the results at every stage. A feedback-driven retrieval loop refines the user's natural language query, while a synthesis loop validates and corrects the SQL and finally, an entailment loop optimizes the end-to-end process and continuously enriches the knowledge base. By integrating these layers of reflection, Reflect-SQL bridges the critical gap between user intent and complex data. On the challenging BIRD benchmark, our framework achieves an execution accuracy of 72.03%, significantly outperforming state-of-the-art baselines, demonstrating a major leap in reliability for enterprise applications.
Figures & tables
| Component | Before Refinement | After Refinement |
|---|---|---|
| loan.status | Categorical column for loan status with values ’A’, ’B’, ’C’, ’D’. | Loan repayment state: ’A’ indicates finished or paid, ’B’ indicates defaulted… |
| trans.k_symbol | Text column named ’k_symbol’ with values ’POJISTNE’, ’SIPO’. | Transaction Category: determines the payment type (e.g. ’POJISTNE’ maps to Insurance)… |
| Parameter | Value |
|---|---|
| 4 | |
| 5 | |
| 3 | |
| 0.6 | |
| 0.2 | |
| 0.1 |
| Model | Simple | Moderate | Challenging | Overall |
|---|---|---|---|---|
| Claude Models | ||||
| Claude-sonnet-4.5 (with reflection) | 88.97 | 50.65 | 32.41 | 72.03 |
| Claude-sonnet-4.5 (w/o reflection) | 81.18 | 45.25 | 24.13 | 64.92 |
| OpenAI API Models | ||||
| o1-preview (with reflection) | 75.89 | 48.49 | 18.62 | 62.19 |
| o1-preview (w/o reflection) | 69.35 | 40.54 | 15.86 | 55.66 |
| Stage | Analysis / SQL Snippet |
|---|---|
| User Query | List the lowest three eligible free rates for students in continuation schools. |
| Initial SQL | SELECT ... (f.Count / f.Enrollment) ... WHERE s.SOCType LIKE ’%Continuation%’ |
| Entailment Check Result | Score : 2 / 5 Reason: The result returns None values due to division by zero or nulls. Filters for positive enrollment and float casting are needed. |
| Refined SQL | SELECT ... (CAST(f.Count AS REAL) / f.Enrollment) ... WHERE f.Enrollment > 0 AND s.SOCType LIKE ’%Continuation%’ |
| Method | Overall Accuracy |
|---|---|
| BIRD paper [ 15 ] | 46.35 |
| DIN-SQL [ 13 ] | 50.72 |
| DAIL-SQL [ 19 ] | 57.41 |
| MAC-SQL [ 20 ] | 59.59 |
| CHASE-SQL + Claude 3.5 Sonnet [ 21 ] | 69.53 |
| CHASE-SQL + Gemini 1.5 Pro [ 21 ] | 73.01 |
| Variant | Simple | Moderate | Challenging | Overall |
|---|---|---|---|---|
| Full Model (Self-Reflect) | 68.64 | 43.10 | 14.48 | 55.80 |
| w/o Self-Reflection in SQL Generation | 62.91 | 37.93 | 12.41 | 50.58 |
| w/o Iterative Retrieval | 67.02 | 40.30 | 10.34 | 53.58 |
| w/o Entailment Check | 64.10 | 41.59 | 13.10 | 52.47 |
Text-to-SQL
Text-to-SQL translates natural-language questions into executable database queries, linking user intent to schemas, values, and SQL logic.
See all 173 papers