Skip to main content

Module schema

Module schema 

Source
Expand description

Schema introspection — turn an open Connection (or raw Database) into the textual schema dump the LLM uses to ground its SQL generation.

§Why we don’t SELECT FROM sqlrite_master

The sqlrite_master catalog persists the original CREATE TABLE SQL, so reflecting via SQL would work. But we already have the same information typed in Database.tables — going through SQL would mean parsing the round-tripped CREATE statement a second time. Walking the typed structure is cheaper and matches what the engine considers authoritative right now (relevant inside an open transaction, where the persisted catalog may already be stale relative to the in-memory snapshot).

§Determinism (matters for prompt caching)

Tables are dumped in alphabetical order, columns in declaration order. The output is byte-stable for a fixed schema — that’s what lets cache_control: ephemeral actually hit on repeat calls. Any change to the format invalidates the cache once for everyone, but steady-state hits are cheap.

Functions§

dump_schema_for_connection
Render the schema of every user-visible table on conn as a sequence of CREATE TABLE … (…); statements, sorted alphabetically by table name.
dump_schema_for_database
Same as dump_schema_for_connection, but takes a Database reference directly. Used by the REPL’s .ask meta-command (which holds &Database, not &Connection).