TL;DR
FlexSQL is a new text-to-SQL agent that replaces rigid, one-way pipelines with a flexible, tool-based interaction loop. By allowing the agent to explore schemas, inspect data values, and implement solutions in either SQL or Python, it achieves state-of-the-art performance on complex analytical benchmarks—even outperforming much larger models like GPT-o3 and DeepSeek-R1.
What Problem Does This Paper Address?
Translating natural language into SQL is relatively easy for simple queries, but it becomes a nightmare in real-world analytical environments. Modern data warehouses (like Snowflake or BigQuery) often contain hundreds of tables and tens of thousands of columns.
Existing systems typically follow a "fixed pipeline": they retrieve schema information once, generate a query, and then try to fix syntax errors. This rigidity is a fatal flaw. If the model picks the wrong table or misunderstands a column's meaning at the start, it gets trapped in a "repair loop" that can never succeed because the underlying plan is broken. Furthermore, natural language is often ambiguous; a single question might have multiple valid interpretations that a rigid system cannot explore simultaneously.
Methods
FlexSQL introduces a framework centered on flexibility across three dimensions:
- Flexible Exploration: Instead of a static schema snapshot, the agent uses a suite of tools (
GetSchema,GetTableCol,GetColValues,FindRows) to incrementally discover the database structure. It can look at actual data values to ground its decisions—for example, discovering that the user's request for "materials science" data actually corresponds to specific codes like 'MS-01' in a hidden column. - Diverse Planning: Rather than committing to one path, FlexSQL uses "diversity-enforced sampling" to generate $K$ different execution plans. This allows the agent to cover multiple interpretations of ambiguous queries (e.g., deciding whether "citations" should include foreign patents or just domestic ones).
- Bilingual Execution & Backtracking: The agent can choose to write its solution in either SQL or Python. Python is often better for complex, multi-step procedural logic that would require messy, nested SQL. If an error is detected during execution, FlexSQL doesn't just try to fix the code; it can backtrack all the way to the planning stage to revise its fundamental assumptions about the schema.
Datasets
The authors evaluated FlexSQL on two subsets of the Spider2.0 benchmark:
- Spider2-Snow: 544 questions targeting enterprise-scale Snowflake databases with massive schemas.
- Spider2-SQLite: 135 questions over local SQLite databases to test performance on more moderately sized schemas and different SQL dialects.
Key Insights and Ideas
- Flexibility Trumps Raw Model Power: One of the most striking findings is that a smaller, open-source model (gpt-oss-120b) using the FlexSQL agentic loop can outperform "frontier" models like GPT-o3 and DeepSeek-R1 that are used in more rigid frameworks.
- The "Grounding" Necessity: Metadata (table and column names) isn't enough. As shown in the motivating example, looking at actual column values is often the only way to correctly map natural language concepts to database filters.
- Python as a Procedural Bridge: LLMs are naturally better at generating procedural code (step-by-step) than complex declarative SQL. By allowing the agent to think in Python and then transpiling that logic back to SQL, FlexSQL plays to the LLM's strengths.
Key Results
- State-of-the-Art Performance: On Spider2-Snow, FlexSQL achieved a 65.4% score (Majority@16), significantly outperforming strong baselines like DSR-SQL (63.8% with DeepSeek-R1) and ReFoRCE (62.9% with gpt-o3).
- Efficiency: The 20B parameter version of FlexSQL outperformed 120B parameter versions of baseline systems, demonstrating high parameter efficiency.
- Test-Time Scaling: The system shows strong scaling properties; as you increase the number of sampled plans ($K$), the accuracy continues to climb steadily.
- Real-World Utility: When integrated as a set of "skills" into a general-purpose coding agent (Claude Code), FlexSQL provided a 10% relative improvement in performance.
Limitations and Future Work
- Latency: The flexible interaction comes at a cost. FlexSQL takes longer to produce an answer (averaging ~288 seconds vs. ~216 seconds for a standard agent) due to the multiple database round-trips and tool calls.
- Transpilation Complexity: While the Python-to-SQL transpiler is highly accurate (over 96%), some extremely complex Python logic—such as specific library calls or intricate regex—remains difficult to translate back into pure SQL.
- Tool Preference: The models showed a heavy preference for code execution tools over simple schema lookups, suggesting that future work could optimize how agents decide which tool is most efficient for a given reasoning step.
Summary updated