# Semantic layer answer evaluation

One AI model answered 55 questions about public Chicago rideshare trips two ways: by writing SQL against the tables (raw SQL), or by picking
metrics that a semantic layer defines once (governed metric queries, compiled by dbt's MetricFlow). The questions were written alongside the metric
definitions they test; the AI answers over company data study reuses them byte for byte. On the first run the totals were close,
46/55 and 45/55, from two opposite results: the semantic layer won on joins
(14/15 against 9/15) and lost on simple questions (13/20 against
19/20). Most governed misses were one formatting slip, so a second run, declared before it was collected but
designed after that slip was seen, enforced a strict response format and scored 51/55 on the same questions, a self-test of
the fix. Its join lead (13/15 against 9/15) has an interval that reaches zero. The page
(`index.html`) explains the result for a non-specialist reader; this file covers the numbers and how to reproduce them.

Method failures: none of the 180 responses was cut
off at the output ceiling or failed at the provider, so nothing is left out ([results-sensitivity.json](results-sensitivity.json),
declared in [evals/sensitivity-declaration.json](evals/sensitivity-declaration.json)).

Every answer replays from a saved copy, so no API key is needed. The data is public City of Chicago trip records, and every right answer was
checked against expected values on a separate synthetic table.

## First run (the primary result)

| Outcome | Raw SQL | Governed metric query |
|---|---:|---:|
| Correct analytical answers | 45/55 | 46/55 |
| Expected clarification checks passed | 3/5 | 4/5 |

Paired analytical-accuracy difference: +1.8 percentage points.
Paired question-bootstrap interval: [-12.7, +16.4] percentage points.
Template-clustered interval, which resamples whole question templates (added after the results were seen):
[-20.0, +29.7] percentage points over 28 template groups.
Both intervals describe this fixed, related question set, not a population.

Model: `gpt-4.1-mini-2025-04-14`, one answer per question and arm. The first run has 120 responses.

![Observed answer accuracy](assets/accuracy.svg)

Rounding check, added after inspecting the primary results: allowing rounding to four decimal places adds 4 matching raw SQL answers and 0 matching governed answers. Under that check raw SQL would score 49/55 and governed 46/55, so the paired difference reverses sign, from +1.8 to -5.5 percentage points. The primary scores keep the frozen tolerance of one millionth.

## Second run: strict response format

7 of the 9 governed failures on analytical questions in the first run used the malformed filter key `operator=` in place of `operator`. The second run was declared in a frozen protocol addendum (`evals/run2-protocol-addendum.json`) before collection and after the first
run's results were seen. It re-collected only the governed arm with a strict JSON-schema response format. Model, questions, messages,
scoring and settings stayed fixed; the raw SQL answers are reused from the first run. The first run stays the primary result.

| Measure | Run 1 (JSON object) | Run 2 (strict JSON schema) |
|---|---:|---:|
| Governed metric query, correct analytical answers | 46/55 | 51/55 |
| Raw SQL, correct analytical answers (same answers) | 45/55 | 45/55 |
| Paired difference, pp | +1.8 | +10.9 |
| Question-level interval, pp | [-12.7, +16.4] | [0.0, +23.6] |
| Template-clustered interval, pp | [-20.0, +29.7] | [0.0, +29.7] |
| Governed clarification checks passed | 4/5 | 3/5 |
| Governed responses with the malformed key | 7 | 0 |

Governed failures in run 2: 4 questions. Grouped by a column the question did not ask for: 3. Strict mode requires every field,
so each run 2 answer carried a `group_by` list, possibly empty. One of the grouped answers was correct in run 1, which sent no group_by list. In run 2, one answer returned the example wording from the prompt as its clarification.
Outcomes are in `results-run2.json`;
both runs' clustered intervals are in `results-clustered.json`.

## Reproduce

Prerequisites: Linux with Bubblewrap user namespaces enabled, Python 3.12.3 and uv 0.11.7.
The namespace boundary is mandatory; there is no unsandboxed execution fallback.
All Python packages are pinned in `requirements.lock`. Installation needs internet access.
The licensed data sample and released response cache are included, with hashes.

```sh
uv venv --python python3.12 .venv
uv pip sync --python .venv/bin/python requirements.lock
make all
```

After installation, `make all` builds the dbt marts, runs fixture checks, scores only
the released cache, and regenerates this report and static page. A missing or stale cache fails.
No credentials are required. Open `index.html` directly, or preview with `python3 -m http.server 8000`.
Initial startup can be slow when the environment lives on a Windows-mounted WSL drive.
The page shell, fonts and number formatting come from viz-design-system release 2.0.4, copied
unedited into `vendor/viz-design-system/` and pinned in `vendor/viz-design-system-adoption.json`.

Collecting new answers is separate: `python collect.py development`, then `python collect.py freeze`
and `python collect.py final`. Do not overwrite the released frozen run. Use a new study directory for a new collection.
The declared second run used `python collect.py freeze-run2`, `preflight-run2` and `final-run2`;
it writes only to `evals/run2-protocol-addendum.json` and `cache-run2/`. `make all` scores both runs from their caches without credentials.

## Method and limits

- The final set contains 60 questions, 20 per tier. Of these, 55 request analytical answers and
  5 require clarification. Questions and gold SQL are fixed before final collection.
- Each gold query is tested against expected values on a small synthetic fixture. Gold SQL is not produced by MetricFlow.
- The raw SQL arm receives schema names and types. Governed metric queries also receive the
  metric contract and a constrained interface. This compares the whole bundle, so it does not isolate
  the effect of any single metadata field.
- Both arms use temperature 0 and an 800-token output ceiling. A fixed provider seed does not prove
  deterministic behaviour. There is one retained outcome per question and arm.
- Numeric comparison uses absolute and relative tolerances of one millionth, without prior rounding.
  NULL equals only NULL. Duplicate rows count. Unordered row sets use matching; requested order matters.
  Day-grain midnight timestamps normalize to civil dates. Column order follows the question.
- Clarification requires an explicit object with a predefined topic word. This coarse automated
  check needs a separate human judgment to establish usefulness.
- The January 2024 sample is the first 10,000 trip IDs in publisher order. It is reproducible,
  not a probability sample for citywide inference. Precise coordinates, sub-day timestamps and
  original trip IDs are excluded from the public seed.
- Published fares and tips are rounded; NULL fares and tips stay NULL. Cash tips are absent.
  Inconsistent authorization and matching flags are retained. Component sums can disagree with
  published total bills; they are not silently repaired.
- Historical freshness uses a fixed evaluation clock and simulated next-day arrival timestamps.
  It demonstrates an operational check, not observed publisher delivery performance.
- SQL runs in a namespace with only the public database and pinned dependencies, no credentials,
  private checkout or network. Relations and functions, CPU, memory, result rows and bytes are bounded.

The source contract is `metric-contract.json`; data provenance is `data/manifest.json`.
The complete run is `evals/run-manifest.json`; outcomes are `results.json`.
The declared second run is `evals/run2-protocol-addendum.json`; outcomes are `results-run2.json`.
The versioned reusable evaluator and data bundle is described in [REUSE.md](REUSE.md).
Its archive and per-file digests are in `artifacts/contract-manifest.json`.

## Data notice

Read [NOTICE.txt](NOTICE.txt) for the required City disclaimer and attribution.

## Round 2: agents with tools (declared before collection)

Four models answered the 55 analytical questions three times each in a working loop with tools, in two setups: a `run_sql` tool only, or `run_sql` plus `query_metrics`, backed by the MetricFlow project. Every tool call is logged. The protocol, labelling rule, scoring and intervals were fixed in `evals/run3-protocol.json` before collection. Declared changes are in `evals/run3-amendment-1.json` to `evals/run3-amendment-7.json`; those made after the results (3 to 7) set how nine groups whose runs were all right or all wrong print, as counts with no interval, how four groups with a miss on 1 or 2 questions print, as a rough lower end rounded down, and add the gap across all runs of each setup. `results-run3.json` holds the scored values. The page is `agents-with-tools.html`.

| Model | Queried raw trip tables first, SQL only | Same, with the layer | Accuracy, SQL only | Accuracy, with the layer |
|---|---:|---:|---:|---:|
| claude-sonnet-5-5 | 160/165 | 12/165 | 147/165 | 163/165 |
| claude-haiku-5-5 | 152/165 | 73/165 | 145/165 | 163/165 |
| gpt-6.1-sol | 133/165 | 2/165 | 165/165 | 165/165 |
| gpt-6-luna | 159/165 | 27/165 | 159/165 | 163/165 |

Within-group comparisons need 30 runs from 10 questions in each setup; only claude-haiku-5-5 met that for the runs that queried the raw trip tables first (no measurable difference). Outcomes, intervals and counts are in `results-run3.json`.

Replay without credentials: `python score_run3.py` scores the saved runs in `cache-run3/` and reproduces `results-run3.json` byte for byte; `python render_run3.py` renders the page. Collection for the GPT models: `python agents.py final --model <id>` (Codex CLI on a clean configuration).

## Round 3: questions written blind (declared before collection)

The same four models, setups, prompts, tools and step limit as round 2 answered 40 questions written by an independent author who never saw the layer, the prompts or the earlier questions, three times each. The questions were written by an author independent of the models tested. The design is `DESIGN-blind.md`; the protocol, with the hashes of the questions, the answer keys and the code, is `evals/blind2-protocol.json`, fixed before collection. The layer can express 9 of the 40 questions. The page is `blind-questions.html`.

| Model | Published 55, strict | Published 55, counting notes | Blind 40, strict | Blind 40, counting notes | SQL right on blind (largest possible gain) | Held, strict |
|---|---:|---:|---:|---:|---:|---|
| claude-sonnet-5-5 | +9.7 (+3.0 to +17.6) | +2.4 (−1.8 to +7.9) | −5.8 (−15.0 to +2.5) | +1.7 (−2.5 to +6.7) | 107/120 (+10.8) | no |
| claude-haiku-5-5 | +10.9 (+4.2 to +18.8) | +10.9 (+4.2 to +18.8) | +0.8 (−5.8 to +8.3) | +0.8 (−5.8 to +8.3) | 115/120 (+4.2) | no |
| gpt-6.1-sol | 0.0 (0.0 to 0.0) | 0.0 (0.0 to 0.0) | +0.8 (−2.5 to +5.0) | +0.8 (−2.5 to +5.0) | 117/120 (+2.5) | yes |
| gpt-6-luna | +2.4 (−0.6 to +6.1) | +2.4 (−0.6 to +6.1) | −15.0 (−24.2 to −6.7) | −15.0 (−24.2 to −6.7) | 104/120 (+13.3) | no |

The gap is accuracy with the layer minus accuracy with SQL only, in points, with a question-clustered interval. "Strict" is the answer format fixed before collection; "counting notes" also counts answers that put a sentence before a correct JSON answer, in both setups and both rounds (added after the results). SQL alone was right on 87% to 98% of blind runs, which left the layer between +2.5 and +13.3 points to gain. The tool description lists DAY_OF_WEEK, which this database lacks; on the weekday questions that cost the layer setup more misses than SQL only. Outcomes, coverage, latency and safety counts are in `results-blind2.json`. No reading of outputs is published; a declared sample of verdicts is left for a person to read.

Replay without credentials: `python score_blind.py` scores the saved runs in `cache-blind2/` and reproduces `results-blind2.json` byte for byte; `python render_blind.py` renders the page. Collection for the GPT models: `python blind2.py final --model <id>`.

Some files are packed in semantic-layer-evaluation-data-1.zip and its sibling parts; unzip them here to restore the full export, which the manifest lists file by file.
