This is a clean fit for the JSON API: you send the database schema and a question, and you get back a structured object with the query. Because the output is schema-constrained, you can pull theDocumentation Index
Fetch the complete documentation index at: https://docs.opper.ai/llms.txt
Use this file to discover all available pages before exploring further.
sql field straight out and execute it.
The example uses an in-memory SQLite database so it runs as-is.
The assistant
How it works
- The
Queryschema forces the model to return asqlstring (plus its reasoning). With structured output you readresult.json_payload["sql"]and run it directly, no parsing or regex. - Passing the live
schema(db)means the model writes queries against your real tables, not guessed ones. - The
reasoningfield 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
Schemas
Pydantic, JSON Schema, and field descriptions.
Hints
Optimize for cost or speed without naming a model.
Streaming
Stream the query as it generates.
Observe
Score generated SQL automatically.