Abstract
Text-to-SQL (Text2SQL) conversion is a critical reasoning task for Large Language Models (LLMs). Traditional Supervised Fine-Tuning (SFT) requires massive datasets and often leads to pattern memorization rather than robust reasoning. While Reinforcement Learning (RL) can enhance reasoning, it is notoriously sensitive to reward design, often leading to "reward hacking" where models exploit reward functions without truly solving the task. We present ConstrainedSQL, a constrained RL framework that treats formatting and executability as hard constraints rather than manually weighted bonuses. Using a Primal-Dual approach combined with Group Relative Policy Optimization (GRPO), ConstrainedSQL achieves state-of-the-art performance on benchmarks like Spider and BIRD using 50x less training data than traditional SFT methods.
Introduction
Converting natural language queries into executable SQL code requires a deep understanding of database schemas and complex logical reasoning. Current approaches primarily rely on SFT, which is data-intensive and prone to overfitting. While RL offers a path toward better reasoning capabilities, designing a balanced reward function for multiple objectives—such as correctness, format, and executability—is difficult. Manual weighting of these signals is often non-portable and fails to generalize, as models may find "loopholes" (e.g., producing SQL that runs but is poorly formatted) to maximize rewards. This phenomenon, known as reward hacking, necessitates a more robust optimization framework.
Methodology
ConstrainedSQL addresses the reward hacking problem by formulating Text2SQL training as a constrained optimization problem. The objective is to maximize the probability of generating correct SQL (matching ground truth) while ensuring that auxiliary metrics meet predefined success thresholds (e.g., a 95% success rate for formatting and execution).
The Primal-Dual Algorithm
The framework employs a Primal-Dual approach integrated with GRPO. Instead of a single weighted reward function, the problem is solved by maximizing a Lagrangian that incorporates both the primary reward and weighted constraint violations. The training process involves two alternating steps:
- Primal Step: The LLM policy is updated to maximize the Lagrangian.
- Dual Step: The Lagrange multipliers (the "weights" for each constraint) are automatically adjusted based on the model's current performance. If the model fails to meet a constraint threshold (e.g., the "format" success rate drops), the corresponding multiplier increases, forcing the model to prioritize that requirement in the next iteration.
This dynamic adjustment eliminates the need for human intervention in tuning reward weights and ensures stable convergence across all objectives.
Experimental Setup
The model was trained on a subset of 5,000 complex samples from the SynSQL-2.5M synthetic dataset. Evaluation was conducted on two industry-standard benchmarks:
- Spider: A large-scale cross-domain Text2SQL dataset.
- BIRD: A challenging benchmark featuring large-scale databases and requirements for external knowledge.
Key Results and Discussion
ConstrainedSQL demonstrates significant improvements in both performance and data efficiency compared to traditional SFT and unconstrained RL methods.
Performance Benchmarks
On the Spider (Dev) set, the 3B parameter model achieved 82.0% accuracy, significantly outperforming the previous RL state-of-the-art, SQL-R1 (79.3%). The 7B model reached 85.2% on Spider (Dev) and 86.7% on the Test set. On the tougher BIRD benchmark, the 3B model achieved 55.0%, surpassing reported results of its RL competitors.
Training Stability and Convergence
A key advantage of the constrained framework is its stability. As shown in the training curves, the primary reward increases steadily while all constraints successfully converge to their targets. This proves that the model is learning to solve the task correctly without sacrificing formatting or executability.
Data Efficiency
One of the most striking findings is the framework's data efficiency. ConstrainedSQL achieved performance comparable to SFT-based models like OmniSQL while using approximately 50x fewer training samples. This suggests that RL is significantly more effective at teaching logical reasoning than SFT is at teaching pattern recognition from massive datasets.
Conclusion
ConstrainedSQL provides a robust and efficient framework for training LLMs on complex reasoning tasks. By replacing manual reward weighting with a mathematically grounded Primal-Dual approach, the method prevents reward hacking and achieves high performance with minimal data. Future work will explore applying this framework to other reasoning-heavy domains beyond SQL and addressing the "parameterization gap" to further enhance model capabilities.
Summary updated