Introduction
MAG-SQL is a novel multi-agent framework designed to bridge the "last mile" of complexity in Text-to-SQL tasks. By decomposing natural language questions into manageable sub-queries and employing a "soft" schema-linking strategy alongside an iterative refinement loop, MAG-SQL achieves state-of-the-art performance on challenging benchmarks like BIRD.
The Challenge of Real-World Text-to-SQL
While Large Language Models (LLMs) excel at simple SQL generation, real-world databases present significant hurdles that lead to "cascading errors" where a single mistake invalidates the entire query:
- Schema Complexity: Datasets like BIRD feature massive schemas with hundreds of columns, often with ambiguous names or "dirty" data.
- Reasoning Gaps: Complex queries require multi-step logic involving joins, filters, and aggregations. One-shot generation often fails to bridge these gaps effectively.
The MAG-SQL Framework
MAG-SQL mimics a human data engineer's workflow through four specialized agents that filter the schema, decompose requirements, and iteratively build and test code.
1. Soft Schema Linker
Unlike "hard" selection methods that might prematurely discard relevant columns, the Soft Schema Linker generates table summaries and uses a Longest Common Substring (LCS) algorithm to retrieve database values. It highlights relevant columns while maintaining the full schema context to prevent information loss.
2. Targets-Conditions Decomposer
This agent breaks complex questions into a sequence of sub-questions. It identifies the targets (SELECT clause) and conditions (WHERE/HAVING clauses), adding exactly one new condition per sub-question to ensure a uniform level of detail for the generator.
3. Sub-SQL Generator
By focusing on adding only one condition at a time, this agent iteratively builds the SQL query, significantly reducing the reasoning burden compared to one-shot generation.
4. Sub-SQL Refiner
Acting as a "critic," the Refiner executes generated Sub-SQL against the database. If an error occurs or results are empty, it provides feedback (e.g., SQLite error messages) to the Generator for correction. This self-correction loop is vital for mitigating hallucinations.
Evaluation and Results
The framework was evaluated on the BIRD and Spider benchmarks:
- BIRD Benchmark: MAG-SQL + GPT-4 achieved 61.08% Execution Accuracy (EX), significantly outperforming vanilla GPT-4 (46.35%) and previous multi-agent systems like MAC-SQL (57.56%).
- Spider Benchmark: It maintained robustness with an accuracy of 85.6%.
- Model Efficiency: Notably, MAG-SQL using GPT-3.5 (57.62%) outperformed some GPT-4 based methods, proving that a structured process can compensate for a less powerful base model.
Key Insights
- Granularity is Key: Decomposing by "Targets" and "Conditions" provides consistent supervision for intermediate steps.
- Iterative Feedback: The Sub-SQL Refiner's ability to use execution feedback is the framework's most critical component; ablation studies showed its removal caused a ~10% performance drop.
- Context Preservation: "Soft" schema selection balances noise reduction with the need to keep potentially relevant columns available for the LLM.
Conclusion
MAG-SQL demonstrates that a structured, multi-agent approach can effectively handle the complexities of large-scale database reasoning. Future work may involve moving away from closed-source dependencies toward fine-tuned open-source models and implementing dynamic planning for more flexible agent orchestration.
Summary updated