NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

MAC-SQL: A Multi-Agent Collaborative Framework for Text-to-SQL

Bing Wang, Changyu Ren, Jian Yang, Xinnian Liang, Jiaqi Bai, Linzheng Chai, Zhao Yan, Qian-Wen Zhang, Di Yin, Xing Sun, Zhoujun Li

Abstract

Recent LLM-based Text-to-SQL methods usually suffer from significant performance degradation on “huge” databases and complex user questions that require multi-step reasoning. Moreover, most existing methods neglect the crucial significance of LLMs utilizing external tools and model collaboration. To address these challenges, we introduce MAC-SQL, a novel LLM-based multi-agent collaborative framework. Our framework comprises a core decomposer agent for Text-to-SQL generation with few-shot chain-of-thought reasoning, accompanied by two auxiliary agents that utilize external tools or models to acquire smaller sub-databases and refine erroneous SQL queries. The decomposer agent collaborates with auxiliary agents, which are activated as needed and can be expanded to accommodate new features or tools for effective Text-to-SQL parsing. In our framework, We initially leverage GPT-4 as the strong backbone LLM for all agent tasks to determine the upper bound of our framework. We then fine-tune an open-sourced instruction-followed model, SQL-Llama, by leveraging Code Llama 7B, to accomplish all tasks as GPT-4 does. Experiments show that SQL-Llama achieves a comparable execution accuracy of 43.94, compared to the baseline accuracy of 46.35 for vanilla GPT-4. At the time of writing, MAC-SQL+GPT-4 achieves an execution accuracy of 59.59 when evaluated on the BIRD benchmark, establishing a new state-of-the-art (SOTA) on its holdout test set.

Introduction

Converting natural language into SQL queries (Text-to-SQL) is a critical step in making databases accessible to non-technical users. While Large Language Models (LLMs) like GPT-4 have demonstrated impressive capabilities, they face three primary hurdles in real-world applications:

  1. Schema Noise in Large Databases: Real-world databases often contain hundreds of tables. Feeding the entire schema into an LLM context window introduces noise, leading to hallucinations or context limit exhaustion.
  2. Complex Reasoning Requirements: Questions requiring multi-step logic (e.g., calculating rates and comparing them to averages) often fail when generated in a single-shot prompt.
  3. Lack of Self-Correction: Standard models typically generate a query and stop. If the output contains a syntax error or a schema mismatch, the system has no mechanism to "debug" the result.

The MAC-SQL Framework

To overcome these limitations, we propose MAC-SQL, a framework that treats Text-to-SQL as a collaborative effort between three specialized agents: the Selector, the Decomposer, and the Refiner.

The MAC-SQL framework architecture, illustrating the collaborative workflow between the Selector, Decomposer, and Refiner agents.

Agent Roles

  • The Selector: This agent acts as a schema filter. It identifies the minimal subset of tables and columns necessary to answer a specific question. By pruning irrelevant data, it reduces the "distraction" for the generation phase.
  • The Decomposer: Utilizing a Chain-of-Thought (CoT) approach, this agent breaks complex user questions into simpler sub-questions. It generates intermediate SQL snippets for these sub-tasks, solving them progressively to reach the final query.
  • The Refiner: Acting as an automated debugger, the Refiner executes generated SQL in a real SQLite environment. If an error occurs (such as a syntax error or a missing column), it captures the error message and uses it as feedback to iteratively fix the query.

SQL-Llama: Democratizing Agentic SQL Generation

While GPT-4 serves as a powerful backbone, its high cost and proprietary nature can be limiting. To address this, we developed SQL-Llama. We fine-tuned Code Llama 7B on a custom "Agent-Instruct" dataset consisting of 10,000 high-quality interaction traces where GPT-4 performed the tasks of the Selector, Decomposer, and Refiner. This allows a smaller, open-source model to mimic the multi-agent collaborative behavior of its larger counterpart.

Experimental Results

We evaluated MAC-SQL on two major benchmarks: Spider and BIRD (Big Bench for Large-scale Database Grounded Text-to-SQLs). BIRD is particularly challenging due to its focus on massive databases (up to 33.4GB) and complex reasoning tasks.

Performance comparison of MAC-SQL against various baselines on the BIRD benchmark.

As shown in the results, MAC-SQL + GPT-4 achieved a new state-of-the-art execution accuracy of 59.59% on the BIRD holdout test set. Notably, our specialized SQL-Llama 7B achieved an execution accuracy of 43.94% on the BIRD dev set, which is remarkably competitive with the 46.35% achieved by a vanilla, single-shot GPT-4.

Key Insights

  • The Importance of Schema Linking: Our analysis shows that if the Selector were perfect (the "Oracle Schema" scenario), accuracy would jump to over 70%. This confirms that identifying the correct tables remains a primary bottleneck.
  • Refinement Utility: Ablation studies indicate that removing the Refiner agent leads to a ~5% drop in accuracy, proving that the iterative "generate-then-fix" loop is vital for handling complex queries.

Conclusion and Future Work

MAC-SQL demonstrates that multi-agent collaboration can significantly enhance Text-to-SQL performance, especially on large-scale databases. While highly effective at fixing syntax errors, the current Refiner agent still struggles with "logical" errors—queries that run successfully but return incorrect data. Future work will focus on improving logical verification and scaling the framework to larger open-source models like 34B variants to further close the gap with proprietary systems.

Summary updated

BibTeX
@inproceedings{wang2025mac,
  author = {Bing Wang and Changyu Ren and Jian Yang and Xinnian Liang and Jiaqi Bai and Linzheng Chai and Zhao Yan and Qian-Wen Zhang and Di Yin and Xing Sun and Zhoujun Li},
  title = {{MAC-SQL: A Multi-Agent Collaborative Framework for Text-to-SQL}},
  booktitle = {International Conference on Computational Linguistics},
  year = {2025},
  url = {https://openreview.net/forum?id=sgeRKCndyH},
}
Figure