Recipe
Natural Language Database Query
Ask questions in plain English and get structured results from your database — no SQL required.
Overview
This recipe wires a natural language input to a schema-aware LLM pipeline that generates, validates, and executes SQL against your live database. Results stream back as formatted tables.
Ingredients
- Schema introspection endpoint (tables, columns, types, foreign keys)
- LLM prompt template with schema context injection
- SQL validator and read-only guard
- Result formatter with column type awareness
Steps
- 1Introspect schema. Fetch table metadata and serialize it as a compact DDL-like summary for the prompt.
- 2Build the prompt. Combine the user question, schema summary, and strict output-format instructions into a single LLM call.
- 3Validate the SQL. Reject any statement that is not a SELECT. Check table and column existence against the schema.
- 4Execute and format. Run the query with a row limit and timeout. Render results as a responsive table with proper type formatting.
Security note: Always enforce read-only access. Never allow INSERT, UPDATE, DELETE, DROP, or ALTER. Use a dedicated database user with SELECT-only privileges.