SPFT - SQL : Enhancing Large Language Model for Text-to- SQL Parsing by Self-Play Fine-Tuning (2025)

Abstract: Despite the significant advancements of self-play fine-tuning (SPIN), which can transform a weak large language model (LLM) into a strong one through competitive interactions between models of varying capabilities, it still faces challenges in the Text-to-SQL task. SPIN does not generate new information, and the large number of correct SQL queries produced by the opponent model during self-play reduces the main model’s ability to generate accurate SQL queries. To address this challenge, we propose a new self-play fine-tuning method tailored for the Text-to-SQL task, called SPFT-SQL. Prior to self-play, we introduce a verification-based iterative fine-tuning approach, which synthesizes high-quality fine-tuning data iteratively based on the database schema and validation feedback to enhance model performance, while building a model base with varying capabilities. During the self-play fine-tuning phase, we propose an error-driven loss method that incentivizes incorrect outputs from the opponent model, enabling the main model to distinguish between correct SQL and erroneous SQL generated by the opponent model, thereby improving its ability to generate correct SQL. Extensive experiments and in-depth analyses on six open-source LLMs and five widely used benchmarks demonstrate that our approach outperforms existing state-of-the-art (SOTA) methods.

## introduction text-to-sql @xcite aims to automatically convert natural language questions into sql queries, enabling non-expert users to easily retrieve information from databases. recent studies @xcite @xcite have demonstrated that supervised fine-tuning (sft) @xcite † corresponding authors figure 1 : comparison results on the spider @xcite and bird @xcite dataset, the base model of sft, spin @xcite , and spft-sql is qwen2.5-coder 7b. to address this issue, recent efforts @xcite @xcite have proposed data synthesis strategies for generating text-to-sql data and fine-tuning open-source models, yielding significant performance improvements (see figure 1 ). however, these methods still rely on closed-source llms, such as gpt-3.5/4 @xcite , for data synthesis, raising privacy concerns. in response, route @xcite introduced a method for synthesizing fine-tuning data for tasks like text-to-sql and schema linking using open-source models, improving model generalization through multi-task supervised fine-tuning and achieving a new state-of-the-art (sota) performance. however, the limited generation capacity of open-source models restricts the quality of synthetic data, which in turn limits model performance. an alternative approach involves iteratively synthesizing data through self-play fine-tuning (spin) @xcite @xcite to transform a weak llm into a stronger one. self-play, which has been successfully applied in domains such as reasoning @xcite , alphago @xcite , and alphazero @xcite , enables models to compete with themselves at various stages, enhancing both performance and data synthesis capabilities while overcoming the limitations of open-source model generation. in the context of the text-to-sql task, the only prior work @xcite applied selfplay to multi-turn text-to-sql, generating multiple rounds of intermediate questions and answers for data augmentation. while this method improved performance in multi-turn tasks, it is not applicable to single-turn text-to-sql, as it only generates intermediate data based on existing annotated pairs. this motivates us to conduct a thorough evaluation of spin in the text-to-sql task, assessing its potential as an alternative approach. as shown in figure 1 , applying the existing spin method @xcite ) to text-to-sql results in a significant performance drop, which is much lower than that of sft-based methods utilizing existing data synthesis techniques @xcite @xcite . a subsequent analysis of failure cases reveals two key challenges for spin in the text-to-sql domain. first, spin only synthesizes sql queries from existing natural language questions, without generating new information. this limitation restricts the model's ability to improve, and repeated training leads to overfitting. second, the self-play mechanism in spin treats all data generated by the opponent model as incorrect, which results in many valid sql queries being discarded as erroneous, thus hindering the model's ability to learn from errors. to address these challenges, we propose a selfplay fine-tuning method for text-to-sql tasks, called spft-sql. specifically, prior to self-play, we introduce a verification-based iterative supervised fine-tuning approach that iteratively synthesizes high-quality data for fine-tuning the llm. this method randomly selects schemas (e.g., tables and columns) from the database and combines them with sql templates to generate executable sql queries. corresponding natural language questions (nlqs) are then synthesized using a sql-to-text model. the synthesized nlq-sql pairs are used to fine-tune the text-to-sql model, enhancing its performance. the sql-to-text model is subsequently updated with the synthesized data that passes a verification strategy. during self-play finetuning, the strongest model from the previous stage serves as the main model, while the weakest model acts as the opponent. we introduce an error-driven loss function that penalizes correct sql queries generated by the opponent model and incentivizes the generation of incorrect queries. this mechanism enables the main model to better distinguish between correct and incorrect results, thus improving its ability to generate correct sql queries. in the next iteration, the newly acquired main model is incorporated into the next round of supervised fine-tuning. the main contributions of this work are as follows: ## methodology the spft-sql framework consists of two stages: verification-based iterative fine-tuning and self-play fine-tuning, as illustrated in figure 2 . in the first stage, verification-based iterative fine-tuning continuously generates high-quality data for finetuning, producing various candidate text-to-sql models for the subsequent self-play phase. during self-play fine-tuning, the strongest model from the previous stage serves as the main model, while the weakest model functions as the opponent. using the proposed error-driven loss, self-play fine-tuning is applied between the opponent model and the main model to enhance the main model's ability to generate correct sql queries from a nlq. this process is iterated until the model converges. ## conclusion in this paper, we propose a novel self-play finetuning framework for text-to-sql tasks, called spft-sql. spft-sql enhances the capabilities of open-source models through verification-based iterative fine-tuning to generate high-quality data augmentation, while further improving the models' ability to generate accurate sql via error-driven adversarial training in self-play scenarios. our work represents the first effective implementation of the self-play method in text-to-sql tasks, significantly narrowing the performance gap between open-source and closed-source models. future research will focus on exploring cross-domain generalization capabilities and developing efficient adversarial architectures.