Design, implementation and evaluation
Local-First Text-to-SQL Agent
An execution-guided agent that turns questions into SQL on a single 8 GB GPU, with an ablation harness that shows which parts actually earn their place.
- Execution accuracy on BIRD Dev (n = 499), up from 50.70%
- 53.51%
- p50 latency cut, 180.7 s to 10.28 s on one 8 GB GPU
- 17.6x
- Generated SQL that parses and runs on BIRD Dev
- 99.0%
The problem
Text-to-SQL demos look solved until the database is real. BIRD Dev schemas are wide, the questions are ambiguous, and a model that writes syntactically valid SQL can still be confidently wrong. The first working version of this system reached 49.9% execution accuracy on the raw local model — a number, not a result, because nothing in it explained why an answer failed or which pipeline stage was responsible.
I wanted two things at once: a local pipeline that runs on one 8 GB RTX 5060, and a harness that could attribute a change in accuracy to a change in the system rather than to sampling noise.
Approach
- Read-only safety layer.
t2s/db.pyandt2s/safety.pyenforce SELECT-only queries, table allowlists, a 100,000 row cap, 30 s timeouts and optional PII column masking. The agent cannot mutate the database it is measuring itself against. - Hybrid schema linking. BM25 and dense embeddings are fused with Reciprocal Rank Fusion, then columns are pruned by a separate
llm.pruner_modelso JSON pruning is decoupled from SQL generation, and foreign-key join paths are expanded. - Parallel candidate generation. Candidate 0 runs greedily at temperature 0.0; candidates 1–4 sample across direct, chain-of-thought and decompose prompts at temperature 0.7, concurrently through
ThreadPoolExecutor(max_workers=min(n, 4))with deterministic ordering. - Execution-guided repair and voting. Syntax errors, runtime errors and empty result sets are repaired for up to two iterations. Candidates are clustered by identical execution results, and the winning cluster's agreement fraction becomes the confidence signal.
- Live confidence cascade.
cascade_conf_threshold=0.6escalates low-agreement or failed local queries toopenai/gpt-oss-120bon Groq, wired intoText2SQLAgent.answer(). - A QLoRA path. 8,954 ChatML examples exported straight from
train.json+train_tables.json(raw DDL) for 4-bit QLoRA (r = 16, α = 32) via Unsloth, merged to 16-bit, quantised to Q4_K_M and served by Ollama. - 36 unit tests cover safety validation, schema linking, API end-to-end behaviour, parallel candidate ordering and cascade escalation.
- 01Question plus read-only, allowlisted schema
- 02Hybrid linking: BM25 + dense, fused with RRF
- 03Decoupled LLM column pruner and FK expansion
- 04Five parallel candidates: 1 greedy, 4 sampled
- 05Execute, repair twice, cluster by result set
- 06Agreement as confidence; escalate below 0.6
Results
All 11 ablations ran on the same 500-query BIRD Dev sample (499 valid gold queries), seed 0. The held-out split is seed 1 with frozen hyperparameters.
| System (seed 0) | Avg tokens | EX | Note |
|---|---|---|---|
+values | 2,412 | 47.3% | Unfiltered trigram re-injection, +52% column bloat |
+values_fixed | 2,007 | 51.1% | Score ≥ 0.85 filter, +3.81% vs +values (p = 0.0204) |
+linking | 1,950 | 52.3% | Hybrid RRF + LLM pruning, −19% tokens vs baseline |
| Ft + decoupled | 1,949 | 52.1% | Base pruner + SFT generator, no token drift |
+voting_novalues | 8,019 | 53.9% | Five-candidate result-set voting |
| Offline cascade | 8,404 | 54.9% | Threshold 0.6, $0.000105 per query |
The headline rests on one held-out comparison, run once with frozen hyperparameters:
| Held-out (seed 1) | Linking only | With voting |
|---|---|---|
| Execution accuracy | 50.70% | 53.51% |
| Generated SQL that runs | 91.6% | 99.0% |
| Queries fixed / broken | — | +28 / −14 |
That is +2.81 points at exact McNemar p = 0.0436: 28 queries fixed, 14 broken. Uncached p50 latency on the 326 strictly non-overlapping queries falls to 10.28 s (mean 13.52 s) from 180.7 s sequential.
What went wrong
The +values regression was self-inflicted. Re-injecting fuzzy trigram hit_cols after pruning looked harmless and inflated linked columns from 12.8 to 19.5 (+52%), dropping EX from 52.3% to 47.3%. Filtering hits to score ≥ 0.85 recovered +3.81% and cut tokens to 2,007. The lesson is that unselected context is worse than no context.
One epoch of QLoRA stayed flat. Decoupling the pruner removed the +16% token drift (2,330 → 1,949) while EX held at 52.1% versus 52.3%. The likely cause is a train/inference schema-format mismatch — raw CREATE TABLE DDL in SFT against rendered schemas with sample rows and BIRD descriptions at inference — plus only 98 packed training steps. Retraining on the inference format for 2–3 epochs is the next experiment, not a claim.
The live cascade was a null result on held-out data. It gained +1.00% offline on seed 0 (p = 0.1797) but scored 53.11% against 53.51% for local voting on seed 1 (p = 0.7905). With 47% of failures already upstream in schema linking, escalating only the final SQL generator has a low ceiling.
- Schema linking miss47%
- Aggregation or filter logic27%
- Value grounding12%
- Join logic9%
- Ambiguous evidence4%
- Syntax or dialect1%
Limits. Everything is evaluated on 500-query BIRD Dev splits, not the full 1,534-question Dev set or Spider. The error taxonomy is LLM-classified, not human-adjudicated. PostgreSQL execution exists in t2s/db.py but is untested. Voting agreement discriminates well (AUROC 0.6823 on seed 0, 0.6618 on seed 1) but is badly calibrated (ECE 0.2645 / 0.2676), so confidence is a ranking signal, not a probability.