🎆 Text-to-SQL1. How it works

Chapter 1

How it works

A business user types a question in English. A model turns it into SQL. We run the SQL and show the answer.

1Question + a description of the database go into a prompt
↓
2A Fireworks model writes a SQL query
↓
3We run the query on the database, read-only
↓
4The person sees a table, plus the SQL if they want it
Point to make in the interview The model only ever sees the schema (table and column names) and a few sample rows. It never sees the full data. That answers the usual customer worry, “are you sending our data to an AI provider?”. Drop the sample rows if the customer wants it stricter still.

The database: Chinook

A well-known demo database for a pretend online music shop. Customers buy tracks, and each purchase is an invoice made up of invoice lines.

WhatSize
Tables11
Customers59 in 24 countries
Invoices412 (2021–2025)
Tracks3,503

It’s small, but realistic enough: answering “revenue by genre” means chaining four tables, Invoice → InvoiceLine → Track → Genre. That chaining is where models used to fall over.

The prompt: every rule is there to stop a known failure

Rule in the promptFailure it prevents
“SQLite dialect, dates are text, use strftime()”Writing Postgres/MySQL date functions that don’t work here
“If it’s ambiguous, state your Assumption”Silently guessing what “best” or “last year” means
“If it can’t be answered, say CANNOT_ANSWER”Making up an answer from data that doesn’t exist
“Only SELECT, never change data”Running a DELETE because someone asked for one
Don’t trust the model, enforce it The read-only rule is in the prompt and the database is opened read-only. If a model ignores the prompt and writes DELETE, the database refuses. Instructions are a request; the connection setting is a guarantee.

The files

FileWhat it does
nl2sql.pyThe engine: builds the prompt, calls Fireworks, runs the SQL, lets the model fix it once if it errors, and adds up cost
questions.jsonThe test: questions plus their correct answers
eval.pyThe exam: runs every model on every question and scores them
app.pyThe web page a human would use