What does accuracy mean for text-to-SQL?
A text-to-SQL answer is accurate when three things hold: the query runs, the number matches an answer someone has checked, and the number uses the definition your business uses.
- The query runs. This is the easiest bar, and a query can pass it and still be wrong.
- The number matches a checked answer. Someone has worked it out separately, or finance has certified it.
- The definition is the one your business uses. "Revenue" might be gross or net. "Customers" might mean anyone in the customer file or only people with a delivered order.
A wrong answer can pass the first test and fail one of the others. It returns a clean number that looks right.
Why don't text-to-SQL benchmarks predict how an agent will do on your data?
A text-to-SQL benchmark usually tests one clean question turned into one SQL query, on someone else's data. Your users ask vague questions and follow up. Your agent searches your tables with its own tools and context, and it may not write SQL at all. A benchmark that leaves all of that out can't predict how the agent will do. The usual gaps:
- Cleaner data. Benchmark databases tend to be prepared, with documented columns and tidy values. Yours has raw tables, odd values and columns nobody documented.
- Fewer tables to search. Many benchmarks hand the agent the schema up front. Your agent may have to find the right table among thousands, or across several databases, and if its search tool doesn't surface the table, no amount of SQL skill helps.
- No business definitions. A benchmark's answer key doesn't know what revenue, a customer or summer means at your company.
- Questions that do the hard part. "Sum revenue where
is_retis'returned'inorders" names the table, the column and the value. "What did we lose to returns last quarter?" is how people ask, and finding the table, column, value and period is where agents go wrong. - One pass, not a conversation. Users ask, read the answer, and ask again. A good agent asks what they meant when a question has two readings, and a one-question test can't reward that.
- Saved answers. Some systems match known questions to approved, canned queries. That scores well on the questions they were written for and says little about a new question that combines them.
- A different setup. Your agent runs with its own tools, instructions and context files, and teams or people may each keep their own. Two people can send the agent in with different definitions and get different numbers.
- Moving scores. Benchmarks and leaderboards keep changing, and a system tuned to one may not do as well on another.
So test on your own data, with your agent, its tools and context, and the questions your users and their agents really ask. This guide says "text-to-SQL" because that is what people search for. Read it as "an AI agent answering questions from your data".
How do you test a conversational analytics tool with a golden question set?
Test it with a golden question set: your users' real questions, each paired with an answer someone has verified, called the golden. A new agent session answers every question without seeing the goldens, a second agent scores each result, and you run the set again after every change. Our post Your Data Model Is Part of Your AI System describes the method in full.
- Collect real questions from your logs, in your users' words, with the tool calls the agent made to answer them. The tool calls show what the agent searched for and which queries it ran. Include awkward questions, such as "best customers", and a few the data can't answer.
- Verify each golden. This is the hard part, and an agent can't do it alone. A golden is a figure finance certifies, a person's decision (which of three customer counts is "customers"), or two separate calculations from the raw tables that agree and that a person confirms.
- Hold some questions back. Use the rest to find and fix problems, and the held-back questions only to check that a fix wasn't tuned to the questions it was tested on.
- Answer with your users' real setup. Each question goes to a new agent session with the same tools, instructions, context files and access a real user's agent has, and no access to the goldens. Record which model, tools and files each run had, and test each setup your users really have.
- Score with a second agent that compares each answer with the golden and says whether the golden itself looks right.
- Find where each miss started, and fix it there.
- Run every question twice or more, and again after every change. A single run can't tell a reliable pass from a lucky one. Keep a fix only if its targets now pass, nothing else broke, and the held-back questions still hold, on two runs.
- Record what each run cost, in turns and tokens, next to how many answers were right. An agent that rediscovers the joins and measures for every question can still reach the right number, but it pays for that every time, and each rediscovery is another chance to choose differently. The open-source
eval-reportskill reports both.
How do you judge whether a text-to-SQL answer is right?
You judge a text-to-SQL answer by its result, not by its SQL, and "right" has more than one shape. A judge, which can be a second agent, compares each answer with the verified one. The open-source eval-judge skill scores each answer as a match, a near match or no match. These are the cases it has to tell apart:
| The answer | Example | Verdict |
|---|---|---|
| Gives exactly the verified answer | "What were July sales?" returns the July figure. | Correct |
| Gives the verified answer plus more | The same question returns sales for every month, with July's figure in the table. | Correct: the answer contains the verified figure |
| Uses different SQL to reach the same result | Two queries join in a different order, or the agent writes Malloy and Malloy translates it to SQL. | Correct: the judge compares results, not query text |
| Takes a different reading the question allows | "Best customers" ranked by one measure, when the question never said which and the measures disagree. | Near match: not counted as a pass or a fail |
| Says it can't answer a question that has an answer | The data holds the answer, but the agent didn't find it. | Wrong |
| Answers a question that has no answer | The data can't answer it, and the agent produced a number anyway. | Wrong |
| Says it can't answer a question that has no answer | The agent says the data doesn't cover it, or asks what you mean. | Correct |
The last three rows are why a good test includes questions with no answer on purpose. An agent that always produces a number will look accurate on a test where every question has one.
How do you find why a text-to-SQL answer failed?
Check two steps in order. First, did the agent find the right pieces? Text-to-SQL research calls this schema linking, and here it's called retrieval. Second, did the agent put those pieces together into the right query? Then name the cause, because the fix goes in different places: the data model, the agent's instructions, or the test itself.
What retrieval means depends on what the agent queries:
- With raw SQL, the agent finds tables and columns, and it also has to discover how the tables join and whether a join repeats rows. For "summer sales by category" it has to find the orders, order items and products tables, work out the keys, and decide what summer means.
- With a Malloy model, the joins are already declared, so the agent finds sources, dimensions and measures. It asks for the category by name, as
products.category, and the model supplies the join.
Check retrieval even when the answer is right. An agent can miss the model's measure, rebuild it from raw columns, and get the number right by luck. In one run that held for a simple count, then gave the run's only wrong answer on a measure with a rule the agent didn't know.
Some common causes, with what each looks like and where the fix goes:
| Example cause | What it looks like | Where the fix goes |
|---|---|---|
| A wrong assumption | Summer read as June to August, when the season runs 25 May to 15 September. The answer was 20% low. | Define the concept in the data model, or have the agent ask |
| The wrong concept | "Customers" read as all 1,000 in the file, when the business counts the 943 with a delivered order | A named, documented measure |
| The wrong table | A raw table that still holds test accounts and duplicates | Hide raw tables so the agent sees only clean sources |
| The wrong filter | 'complete' when the data says 'Complete', or a filter nobody asked for | Make stored values searchable; say what each term includes |
| The wrong measure | Gross revenue when finance reports net | One definition of each measure, in one place |
| A join that double-counts | Six orders total $955, but joined to their twelve line items, SQL sums $1,445 | Declared joins; aggregates that know their table |
| A data problem | A partial month read as a drop | A note in the model; the agent checks which dates the data covers |
| An uneven access rule | A region filter added to one query and forgotten on the next | An access filter on the source, applied to every query |
| A different answer next time | Two readings left open, and the agent picks one at random | Define the reading in the model |
| A wrong answer key | The key missed a measure the agent found | Fix the test |
A data model here means more than the table structure. It holds what each term means to your business (what revenue counts, which customers count, which table is the clean one) alongside how the tables join. The agent picks from those definitions for each question, and still checks the data itself, such as which dates it covers. A model can't define what nobody has decided, though. If your team has never said what "best customer" means, the agent still has to ask, so the test scores the agent's instructions as well as the model. Why AI numbers don't match your dashboard works through several of these causes with real figures, and Malloy vs. SQL shows three errors a Malloy model won't let you write.
What does a real text-to-SQL eval run find?
A real eval run finds that wrong answers have different causes, and that the fix is often not where you'd expect. The run in Your Data Model Is Part of Your AI System used Malloy Publisher's storefront example: a small store's data, a data model, and twelve questions, three of them about business rules deliberately left out of the model. It passed 8, failed 3 and came close on 1, and a second run failed the same three the same way. Answering and scoring cost $3.34.
- Summer sales: the agent assumed June to August and came in 20% low. Fix the model: define the season.
- Customer count: the agent offered 1,000 and 974 and never asked which customers count. The golden is 943. Fix the model: define a customer.
- Top customer by spend: a note added to fix net sales led the agent to drop returns when nobody asked. Fix the model: say what "spend" means.
- Best customers: the agent knew "best" wasn't defined and answered anyway, because its instructions said to state an assumption rather than stop. Fix the agent's instructions.
- The answer key itself missed a measure the agent found. Fix the test.
Five fixes in three places from one run. Record why each answer failed, not just the pass rate.
How do you keep a text-to-SQL eval working when the data changes?
Store how each golden was derived, not just its number, and keep the test set in the same repository as the data model. When new data arrives, re-derive the goldens from the raw tables. When a definition changes, re-check every golden that depends on it. Then run the set again. The open-source eval skills do each of these:
- Keep the derivation. A golden stored as a bare number goes stale when new rows land. A golden stored with the queries that produced it can be re-derived, and a changed value means the golden moved, not that the agent was wrong.
- Re-derive from the raw tables, not the model. Deriving goldens through the model you just changed would let an edit certify its own answer key.
- Keep the set beside the model. In the same git repository, a definition change and the goldens that depend on it move in one commit, so one can't drift from the other.
- Re-check goldens when a definition changes. In one run, a fix to a definition moved the "top customer" group from 108 customers to 87. Two answer keys still described the old behavior, and two correct answers were marked wrong for a full run before anyone noticed.
- Run again after every kind of change: new data, a new definition, an edited instruction file, an upgraded language model. Each can move an answer without anyone touching the others.
Goldens with no number, such as "the data can't answer this" or "the agent should ask which kind of customer", don't need re-deriving. See eval-import and eval-improve for the details.
What open-source tools run a text-to-SQL eval?
The eval skills in Malloy Publisher, built by the Malloy community, run each step of this test: importing questions, answering, judging, finding causes, and fixing. They are open source, ship with the storefront example set and its answer key, and run against any model Publisher serves, without buying anything.
To go further:
- How to build and run an eval: Your Data Model Is Part of Your AI System.
- Why a data model, and why Malloy: Why Malloy, Malloy vs. SQL, and The Semantic Layer Is an Interface, Not a Compiler.
- How an agent should work from question to answer: How an agent turns a question into a trustworthy answer.
- Access rules and curation: Curate What Your Agents See and Policy Is Prose. Compliance Is Controls.
Credible runs the same models and skills as a managed service. It checks sign-in and access rules on every query, and publishes each change to the model as a new version you can roll back to. Its engine also searches an index of the model and hands the agent only the part each question needs, as Inside the AI Analytics Engine explains. See how Credible controls access, pricing, or the docs.
Frequently asked questions
Which text-to-SQL tool is the most accurate?
No published score can tell you which tool is most accurate on your data. Tools differ in where they get its meaning: some read only table and column names, and others read written definitions of metrics and joins. Test each tool on your own questions, each with a checked answer, and compare the scores.
Why can a text-to-SQL benchmark overstate accuracy?
Because a benchmark tests someone else's data, agent and questions. Its data is cleaner, with fewer tables and no business definitions, and its questions are complete. Your users ask vague questions and follow up, and your agent runs with its own tools and context. Test on your data, with your agent and your users' questions.
Does a text-to-SQL answer count as correct if it states its assumption?
No, not if the number is wrong. The test is whether someone who acted on the number would be wrong. In one eval run, an agent said it assumed summer meant June to August and answered 20% low, so it failed. An answer with no number can still be correct, such as saying the data can't answer the question. The eval run, question by question →
How many questions do I need to test a text-to-SQL tool?
Enough to cover the questions people really ask, including the awkward ones. Malloy Publisher's example set has twelve. Beyond size, check that every answer is verified, that some questions are unanswerable on purpose, that some are held back to check fixes, and that the test runs again after every change.
Does a semantic layer improve text-to-SQL accuracy?
It can, because the agent looks definitions up instead of guessing them. When what revenue means, which joins are safe and what each field holds are written down where the agent reads them, less is left to guess. The agent can still choose its own reading of a word nobody defined, so measure the gain on your own questions.