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

FlexSQL: Flexible Exploration and Execution Make Better Text-to-SQL Agents

Quang Hieu Pham, Yang He, Ping Nie, Canwen Xu, Davood Rafiei, Yuepeng Wang, Xi Ye, Jocelyn Qiaochu ChenCOLM 2026

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:

  1. 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.
  2. 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).
  3. 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.
Figure 2: Overview of the FlexSQL framework. The agent takes a natural language query and optional external documents as input, and produces a SQL query through three com- ponents: Plan Generation explores the database via context query and executor tools to produce K diverse plans, with a refinement loop; Program Generation translates each plan into SQL or Python, with a repair loop for code-level errors and backtracking to Plan Gen- eration for plan-level errors; Majority Voting selects the consensus answer and transpiles Python solutions to SQL when needed.

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.
Figure 1: Condensed trace of FlexSQL on the motivating example. The agent progressively discovers the schema, grounds its filters in actual data values, verifies intermediate results, and generates diverse plans covering different interpretations of the query—all through incremental tool interaction with the database.

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.
Figure 3: Pass@K test-time scal- ing of FlexSQL and ReFoRCE.

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

BibTeX
@inproceedings{pham2026flexsql,
  author = {Quang Hieu Pham and Yang He and Ping Nie and Canwen Xu and Davood Rafiei and Yuepeng Wang and Xi Ye and Jocelyn Qiaochu Chen},
  title = {{FlexSQL: Flexible Exploration and Execution Make Better Text-to-SQL Agents}},
  booktitle = {COLM},
  year = {2026},
  url = {https://openreview.net/forum?id=X8B5cXOSyH},
}
Figure