FlexSQL: Flexible Exploration and Execution Make Better Text-to-SQL Agents
FlexSQL introduces a text-to-SQL agent that outperforms stronger models on the Spider2-Snow benchmark by employing a flexible design principle that allows for dynamic schema exploration, data inspection, and diverse execution plans with a two-tiered repair mechanism throughout the reasoning process.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Imagine you are a detective trying to solve a mystery, but instead of a crime scene, your "crime scene" is a massive, chaotic library containing millions of books, files, and data points. Your boss gives you a vague clue: "Find all the patents about materials science filed in early 2014 and count how many older patents they mention."
Most current computer programs trying to solve this act like a rigid robot. They are given a map of the library once at the start. They pick a few shelves based on that map, write a list of instructions (a query), and then try to execute it. If they get stuck or the answer is wrong, they can only fix small typos in their instructions. They cannot go back and say, "Wait, I picked the wrong shelves entirely," or "I didn't realize 'materials science' was hidden under a code like 'MS-01'." They are stuck with their initial mistake.
FlexSQL is a new kind of detective agent that acts more like a curious human explorer. Instead of being rigid, it is flexible. Here is how it works, using simple analogies:
1. The "Flexible Explorer" (Flexible Interaction)
Instead of looking at the library map once and committing to a path, FlexSQL is allowed to wander around the library while it thinks.
- The Old Way: The robot looks at the map, guesses "Materials Science" is in the "Science" section, and starts writing its list.
- FlexSQL's Way: The agent says, "I'm not sure where 'Materials Science' is. Let me walk over to the 'Technology' section, peek inside a box, and read the labels."
- It uses special tools to browse the shelves (schema), read the actual contents of the boxes (data values), and run small test searches to see if its guesses make sense. If it realizes it's in the wrong aisle, it can immediately turn around and go to the right one. It doesn't get stuck in a dead end.
2. The "Brainstorming Team" (Diverse Planning)
The clue "earlier patents each one cites" is tricky. Does it mean only domestic patents? Foreign ones too? Pending applications?
- The Old Way: The robot picks one interpretation (e.g., "Only domestic") and sticks to it. If it's wrong, the whole answer is wrong.
- FlexSQL's Way: It acts like a team of detectives brainstorming. It generates multiple different theories (plans) at once.
- Plan A: Count only domestic citations.
- Plan B: Count domestic and foreign.
- Plan C: Count everything, including pending apps.
It runs all these theories and then lets the "majority vote" decide which answer is the most likely to be correct. This way, even if one theory is wrong, the team might still get the right answer.
3. The "Bilingual Translator" (Flexible Execution)
Sometimes, a task is too complicated to write as a single, straight-line instruction (SQL). It might require a loop, a "if-then" decision, or a step-by-step calculation that is messy to write in a database language.
- The Old Way: The robot tries to force everything into one rigid SQL sentence, often making it too complex and prone to breaking.
- FlexSQL's Way: It is bilingual. It can write the instructions in SQL (the language databases speak) OR in Python (a flexible programming language).
- If the task is a simple list, it uses SQL.
- If the task requires a complex loop or a step-by-step calculation, it writes a Python script first because it's easier to think that way.
- Once the Python script works perfectly, it translates the final result back into SQL so the database understands it. It's like writing a complex recipe in a notebook first, then translating it into the official kitchen order.
4. The "Do-Over" Button (Backtracking)
If FlexSQL writes a plan and then realizes, "Oh no, I misunderstood the question entirely," it doesn't just try to fix the code. It hits the backtrack button. It goes all the way back to the beginning, re-examines the library shelves, changes its entire strategy, and starts a new plan. This prevents it from wasting time trying to fix a broken foundation.
The Results
The paper tested this "flexible detective" on a very difficult benchmark called Spider2, which simulates massive, real-world corporate databases.
- The Score: Using a model called
gpt-oss-120b, FlexSQL scored 65.4%. - The Comparison: This score was higher than other top systems that used much larger, more powerful models (like
DeepSeek-R1orgpt-o3). - The Takeaway: By being flexible—wandering the library, brainstorming multiple theories, speaking two languages, and hitting "undo" when needed—FlexSQL solved problems better than the "rigid robots," even with a smaller brain.
In short, FlexSQL proves that in the complex world of big data, flexibility beats rigidity. It's better to be an agent that can explore, adapt, and change its mind than one that stubbornly follows a single, pre-written map.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.