Abstract
Real-world Text-to-SQL systems frequently encounter "messy" user interactions, including ambiguous, unanswerable, and improper queries. Traditional benchmarks often overlook these complexities, leading to models that struggle with "SQL-forcing" and context loss in multi-turn dialogues. This paper introduces QDA-SQL, a novel data augmentation framework that leverages Large Language Models (LLMs) to generate diverse, multi-turn conversational data. Coupled with StateFlow, a state-machine-based inference process, this approach enables fine-tuned open-source models like CodeGemma-7B to significantly outperform GPT-4 Turbo on conversational database tasks.
Introduction
Text-to-SQL research typically focuses on "clean" datasets where every natural language query maps directly to a valid SQL statement. However, real-world human-machine interaction is rarely so straightforward. Users often pose ambiguous questions (e.g., "What is Glenn's ID?" when "Glenn" could refer to a donor or a school), unanswerable questions (requesting data not present in the database), or improper social chit-chat. Existing models often suffer from "SQL-forcing," attempting to generate queries for every input, which results in hallucinations or system failures.
Methods
The proposed framework consists of two core components: the QDA-SQL augmentation method and the StateFlow inference framework.
QDA-SQL Augmentation
To move beyond manual annotation, QDA-SQL uses a high-performing LLM (e.g., Gemini Pro) as a "teacher" to generate large-scale multi-turn dialogues. Generation is guided by four thematic relations: Constraint Refinement, Topic Exploration, Participant Shift, and Answer Exploration. A three-step "Verify and Refine" process ensures data quality:
- Alignment Check: Verifying the Q&A matches the intended type (e.g., ensuring an "unanswerable" question is truly unanswerable).
- Expression Optimization: Refining natural language for human-like fluency.
- SQL Execution Scoring: Ensuring generated SQL is executable and accurate against the database.
StateFlow Inference
StateFlow conceptualizes the Text-to-SQL process as a state machine. Instead of a single-shot prompt, the model transitions through distinct states to improve reliability:
- Intent Recognition: Determining if a question is answerable, ambiguous, or out-of-scope.
- Solve: Generating the SQL query.
- Verify: An internal loop where the model checks its own SQL for execution errors. If an error is detected, the model logs the error and returns to the "Solve" state to self-correct.
Datasets and Evaluation
The authors enhanced the SParC and CoSQL training sets, resulting in 10,874 dialogues (65,393 turns). They also curated MMSQL (Multi-type Multi-turn text-to-SQL), a new test set specifically designed to evaluate handling of ambiguous and unanswerable queries.
Experimental results show that specialized, smaller models trained on this high-quality synthetic data can outperform much larger models. A fine-tuned CodeGemma-7B achieved Question Match (QM) scores of 57.6% on SParC and 56.5% on CoSQL, significantly surpassing GPT-4 Turbo (31.4% and 33.6%, respectively) in zero-shot settings.
Key Insights
- Intent Recognition is Crucial: The ability to recognize when not to write SQL is a primary driver of robustness in interactive environments. Models trained with intent recognition were far more resilient.
- Complexity Breeds Competence: Augmented data led to SQL with greater Abstract Syntax Tree (AST) depth and longer dialogue chains. This increased complexity in training data made models more resilient to difficult queries.
- Self-Correction via StateFlow: The "Verify" state acts as a safety net, allowing the system to mimic human debugging by observing execution errors and attempting corrections.
Conclusion
QDA-SQL demonstrates that high-quality, diverse synthetic data and structured inference processes can empower smaller open-source models to exceed the performance of state-of-the-art proprietary LLMs in complex, multi-turn Text-to-SQL tasks. Future work will focus on automating "Goal SQL" generation and expanding the framework's external knowledge capabilities.
Summary updated