yitae.dev
← Back to Experiments
Ontology & GraphRAG Construction · 2026-11-15 · Local Benchmark Harness · 9 min read
Mixed

Ontology in the Prompt: +0.725 on Computed Financial Metrics, −0.225 on Multi-Condition Lookups

Paired Stage A evaluation (1,600 runs) plus public upstream operational findings: why static ontology injection lifts Tier 2 (+0.725 via curated SQL attributes) while regressing Tier 3 (-0.225 via template/entity shifts), and why Stage B DB benchmarks remain withheld per AUP.

Headline result: Tier 2 +0.725 (p=4.74e-15) vs Tier 3 -0.225 (p=4.01e-05) on Postgres

On this page (7 sections)
Pre-registered Hypothesis, Falsification Criteria & Environment Manifest
Hypothesis
Supplying explicit ontology context improves query accuracy uniformly across all five reasoning tiers on both Postgres and Neo4j.
Falsification Criteria
Any statistically significant accuracy drop (paired McNemar p < 0.05) in any reasoning tier under ontology grounding refutes uniform improvement.
Host / Compute
Local benchmark harness (~/lab/auto-ontology-bench)
Runtime
Postgres 16 (local) + Neo4j (Stage A Text2SQL/Cypher only; DB-only Stage B withheld per Neo4j AUP)
Workload / Dataset
5 reasoning tiers × 40 questions × 2 temp=0 repeats per condition (Tier 5 tests unanswerable abstention)
Sample Size / Repeats
1,600 Stage A LLM+DB requests (400/condition across 2 repeats; Stage B DB benchmarks omitted per AUP)

1. Motivation: Why Static Ontology Injection Backfires

When Text2SQL or Text2Cypher pipelines return wrong numbers, teams reflexively append full domain ontologies, type hierarchies, and column rules into the system prompt. On paper this removes ambiguity; in practice, stuffing 1,400–1,600 extra tokens of schema rules into every request alters query generation differently depending on question topology.

2. Tier Definitions & Real Paired Examples (cq_dataset.jsonl)

Stage A evaluated four conditions (neo-no-onto, neo-onto, pg-no-onto, pg-onto) across 5 query tiers (80 paired requests per tier; 1,600 total). Here is what each tier tests and what flipped in actual records.jsonl runs:

  • Tier 1 (Direct 1-hop entity lookup): “Who sits on TSMC’s board of directors?”
  • Tier 2 (Computed ratio / derived metric — Biggest Ontology Win): “What was Albemarle’s return on equity in fiscal year 2024?” (cq2_085)
    • Without ontology (pg-no-onto → empty): guessed a literal row WHERE md.name ILIKE '%return on equity%' that does not exist in metric_definitions.
    • With ontology (pg-onto → correct): knew ROE is computed from net_income ÷ stockholders_equity and synthesized joined CTEs over metric_code = 'net_income'.
  • Tier 3 (Multi-condition similarity / attribute filter — Ontology Regression): “Which other companies disclose risk factors in the ‘Operations and manufacturing’ risk category, as TSMC does?” (cq3_133)
    • Without ontology (pg-no-onto → correct): resolved WHERE ticker = 'TSM' cleanly.
    • With ontology (pg-onto → empty): emitted WHERE name ILIKE '%TSMC%' (missing rows stored under full legal name Taiwan Semiconductor...) and extra column constraints, dropping matched rows to zero.
  • Tier 4 (Multi-hop / supply-chain traversal): “As of 2026-06-30, which companies within two supply tiers upstream of SpaceX did Renaissance Technologies hold?” (cq4_163)
    • With ontology (pg-onto → syntax error): synthesized self-referencing CTE WITH upstream AS (... FROM upstream up ...) omitting WITH RECURSIVE, triggering PostgreSQL UndefinedTableError.
  • Tier 5 (Unanswerable abstention): “Give Caterpillar’s most recent number of patents granted.” (Expected: decline).
Three Divergent Effects of Full Ontology Prompting
Static Ontology Prompt~2.5K–2.8K tokens(metric formulas + entities)Tier 2 Computed Ratios: 0.17 → 0.90 Postgres (+0.725)Formula axioms resolve missing metric codes (wrong answers drop 28 → 1)Tier 3 Multi-Cond Lookups: 0.49 → 0.26 Postgres (−0.225)Over-specified name filters & joins shift outputs into empty results (41 → 50, syntax = 1)Tier 4 Multi-Hop Paths: Syntax/Exec Errors Spike 12 → 39Recursive path concepts without SQL WITH RECURSIVE grammar cause invalid CTEs
How one static ontology prompt produces a Tier 2 breakthrough, Tier 3 over-filtering, and Tier 4 broken recursive SQL.

3. Environment & Statistical Protocol

Both conditions ran paired evaluation over 40 test questions × 2 temperature-0 repeats per tier (80 pairs/tier, 1,600 Stage A requests total; gold items remain pending-human-review), scoring lenient gold-set match on Tiers 1–4 and correct abstention on Tier 5. Significance uses two-sided paired McNemar tests; bracketed ranges denote 95% bootstrap confidence intervals. Per Neo4j’s Acceptable Use Policy (FINDINGS.md §3), Stage B DB-only throughput and latency sweeps are withheld and not published.

4. Visual Evidence & Quantitative Results

Paired bar charts comparing accuracy with 95% confidence intervals across Tier 1 to Tier 5 on Postgres and Neo4j.
Figure 1. Paired Stage A accuracy across Tiers 1–5 (n=80 pairs/tier; 1,600 total requests). Adding ontology lifts Tier 2 across both engines (+0.725 Postgres, p=4.74e-15; +0.525 Neo4j, p=4.46e-10) while hurting Tier 3 (-0.225 Postgres, p=4.01e-05; -0.125 Neo4j, p=0.0129).
ConditionTier 1Tier 2Tier 3Tier 4Tier 5Overall (n=400)
neo-no-onto0.39 [0.28–0.50]0.07 [0.03–0.14]0.28 [0.19–0.38]0.16 [0.09–0.24]0.86 [0.79–0.94]0.35 [0.31–0.40]
neo-onto0.36 [0.26–0.47]0.60 [0.50–0.71]0.15 [0.07–0.24]0.12 [0.05–0.20]0.88 [0.80–0.95]0.42 [0.37–0.47]
pg-no-onto0.78 [0.68–0.86]0.17 [0.10–0.26]0.49 [0.38–0.60]0.26 [0.17–0.36]0.90 [0.82–0.96]0.52 [0.47–0.57]
pg-onto0.78 [0.68–0.86]0.90 [0.82–0.96]0.26 [0.16–0.36]0.15 [0.07–0.24]0.86 [0.79–0.94]0.59 [0.54–0.64]

Inspecting per-tier outcome records (records.jsonl) reveals where each failure shift actually happened alongside Stage A context/latency overhead:

Left: per-tier outcome breakdown on Postgres from Tier 1 to Tier 4. Right: Stage A prompt token expansion and median LLM inference latency.
Figure 2. Left: Per-tier Postgres failure modes (n=80 pairs/tier) proving Tier 3 lost accuracy through empty/wrong results (41 → 50 empty, 0 → 8 wrong, syntax = 1), whereas SQL syntax/execution errors spiked on Tier 4 (12 → 39). Right: Static ontology injection doubles prompt tokens (881 → 2,517 Neo4j; 1,357 → 2,788 Postgres) and adds +2.1–2.7s median LLM latency (Stage B DB-only benchmarks withheld per AUP).
ConditionCorrect (T1–4)EmptyWrong AnswerSyntax + Exec Erre2e p50 (ms)LLM Total p50 (ms)Prompt Tokens
neo-no-onto72 / 320205266 + 17,5677,443881
neo-onto99 / 320198112 + 010,34110,1532,517
pg-no-onto136 / 3201363413 + 05,6985,6971,357
pg-onto167 / 320971538 + 37,9017,8362,788

5. Mechanism Breakdown: Why Aggregate Accuracy Masks Two Opposite Regimes

  1. Ontology fixes missing derived formulas via curated attributes (Tier 2): Without derived metric definitions (net_income / stockholders_equity), the LLM invents nonexistent metric names (wrong answers drop from 28 to 1 on Postgres, 18 to 6 on Neo4j). Crucially, FINDINGS.md §2 notes Auto-Ontology’s automatic layer compiled 0 SQL attributes from row-stored metrics (metric_definitions × financial_metrics)—the 42 human-curated SQL attributes drove the Tier 2 jump.
  2. Aggregate +0.070 hides Tier 3 template/grounding drift and Tier 4 recursion bugs: Tier 3 did not break because of syntax errors (only 1 syntax error in Tier 3). Auditing 31 Postgres regressions revealed two concrete causes: matched concept-level query templates induced per-row rather than distinct outputs, and the extra context shifted entity linking away from canonical tickers (152 → 105 queries) toward loose name filters like ILIKE '%TSMC%'. Syntax errors happened in Tier 4, where abstract path semantics pushed self-referencing CTEs without WITH RECURSIVE. Meanwhile, Text2Cypher trailed Text2SQL by 0.17 across both arms due to exact property lookups {name: '...'} (122–146 of ~320 queries/arm at ~0% accuracy vs 0.45–0.67 when ticker-anchored).
  3. LLM inference, not local Postgres latency, dominates E2E cost: Local Postgres DB time remained under 0.05% of E2E latency (1.1 → 2.6 ms vs 5,697 → 7,836 ms LLM); doubling prompt size added ~2.2–2.7 seconds to median LLM inference, plus 54 s median on check_answerable.

6. Key Findings & Transferable Insights

Distilled via multi-agent deliberation (Codex systems practitioner + Claude evidence gate) from FINDINGS.md §1–§2:

  • Curate row-stored metrics explicitly; automatic schema ingestion misses semantic metrics: Auto-Ontology compiled 0 SQL attributes from metric_definitions rows, whereas 42 curated attributes drove +0.725. Rule: Inspect compiled attribute yield directly instead of trusting schema ingested = covered.
  • Do not run uncalibrated check_answerable inline on concurrent paths: _run_lock serialized check_answerable at ~1 call/53 s (79/80 timed out past 180 s at c=8), and answerable Tier 2 questions scored only 0.20–0.33 coverage—forcing dev calibration to choose no abstention gate.
  • Assert non-empty context (ctx_chars, ctx_matched_sql) in request telemetry: Candidate IDs turned out to be attribute IDs, not term IDs—causing silent 404s that passed empty ontology contexts (*-buggy runs), while incompatible lightning-30b threw HTTP 400 × 3 (~89 s) before silently falling back to the raw prompt.
  • Anchor entities canonically before query generation & restrict ingested catalog views: Ticker-anchored Cypher (0.45–0.67) massively beat ungrounded exact {name: ...} matches (~0%), and naive pg_catalog introspection ingested internal monitoring views like pg_stat_statements.

7. Scope Limits & Next Experiment

  • Dependent repeats & review status: The 80 pairs/tier represent 40 questions × 2 temperature-0 repeats, all 250 gold items remain pending-human-review, and Tier 2 curation itself can favor the ontology arm (FINDINGS.md §6). Stage B Aura DB throughput sweeps are withheld behind Neo4j AUP (yw-pow.6).
  • Next step: Test tier-gated selective ontology injection—supply curated metric definitions only on computed signatures, resolve canonical ticker/entity IDs deterministically before filter generation, and supply explicit WITH RECURSIVE templates for Tier 4 path queries.