Back to Resources

How Chion Prevents Hallucinated SQL: Schema Profiling to Execution

Full pipeline trace showing how Chion prevents SQL hallucinations: schema profiling → entity resolution → typed contract → L1/L2 validation → execution → repair.

By Jonathan Dag··10 min read

What is SQL hallucination?

SQL hallucination occurs when an LLM generates SQL that references tables, columns, or values that don't exist in the actual database. The query might be syntactically valid but semantically wrong — referencing a revenue column that's actually called total_amount, or JOINing on a relationship that doesn't exist.

The pipeline trace

1. Schema profiling

Before any LLM sees anything, Chion catalogs every table, column, data type, foreign key, and constraint. This creates a ground-truth map of what actually exists.

2. Column value sampling

Top values per column are sampled and embedded into a pgvector index. When you mention "Acme Corp," entity resolution matches it to the actual value in your company_name column — not a hallucinated variant.

3. Entity resolution

Your question terms are resolved to physical columns via vector search. "Revenue" maps to orders.total_amount, not a hallucinated revenue column.

4. SQL contract

A typed specification constrains the SQL generator to columns, tables, and relationships that actually exist. Out-of-contract references are a hard failure.

5. Two-layer validation

L1 blocks non-SELECT mutations. L2 validates column references, JOIN conditions, and aggregation rules against the schema profile.

6. Execution and repair

If the query fails at execution, up to 3 repair cycles rewrite it using the error message and contract context.

The result

The LLM generates SQL — but the pipeline ensures it references real tables, real columns, and real relationships. Schema profiling prevents hallucination at the structural level, not the prompt level.