Skip to content

Diogo TravassosData leadership · Analytics · Decision-making

Public dataAnswer keys checked on a synthetic table

Semantic layer answer evaluation

Part 2 of 3 in What an AI needs from company data

In one run on 55 questions written alongside the metric definitions, a semantic layer helped an AI model on joins (14 vs 9 of 15) and hurt it on simple questions (13 vs 19 of 20), so the totals tie

One model, gpt-4.1-mini, answered each question once, writing its own SQL or picking metrics that the semantic layer defines once: 46 and 45 of 55 right in total, a gap too small to tell either way. Seven of the nine first-run misses through the semantic layer were one formatting slip. A strict response format, designed after seeing that slip and scored on the same questions, took it to 51 of 55; whether that holds on new questions is untested. Counting answers right to four decimals as right, raw SQL would lead the first run, 49 to 46.

Key figures
Questions that need a join14/15first run, semantic layer; raw SQL 9/15; 13/15 in the strict-format run, with an interval that reaches zeroSimple questions13/20first run, semantic layer; raw SQL 19/20; 19/20 with the strict format, a tie near the ceilingStrict format, second run51/5593% right, in a self-test: designed after the first run's misses were seen and scored on the same questionsWrong results with no error shown2 vs 10first run, semantic layer vs raw SQL; 4 vs 10 with the strict format, 4 vs 6 if answers right to four decimals count

Main limitation One small model answered each question once. The questions were written alongside the metric definitions they test, using the semantic layer's own metric names, and the AI answers over company data study reuses them byte for byte, so these counts describe this question set, not how an assistant would do on your data.

A second round gave four current models tools to query the data, three tries per question, and found that the semantic layer stopped most of their runs from querying the raw trip tables first: agents with tools.

A third round asked the same four models 40 questions written blind by an independent author who never saw the layer, the prompts or the earlier questions, and the layer's result held for one of them: questions written blind.

In plain words

More teams now let people ask an AI chatbot for a number instead of opening a dashboard. The chatbot can write SQL against the tables itself, or it can ask a semantic layer, where the data team has defined each metric once, for the metric by name. A wrong number from a chatbot looks exactly like a right one, so two questions matter: which way gets more answers right, and what do the misses look like?

I tested both ways on public Chicago rideshare trips. One model, OpenAI's gpt-4.1-mini, answered 60 questions each way: 55 with one right answer, checked against SQL I wrote, which was tested against expected values on a small synthetic table, and 5 that the data cannot answer, where the right move is to ask back. The setup stands in for an AI chatbot that a software company's customers use to chart their own data. The data is public and the checks synthetic so that no customer data is shared.

On the first run the totals were close, 46 and 45 of 55 right, too close to call either way. The total hides two opposite results. On the simplest questions the semantic layer lost, and 7 of its 9 misses were one formatting slip: the model wrote a filter key as "operator=" instead of "operator", which the interface rejects. On questions that need a join it won, 14 of 15 against 9 of 15, mostly because the SQL the model wrote for itself got definitions wrong, for example averaging daily averages and counting only the trips that had a tip. I then declared a second run that made the model fill a strict template, which removed the slip: 51 of 55. Because I designed that run after seeing the slip, it shows the fix works on these questions, not that it would hold on new ones.

Semantic layer
One place where a data team defines each metric, for example what counts as a trip and whether an average fare skips missing fares, so every dashboard and assistant asks for it by name. This study uses dbt's open-source MetricFlow.
Governed metric query
The model chooses metrics, groupings and filters from that list, and the semantic layer writes the SQL.
Raw SQL
The model sees the table and column names and writes the SQL itself.
Strict response format
The model fills a fixed template whose field names the provider enforces, so a misspelt key cannot come back.
Gold answer
The right answer, from SQL I wrote and tested against expected values on a small synthetic table.
Percentage points
The gap between two percentages: 83.6% against 81.8% is a gap of 1.8 points.
Interval
The range the gap could take if the questions had been slightly different. One that crosses zero means no clear winner.

What the model got right

Correct answers by kind of question. The raw SQL answers are the same for both runs; only the semantic-layer side was run again.
Kind of questionRaw SQLSemantic layer, first runSemantic layer, strict format
Simple filters and dates (20 questions)191319
Grouping and combinations (20 questions)171919
Questions that need a join (15 questions)91413
All analytical questions (55)45 46 51
Should ask back instead of answering (5)343
Wrong answers that showed a result, not an error1024

Read across the rows. In the first run raw SQL got more simple questions right and fewer joins; the semantic layer was the opposite. With the strict format both sides sit near the top on simple questions, where this test can no longer separate them, and the semantic layer keeps most of its lead on joins, with an interval that reaches zero. The bottom row is the one I care most about: when raw SQL was wrong, it showed a result 10 times out of 10, so nothing told the reader to doubt it.

First run: the semantic layer's lead is +1.8 points, and its interval runs from −12.7 to +16.4, across zero.

Governed metric query minus Raw SQL accuracy on 55 analytical questions, in percentage points, with two intervals: one resamples single questions, one resamples whole question templates (see Method and checks).

Paired accuracy difference with two bootstrap intervalsGoverned metric query minus Raw SQL accuracy: +1.8 percentage points. Paired question-level bootstrap interval from −12.7 to +16.4; template-clustered interval from −20.0 to +29.7. Both include zero.−30−20−100+10+20+30First run, question-level interval: Governed metric query minus Raw SQL accuracy +1.8 points, interval −12.7 to +16.4, includes zeroQuestion-level interval−12.7+16.4+1.8First run, template-clustered interval: Governed metric query minus Raw SQL accuracy +1.8 points, interval −20.0 to +29.7, includes zeroTemplate-clustered interval−20.0+29.7+1.8Raw SQL betterGoverned better
Show the data
Exact stored values; accuracies and interval are proportions (1.0 is 100%)
MeasureValue
Questions55
Raw SQL correct45
Governed correct46
Raw SQL accuracy0.8181818181818182
Governed accuracy0.8363636363636363
Difference0.018181818181818077
Question-level interval, lower−0.12727272727272726
Question-level interval, upper0.16363636363636364
Bootstrap replicates10000
Template-clustered interval, lower−0.2
Template-clustered interval, upper0.2972972972972973
Template groups (all analytical questions)28

Download every answer (results.json)

In the strict-format run the lead over raw SQL is +10.9 points, and both intervals start at 0.0, so it is not clearly above zero even on these questions. What the second run shows is the semantic layer's own increase, from 46 to 51 right once the slip was gone, in one run on the questions the fix was designed against; how much it varies between runs, and how it does on new questions, is unknown. That column measures a rule more than the model: the provider enforces the template, so the slip could not happen. The first-run columns show the model's own judgment.

What went wrong, and what I missed

What I would use when

Where each approach fits
ApproachUse it forWatch out for
The model writes raw SQLAnalysts exploring data who read the query before they trust the number. Simple counts and date filters: 19 of 20 right here.It writes its own definitions. All 10 of its misses here looked like answers.
Governed metric queries through a semantic layer (dbt's MetricFlow here, or a BI tool's own semantic model)Any number a customer or executive sees, and questions that need a join or an agreed definition: 14 of 15 right here.The interface becomes the weak point: one formatting slip caused 7 of 9 misses. Every metric needs a named owner.
A strict response formatEvery time code reads what a model returns. It costs a setting and a schema.Required fields get filled: three answers here added a grouping nobody asked for. Check the answer against the question, not only the format.
Asking backQuestions the data cannot answer, such as waiting times or cash tips, which this data does not record.Hard to score automatically. Have a person read a sample every week.

My pick: for anything customer-facing, governed metric queries with a strict response format and a check that rejects any grouping the question did not ask for. Raw SQL stays for internal analysts who read the query. And one home for metric definitions: a second semantic model inside a BI tool, next to the semantic layer, recreates the problem the layer exists to remove.

How I would roll this out with a team

What it is worth

The decision a data leader faces is whether to build a semantic layer before letting an AI answer customers. The measured part: with the strict format, the semantic-layer side returned 4 wrong results with no error shown, against raw SQL's 10 (6 if four-decimal rounding counts as right). That is 2 to 6 fewer wrong numbers per 55 questions.

The rest are my assumptions, labelled as such. A wrong number that a customer acts on costs about one hour of an analyst's or account manager's time to trace, explain and correct, at US$75 an hour. The rollout above costs about four engineer-weeks at US$4,000 each, so US$16,000. On those figures the semantic layer pays for itself after about 2,000 to 5,900 questions: 4 to 12 months for an assistant answering 500 questions a month, 1 to 3 months for one answering 2,000. That leaves out the expensive tail: one wrong number in a renewal meeting or a board deck.

That range moves only the measured gap. Moving the assumed inputs too, from half an hour to two hours per wrong number, US$50 to US$100 an hour and two to eight engineer-weeks, the payback runs from about 370 to 35,200 questions: under a month to about 70 months at 500 questions a month (every combination). The time a wrong number costs and the rollout effort move it as much as the gap does.

The strict format is the easy call: about a day of work on my estimate, and it turned all seven formatting errors here into answers. The model calls cost under a tenth of a cent per answer, so the money is in people's time, not in tokens. My provisional call: for an assistant that answers customers more than a few hundred times a month, build the semantic layer first and switch on the strict format the same day, then confirm it on held-out questions from real customers before relying on it.

Where AI fits

This study is how I check any AI that answers with numbers. The dangerous errors are plausible numbers with no error attached, because a good answer and a bad one look the same. So I ask the model questions whose answers I already know, compare two setups on the same questions like a control group, read every miss, and turn what I learn into hard rules the model has to follow. Here those rules are metric definitions it cannot rewrite and a response template it cannot misspell. Then I run it again, declared in advance, and keep the first result on the page.

The next test is declared here and not yet run: two or three current models, five answers per question instead of one, the strict format on both sides, the rounding rule fixed in advance, and a fresh set of questions written by someone who has not seen these misses.

The AI answers over company data study reuses these questions byte for byte with four models, three runs each. The agent-ready data marts study tests the other side of the same problem with current models: how much documentation a model needs to answer correctly from tables in the first place.

Check any answer yourself

Every answer below is the model's original response, replayed from a saved copy, beside the gold answer. Nothing on this page calls a model, runs a query or tracks you.

Raw SQL

Original model response
Query

Returned rows

Governed metric query

Original model response
Query

Returned rows

Independent gold answer

Method and checks

7 of the 9 first-run misses through the semantic layer were one formatting slip, a malformed filter key, scored as an invalid response. The declared second run used a strict response format, and the page reports both runs, with the first still the main result.

What the two sides receive

The governed side receives metric definitions and selects an allowed query. MetricFlow compiles that selection. The raw SQL side receives table and column names and writes SQL. This compares the whole interface and metadata bundle, including its restrictions, so the difference does not isolate the effect of any single metadata field.

Both sides use the same model settings and token ceiling. Errors and abstentions stay in the denominator. Development questions are separate from the final set of 60 questions, 20 per tier.

Results by question tier, with intervals

Each tier has 20 or fewer questions from one run, so these intervals are wide. The template-clustered interval resamples whole question templates instead of single questions; tier 1 is only 2 templates of 10 questions each, so its clustered interval can take only a few values, and tier 3 has no shared templates, so both intervals match.

Correct analytical answers by tier, first run. Difference is governed minus raw SQL in percentage points, with its question-level and template-clustered bootstrap intervals below. Tier 3 also has 5 clarification questions, scored separately.
TierRaw SQLGoverned metric queryDifference
1 · Scalar and date filters19/2013/20−30.0question-level −55.0 to −5.0template-clustered −60.0 to 0.0 (2 template groups)
2 · Grouping and combinations17/2019/20+10.0question-level −10.0 to +30.0template-clustered −10.0 to +45.5 (11 template groups)
3 · Joins and clarification9/1514/15+33.3question-level +13.3 to +60.0template-clustered +13.3 to +60.0 (15 template groups)

7 of the 9 governed failures on analytical questions share one model error: the response used the malformed filter key "operator=" in place of "operator" and was scored as an invalid response. All 7 are tier 1 questions, where the governed arm had 7 failures and raw SQL had 1 failure. This count is read from the stored responses; scoring is unchanged.

Does a strict response format change it? The second run in full

7 of the 9 governed failures in the first run came from one malformed filter key. The second run sent the same 60 questions to the same model through the governed interface, with a strict JSON-schema response format in place of a free JSON object. The first run stays the primary result.

With the strict format, governed metric queries answered 51/55 analytical questions correctly, against 45/55 for raw SQL. The paired difference is +10.9 points, with a question-level interval from 0.0 to +23.6 and a template-clustered interval from 0.0 to +29.7. Both intervals reach zero, so this one run does not establish a clear advantage over raw SQL. None of the 7 malformed filter keys recurred.

Second run: the paired difference is +10.9 points.

Governed metric query (strict response format) minus Raw SQL accuracy on 55 analytical questions, in percentage points, with both bootstrap intervals.

Paired accuracy difference with two bootstrap intervalsGoverned metric query minus Raw SQL accuracy: +10.9 percentage points. Paired question-level bootstrap interval from 0.0 to +23.6; template-clustered interval from 0.0 to +29.7. Both include zero.−30−20−100+10+20+30Second run, question-level interval: Governed metric query minus Raw SQL accuracy +10.9 points, interval 0.0 to +23.6, reaches zeroQuestion-level interval0.0+23.6+10.9Second run, template-clustered interval: Governed metric query minus Raw SQL accuracy +10.9 points, interval 0.0 to +29.7, reaches zeroTemplate-clustered interval0.0+29.7+10.9Raw SQL betterGoverned better
Show the data
Exact stored values; proportions (1.0 is 100%)
MeasureValue
Difference0.10909090909090902
Question-level interval, lower0.0
Question-level interval, upper0.23636363636363636
Template-clustered interval, lower0.0
Template-clustered interval, upper0.2972972972972973

Download the second run (results-run2.json)

First run (free JSON object) beside the second run (strict JSON schema). Tier rows count analytical questions.
MeasureFirst runSecond run
Governed metric query, correct analytical answers46/5551/55
Raw SQL, correct analytical answers (same answers in both)45/5545/55
Paired difference, pp+1.8+10.9
Question-level interval, pp−12.7 to +16.40.0 to +23.6
Template-clustered interval, pp−20.0 to +29.70.0 to +29.7
Tier 1 · Scalar and date filters, governed (raw SQL 19/20 in both)13/2019/20
Tier 2 · Grouping and combinations, governed (raw SQL 17/20 in both)19/2019/20
Tier 3 · Joins and clarification, governed (raw SQL 9/15 in both)14/1513/15
Governed metric query, clarification checks passed4/53/5
Governed responses with the malformed key "operator="70
Why governed analytical answers failed, by question ID (the IDs match the inspector above), read mechanically from the stored responses and returned rows
CauseFirst runSecond run
Malformed filter key "operator=" (scored invalid)Q11, Q12, Q13, Q14, Q16, Q17, Q19none
Grouped by a column the question did not ask forQ40Q16, Q40, Q52
Filter value matched no trips (empty result)noneQ54
Other wrong answer or errorQ54none
  • The second run was declared and frozen before it was collected, after the first run's results were seen. It fixed the model, questions, messages, scoring and settings.
  • Only the governed side was collected again; the raw SQL answers are the first run's. One model answer per question.
  • Strict mode cannot offer a choice of two objects at the top level, so each answer arrives inside one "answer" key, which scoring removes before applying the unchanged first-run rules. Strict mode also requires every field, so each answer carried a grouping list, possibly empty.
  • Three second-run failures grouped by a column the question did not ask for (Q16, Q40, Q52). Q52 was answered correctly in the first run, which sent no grouping list; the required field may have prompted the extra column.
  • In the second run, Q58 returned the example wording from the prompt as its clarification, so the coarse topic check failed it.
How much the intervals can tell you

The intervals describe resampling this fixed question set. Related, purposively written questions and a single model run do not support a population-wide reliability claim. Questions built from one template tend to fail together, so the template-clustered interval resamples whole templates (28 groups; the largest holds 10 questions) and runs from −20.0 to +29.7 points for the first run. I added it after seeing the results.

Run settings
  • Model: gpt-4.1-mini-2025-04-14, temperature 0 and an 800-token output ceiling on both sides. A fixed provider seed does not prove deterministic behaviour. There is one retained outcome per question and side.
  • The first run has 120 responses; the second run adds 60 governed responses.
  • Interval: paired percentile bootstrap over question IDs, 10,000 replicates.
  • Template-clustered interval: the same bootstrap over whole question templates, 10,000 replicates. Split each question at its first ", return ". The template key is the text before it plus every sentence after the first sentence of the return clause. Questions sharing a key with at least one other question form one template group; every other question is its own group.
  • Method failures: none. All 180 responses stopped on their own, with none cut off at the output ceiling and none failed at the provider, so no answer is scored wrong for a setup reason (a census declared before it was run; results-sensitivity.json). The malformed filter keys are the model's own answers and stay scored as wrong.
Scoring rules and the asking-back check

Numeric comparison uses absolute and relative tolerances of one millionth, without prior rounding. NULL equals only NULL. Duplicate rows count. Requested order matters.

Clarification checks: Raw SQL 3/5; governed 4/5. These use a coarse topic-matching check. They do not rate how helpful the question back was.

Data and its limits

The data is a deterministic convenience sample of 10,000 public Chicago trips from January 2024. Published fares and tips are rounded. Cash tips and driver identifiers are unavailable. Missing values remain missing.

Some source records disagree about sharing authorization. The defined match fraction uses all trips. This study does not measure profit, driver activity, enterprise performance or any employer's results.

Displayed numbers

Headline percentages are rounded to whole numbers for reading. To one decimal they are 81.8% for raw SQL, 83.6% for the semantic layer in the first run and 92.7% in the second. Elsewhere percentages and percentage points are rounded to one decimal, half away from zero, with a true minus sign for negatives. Exact values are in the chart data tables and in results.json.

Data notice

Data attribution and required notice

Source: City of Chicago, Transportation Network Providers Trips (2023-2024), dataset n26f-ihde;
and Boundaries Community Areas, dataset igwz-8jzy.
Reuse is subject to the City of Chicago Data Terms of Use:
https://www.chicago.gov/city/en/narr/foia/data_disclaimer.html

This site provides applications using data that has been modified for use from its original source, www.cityofchicago.org, the official website of the City of Chicago. The City of Chicago makes no claims as to the content, accuracy, timeliness, or completeness of any of the data provided at this site. The data provided at this site is subject to change at any time. It is understood that the data provided at this site is being used at one’s own risk.

Area 0 is a derived label for outside-Chicago or unavailable area codes.
No City endorsement is implied.

Use this at your company

To test an AI assistant against your own semantic layer, paste this prompt into an assistant that can query your warehouse.

This method comes from Diogo Travassos's study "Semantic layer evaluation" (https://diogotravassos.dev/semantic-layer-evaluation/). If you can open links, read it first: it works the method through end to end, with its checks. The steps below stand on their own if you can't.

Help me test whether an AI assistant answers business questions more reliably by picking metrics from our semantic layer (metrics defined once, in one place) than by writing its own SQL against the tables.

Data you need from me: read access to the warehouse tables, the semantic layer's metric and dimension definitions, and 40 to 60 real questions people ask, each with an answer I have checked.

Method:
1. Ask the assistant every question twice: once writing SQL on the tables, once choosing a metric, filters and grouping from the semantic layer. Keep the model, settings and wording the same.
2. Run both answers and compare each with the checked answer. Count an error, a refusal or a malformed reply as wrong.
3. Group the questions by type: simple filters, breakdowns, and questions that need a join.

Checks before trusting the answer:
- Never show the checked answers to the assistant.
- Read every miss and sort it by cause. If one formatting slip explains most misses, fix the format in a declared second run and report both runs.
- Count wrong answers that came with no error, because those are the ones a person would believe.
- Questions built from the same template fail together, so resample whole templates when you estimate the range.

Report back: accuracy for each approach overall and by question type, the causes of misses, the wrong answers that showed no error, and the limits: one model, how many questions, and which of them you wrote.

In your report, say which study you followed and link it: https://diogotravassos.dev/semantic-layer-evaluation/