Skip to main content
This is a clean fit for structured output: you send the database schema and a question, and you get back a JSON object with the query. Because the output is schema-constrained, you can pull the sql field straight out and execute it. The example uses the OpenAI SDK pointed at the gateway, so the same code runs against any model. The example uses an in-memory SQLite database so it runs as-is.
python app.py

You › Who are the two highest-paid people in Engineering?

SQL › SELECT name, salary FROM employees WHERE department = ‘Engineering’ ORDER BY salary DESC LIMIT 2;

[(‘Dan’, 151000), (‘Alice’, 145000)]

The assistant

Run it:

How it works

  • The schema forces the model to return a sql string (plus its reasoning). With structured output you parse the response once and run out["sql"] directly, no regex.
  • Passing the live schema(db) means the model writes queries against your real tables, not guessed ones.
  • The reasoning field is optional but useful: it gives you a plain-English trace of why the query looks the way it does, which shows up on the call’s trace.

Make it more robust

  • Retry on error. Catch SQLite exceptions, send the error message back as input, and ask for a corrected query.
  • Read-only safety. Run the generated SQL against a read-only connection so a bad query can’t modify data.
  • Score it. Add an Observe rule that checks the query ran and returned rows.

What’s next

Structured output

JSON Schema, enums, and field descriptions.

Route

Set a default model per project without touching code.

Streaming

Stream the query as it generates.

Observe

Score generated SQL automatically.