Ideaticon

Answers / Testing a text-to-SQL agent

How to test a text-to-SQL agent on your own data.

Write down the questions your people really ask, each with the figure your system of record gives. Ask the agent every question three times, in a new conversation each time. A question passes only when the figure matches on all three asks. An answer that reads well and carries the wrong figure fails.

A public benchmark is not your warehouse

Spider and BIRD score a model on public databases. Your warehouse has its own table names, its own joins, and terms such as margin or active customer that mean something particular to you. A model that scores well in public can return the wrong number here, and the query will still run without an error.

The test that predicts your rollout is built from your questions and your figures.

Build the question set

Collect real questions from the people who will ask them, and keep their wording. For each one, record the figure the system of record gives and the SQL that produces it.

A set that finds problems has one of each:

  • A plain lookup: one number for one period.
  • A comparison: this quarter against the last.
  • A term with synonyms, such as revenue, sales and turnover.
  • A period that is still open.
  • A question the data cannot answer.

The last two matter most. A good agent says that a month is still open, and says so when a question is outside the data.

Ask every question three times

A language model does not write the same query every time. Databricks says so in its documentation for Genie: the same prompt can return different outputs.

One right answer in one run proves little. Ask each question three times, each in a new conversation, and count it as passed only when all three figures match.

Let the figure decide

Run the reference SQL again for every check, so the expected figure reflects today’s data. Then compare the numbers. The wording around a number does not count. A fluent answer with the wrong figure fails, and a plain one with the right figure passes. An answer that declines a question outside the data passes too.

A sample scorecard

Question Reference Ask 1 Ask 2 Ask 3 Result
Revenue last month 4.82M 4.82M 4.82M 4.82M Pass
Gross margin, third quarter against second 31.4% and 29.8% Match Match Match Pass
Number of active customers 1,214 1,214 1,377 1,214 Fail
Margin for the month still open Open, to date Says open Says open Says open Pass
Churn next year Outside the data Declines Declines Gives a figure Fail
A sample. The questions and figures are invented and come from no client.

Three of five pass, and the two failures say what to fix. "Active" is read two ways, so it needs one definition. The agent also needs a rule for questions the data cannot answer.

Fix the layer, then run everything again

When a question fails, the cause is usually upstream of the model: a term nobody defined, two tables that both look like the right one, or a join the agent had to guess.

Fix it where the agent reads it: a definition on the column or the measure, a synonym, one certified table in place of three candidates. Databricks gives the same order in its guidance for Genie: measures and example SQL first, text instructions as a last resort.

Then run the whole set again. A fix for one question can break another.

When to run it again

  • A definition, a table or a join changes.
  • The model or its instructions change.
  • A new group of users is given access.
  • A miss is reported in real use. It becomes a question in the set.

What the test does not prove

It proves the questions in the set and nothing beyond them. People will ask others. So a rollout goes to named groups first, someone reads the misses, and each miss becomes a new question. The set grows with use.

Where the tools help

Genie has a benchmarks feature that holds up to 500 questions with reference SQL, and Snowflake has evaluations for Cortex Analyst. Use them to hold the set. A benchmark run in Genie records one result for each question, so run it three times, and check that a pass means the figures match.

Where we use it

This is the test we write into a statement of work before a build starts. At Concelex, people ask finance questions in Romanian, and the answers are tested against a fixed set of questions. It is part of how we build a semantic layer.

Contact

Start with the problem.

Tell us what needs to change. If you can, add what you run today, the date that matters, and how you will know it worked. Teo replies within 48 hours.