Dev.to AI 🤖 Ai 👁 0 📖 3 min read

How I Built an Automated LLM Evaluation Harness to Eliminate AI Math Hallucinations on CSVs

The Problem With "Chat With Your CSV" Apps Most AI spreadsheet tools do one of two scary things: Paste your private CSV rows directly into an LLM prompt and pray it doesn't hallucinate the math, or Let the LLM write

How I Built an Automated LLM Evaluation Harness to Eliminate AI Math Hallucinations on CSVs

The Problem With "Chat With Your CSV" Apps

Most AI spreadsheet tools do one of two scary things:

  1. Paste your private CSV rows directly into an LLM prompt and pray it doesn't hallucinate the math, or
  2. Let the LLM write arbitrary Python code and run exec() on the server — a massive Remote Code Execution (RCE) risk.

While building AskGrid ("Don't Scroll, Just Ask.") solo on a \$0 budget with Python 3.12, FastAPI, Pydantic v2, and Pandas, I locked down two non-negotiable rules:

  • Zero raw rows ever go to the LLM (only column names + inferred data types).
  • Zero LLM math and zero exec() — the LLM's only job is to act as a Natural-Language Intent Router, outputting a strict Pydantic JSON AnalysisIntent. Deterministic Pandas code executes 10 allowlisted operations and returns a "How this was calculated" audit trail.

The Architecture: LLM-as-Router + Deterministic Execution

User Question ("Top 3 customers by spend in Colombo?")
        │
        ▼
[Schema-Only Prompt]  ──▶  Gemini Flash-Lite (temperature=0.0, Pydantic Structured Output)
        │                  (sees ONLY {column: type} — ZERO row values!)
        ▼
AnalysisIntent JSON   ──▶  Allowlist Validator (blocks fake columns, bad dtypes, n > 50)
        │
        ▼
Deterministic Pandas  ──▶  Exact Result + ChartSpec + CalculationAudit ("How it was calculated")

To handle real-world cloud flakiness on a free tier, I put Gemini behind a provider-agnostic Python Protocol (AIProvider) with a 5-model fallback chain and a lightweight Circuit Breaker that skips models returning 503 / 429 / transport timeouts during a cooldown window.

3 Real Bugs My Golden Dataset & Eval Harness Caught

You don't appreciate automated tests and eval harnesses until they save you in real life. Mine caught three sneaky bugs before a single user ever touched the UI:

1. The Currency Regex That Ate Order IDs (ORD-1011 → -1011)

My CSV parser cleans currency strings like LKR 125,000.00 into floats. My first regex matched [A-Za-z]{1,5}\s*-?\d+ — which accidentally matched "ORD-1011" as negative currency -1011!

Golden test Q08 caught it immediately. I tightened the regex to require real currency symbols or known currency codes + space, and added a regression test.

2. The httpx2.ReadTimeout That Escaped My Circuit Breaker

My Gemini adapter caught (ServerError, ClientError, ValueError). During a live eval run, a slow free-tier response threw httpx2.ReadTimeout — which isn't a subclass of ServerError! Widening the fallback catch inside the model loop fixed it so network timeouts cleanly trip the Circuit Breaker and route to the next healthy Flash pool.

3. Prompt v1.0 → v1.1: Why "Looks Good in Swagger" Isn't Enough

I built an end-to-end eval harness (server/evals/run_evals.py) that runs 18 synthetic golden cases (15 computable questions across all 10 operations + 3 safety/refusal/prompt-injection cases) all the way from plain English → live Gemini → validator → Pandas → exact ground-truth number assertion.

First run (PROMPT_VERSION = "v1.0"):

  • Intent Accuracy: 17/18 (94.4%)
  • E2E Numerical Exact: 14/15 (93.3%)
  • Safety & Prompt Injection: 3/3 (100.0%)

Why did Q07 ("Who are the top 3 customers by spend in Colombo?") fail? Gemini got the filter, column, and grouping right — but picked group_aggregate (returning all 4 Colombo customers) instead of top_n(n=3)!

The Fix (PROMPT_VERSION = "v1.1"):

I added one explicit disambiguation rule separating top_n (ranked/limited questions) from group_aggregate (all groups) and re-ran the full suite:

Metric Prompt v1.0 Prompt v1.1
Intent Accuracy 17/18 (94.4%) 18/18 (100.0%)
E2E Exact Pandas Math 14/15 (93.3%) 15/15 (100.0%)
Safety & Refusals (incl. Prompt Injection) 3/3 (100.0%) 3/3 (100.0%)
Median Latency (p50) 1.74s 1.17s

What's Next

The entire Python + AI backend (v0.3.0) is now live on GitHub with ruff, strict mypy, pytest (23 offline CI tests), pip-audit, and Gitleaks secret scanning. Next up: building the React + TypeScript + Tailwind CSS interactive workspace UI!

  • Follow the build in public on X: https://x.com/niRmana_dev
📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.