Query API
The request and response models of the query routes, for developers calling them over HTTP.
Request/Response Models
Source: packages/api/src/nl2sql_api/models/query.py
QueryRequest
| field | type | required | meaning |
|---|---|---|---|
question |
str |
yes | The question. (It was natural_language; that name is now a 422.) |
datasource_id |
Optional[str] |
no | Datasource override. |
execute |
bool |
no | Execute SQL against datasource (default true). |
choice |
Optional[ClarificationChoice] |
no | The option picked from an earlier clarification_request: send {"request_id", "option_id"} with the same question to answer it. |
approval_ids |
List[str] |
no | Ids of approvals an approver granted, from an earlier run a hook held. choice and approval_ids together are a 422 CLARIFICATION_INVALID, before the engine is called. |
The body is strict: a field it does not name is a 422, so a misspelt or removed field is never silently ignored.
SubQueryResult
The engine's own nl2sql.SubQueryResult, served as is.
| field | type | required | meaning |
|---|---|---|---|
id |
str |
no | Sub-query identifier. |
intent |
str |
no | Semantic intent of the sub-query. |
sql |
str |
no | SQL generated for the sub-query. |
datasource_id |
str |
no | Datasource the sub-query targets. |
schema_version |
str |
no | Schema version used for planning. |
plan |
Optional[Dict[str, Any]] |
no | The validated plan, dumped. |
validation |
List[ValidationCheck] |
no | Validation checks (name, passed, message). |
rows |
Optional[RowSample] |
no | Capped row sample (columns, rows, total_rows). |
status |
str |
no | "success" or "error" for this sub-query, from its final attempt: "success" when it ended with SQL and, if executed, a result; otherwise "error". A retry that recovers reports "success". "dropped" when a hook denied it with scope="sub_query": the run went on without it. |
retry_count |
int |
no | Plan/SQL refinement attempts made. |
plan_source |
"llm" \| "cache" \| "verified" \| "supplied" \| "approved" \| "chosen" |
no | "cache" when the plan came from the plan cache (no planner call; still validated and executed), "verified" when a verified query answered (there is no plan; sql is the verified query's own, with its :name placeholders), "supplied" or "approved" for a pinned plan (see Query API → Running a plan you hold), otherwise "llm". |
row_cap |
Optional[int] |
no | The row limit that bounded this sub-query's SQL, or null when the plan's own LIMIT was smaller. |
truncated |
bool |
no | True when the result reached row_cap, so rows may be missing. |
QueryResult
The engine's own nl2sql.QueryResult, served as is: the route's response_model is that class, so the HTTP response cannot drift from what the engine returns.
| field | type | required | meaning |
|---|---|---|---|
sub_queries |
List[SubQueryResult] |
no | One entry per decomposed sub-query, each with its SQL. |
final_answer |
Optional[Dict[str, Any]] |
no | Answer synthesizer payload (summary, format_type, content, warnings, truncated). |
errors |
List[Dict[str, Any]] |
no | Pipeline errors (node, message, error_code, severity, provider, provider_response). provider and provider_response are set for a PROVIDER_* code and null otherwise; see provider failures. |
trace_id |
str |
no | Trace identifier. |
reasoning |
List[Dict[str, Any]] |
no | Reasoning events/logs. |
warnings |
List[RunWarning] |
no | What the answer should say without failing the run: code, node, message, sub_query_id, detail. See Warnings. |
artifact_refs |
Dict[str, ArtifactRef] |
no | Result artifact references keyed by execution node id. |
status |
str |
no | "success", "error" or "plan_only" for the run. |
timings |
Dict[str, float] |
no | Wall-clock seconds per graph node. |
usage |
QuestionUsage |
no | LLM calls, input/cached/output/reasoning tokens and model time per node (nodes) and for the question (total), plus every call (calls) and the number of plans served from the plan cache (plan_cache_hits). The same model as QueryResult.usage; see the core query API. |
trace_path |
Optional[str] |
no | Where the run's trace file was written on the server, or null. See Debugging a Run. |
truncated |
bool |
no | True when any sub-query's result was cut at its row limit. |
result_manifest |
List[ResultSummary] |
no | The run's saved results, one per terminal DAG node: node_id, columns, row_count (the whole result's), truncated (a sub-query feeding it was cut at its row limit) and column_kinds (each column's type category: number, text, temporal, boolean or other, read from the saved file's column types). Never the rows: read those with the two result routes below, result_id being the node_id. Empty when nothing was saved (execute: false, or the run failed before aggregation). |
source |
AnswerSource |
no | Where the answer came from: {"type": "verified", "id", "datasource_id", "params"} when a verified query from curated.yaml answered, with the values it ran with; {"type": "generated"} (the other fields null) otherwise. The last event of POST /query/stream carries it too. |
Only a capped sample of the rows is inlined, in sub_queries[].rows. The full
result set is written to artifact storage and addressed through artifact_refs
(uri, format, row_count, columns). The run's combined results are listed in
result_manifest and read back, a page at a time or as CSV, with the results routes.
An errors[] entry for a key the provider rejected:
{
"node": "datasource_resolver",
"message": "OpenAI rejected the API key.",
"error_code": "PROVIDER_AUTH_FAILED",
"severity": "ERROR",
"provider": "OpenAI",
"provider_response": "HTTP 401 (invalid_api_key): Incorrect API key provided: [redacted key]. You can find your API key at https://platform.openai.com/account/api-keys."
}
Branch on error_code, show message, and keep provider_response for a "provider
response" disclosure. The codes are listed under
provider failures.
Endpoints
POST /api/v1/query
Source: packages/api/src/nl2sql_api/routes/query.py
Request model: QueryRequest
Response model: QueryResult
Execution flow:
- The identity.caller dependency supplies the caller's UserContext (see
Identity); with no identity it answers HTTP 401
before the engine runs.
- Delegates to engine.run_query(...), which returns a QueryResult.
- Returns that QueryResult unchanged; there is no field mapping.
The handler is declared with def, not async def: the pipeline performs blocking
LLM and database calls, so Starlette runs it in its threadpool instead of on the
event loop.
Keys and models in the request: where api.request_llm is on, a request may send X-NL2SQL-Api-Key-<provider>
headers (one provider's key each) and X-NL2SQL-Models (a JSON object of agent name to "provider:model", no secret)
for this run only. A model is honoured only on a provider the same request sent a key for, so it can never move a step
onto the operator's account, even if the server has a key for it (422 MISSING_PROVIDER_KEY). With request_llm
off the headers are a 403. A key is never logged, stored in a trace or echoed in a response. The stream route takes
the same headers.
Errors:
- Pipeline failures are a normal HTTP 200 carrying errors; they are not HTTP failures.
- Genuinely unexpected failures return HTTP 500 with a generic detail; the
traceback is logged server-side rather than returned to the client.
- No role for the request returns HTTP 401 with a message naming the settings.
- An invalid request body returns HTTP 422 (FastAPI validation).
Example response:
{
"sub_queries": [
{
"id": "sq-1",
"intent": "top customers by revenue",
"sql": "SELECT s.customer, SUM(s.revenue) AS revenue FROM sales AS s GROUP BY s.customer ORDER BY SUM(s.revenue) DESC, s.customer LIMIT 5",
"datasource_id": "warehouse",
"schema_version": "v3",
"plan": {"tables": [{"name": "sales", "alias": "s"}]},
"validation": [
{"name": "plan_present", "passed": true, "message": "Plan received from the planner"},
{"name": "structure_and_schema", "passed": true, "message": "Tables, columns and joins resolve against the retrieved schema"},
{"name": "policy", "passed": true, "message": "Role 'admin' may read every table in the plan"}
],
"rows": {"columns": ["customer", "revenue"], "rows": [["acme", 42]], "total_rows": 5},
"status": "success",
"retry_count": 0,
"plan_source": "llm",
"row_cap": null,
"truncated": false
}
],
"final_answer": {
"summary": "Acme leads the top 5 customers by revenue, with 42.",
"format_type": "text",
"content": "Acme leads with 42. The full list of 5 customers is in the table.",
"warnings": [],
"truncated": false
},
"errors": [],
"trace_id": "0f1c...",
"reasoning": [],
"warnings": [],
"artifact_refs": {
"sq-1": {
"uri": "file:///artifacts/sq-1.parquet",
"format": "parquet",
"row_count": 5,
"columns": ["customer", "revenue"]
}
},
"status": "success",
"source": {"type": "generated", "id": null, "datasource_id": null, "params": null},
"timings": {"ast_planner": 0.02, "logical_validator": 0.001, "generator": 0.006, "executor": 0.038},
"usage": {
"total": {"calls": 3, "input_tokens": 13200, "cached_input_tokens": 0, "cache_write_input_tokens": 0,
"output_tokens": 590, "reasoning_tokens": 0, "total_tokens": 13790, "latency_s": 4.1, "cost": null},
"nodes": {
"decomposer": {"calls": 1, "input_tokens": 1900, "cached_input_tokens": 0, "cache_write_input_tokens": 0,
"output_tokens": 250, "reasoning_tokens": 0, "total_tokens": 2150, "latency_s": 1.3, "cost": null},
"ast_planner": {"calls": 1, "input_tokens": 11000, "cached_input_tokens": 0, "cache_write_input_tokens": 0,
"output_tokens": 300, "reasoning_tokens": 0, "total_tokens": 11300, "latency_s": 2.2, "cost": null},
"answer_synthesizer": {"calls": 1, "input_tokens": 300, "cached_input_tokens": 0, "cache_write_input_tokens": 0,
"output_tokens": 40, "reasoning_tokens": 0, "total_tokens": 340, "latency_s": 0.6, "cost": null}
},
"calls": [
{"node": "decomposer", "model": "gpt-4o-2024-08-06", "input_tokens": 1900, "cached_input_tokens": 0,
"cache_write_input_tokens": 0, "output_tokens": 250, "reasoning_tokens": 0, "total_tokens": 2150,
"latency_s": 1.3, "cost": null, "usage_reported": true, "error": null}
],
"plan_cache_hits": 0
}
}
(Illustrative numbers. usage.calls is shortened to one entry; a real response lists all three.)
POST /api/v1/query/stream
The same run as POST /api/v1/query, reported as it happens, as
Server-Sent Events
(Content-Type: text/event-stream). The request body, the caller role and the 401
are exactly those of /query. Delegates to engine.stream_query(...) (see
the core query API).
The events are the engine's own run events
(Events), from the caller-safe allow-list. Each
is an event: line naming it and one data: line holding the whole event as
JSON: name, at (UTC), trace_id (the run's, its trace_id in the result
routes), tenant_id and data. The table lists each event's data:
| event | data | when |
|---|---|---|
run.started |
question, user_id, tenant_id, roles, datasource_id, execute |
The run began. Always first. |
node.started |
node, sub_query_id |
A pipeline step began. The SQL agent's steps (ast_planner, logical_validator, generator, executor) come once per sub-query, and again on a retry. |
node.finished |
node, sub_query_id, seconds, ok |
The step ended; ok is false when it raised. |
result.manifest |
results |
The aggregator saved the run's results. results is the result_manifest the final response will carry, so a client can start paging the rows (with the event's trace_id) before the answer is written. |
answer.delta |
field, text |
The next piece of the answer's summary or content, as the answer model writes it. Joined in order, the pieces of a field are that field of final_answer. |
sanity.flagged, run.limit_reached |
see Events | A sanity check's finding; a cost or call cap reached. |
run.finished |
the run's summary, plus result: the QueryResult |
The last event of a run that finished: result is exactly what /query returns for it. A refusal or a provider failure is a run.finished with status: "error", as /query returns it. |
run.failed |
message (a key in it is redacted), error_code; and user_id, roles when the engine ended the run |
The last event of a run that could not finish (an unexpected failure; the traceback is logged on the server). |
Every stream ends in exactly one run.finished or one run.failed. Policy
denials, hook decisions and model calls are never on it; they are an
operator's, through engine.events.subscribe.
event: run.started
data: {"name": "run.started", "at": "2026-10-15T09:12:03.120Z", "trace_id": "0f1c...", "tenant_id": null, "data": {"question": "Who are the top 5 customers by revenue?", ...}}
event: node.started
data: {"name": "node.started", ..., "data": {"node": "datasource_resolver", "sub_query_id": null}}
event: node.finished
data: {"name": "node.finished", ..., "data": {"node": "datasource_resolver", "sub_query_id": null, "seconds": 0.41, "ok": true}}
...
event: result.manifest
data: {"name": "result.manifest", ..., "trace_id": "0f1c...", "data": {"results": [{"node_id": "sq_1", "columns": ["customer", "revenue"], "row_count": 5, "truncated": false, "column_kinds": {"customer": "text", "revenue": "number"}}]}}
event: answer.delta
data: {"name": "answer.delta", ..., "data": {"field": "summary", "text": "Acme leads "}}
...
event: run.finished
data: {"name": "run.finished", ..., "data": {"status": "success", "duration_s": 6.2, ..., "result": {"sub_queries": [...], "final_answer": {...}, "status": "success", ...}}}
curl -N -X POST http://localhost:8000/api/v1/query/stream \
-H "Content-Type: application/json" -H "Authorization: Bearer $NL2SQL_KEY" \
-d '{"question": "Who are the top 5 customers by revenue?"}'
What streams token by token: only the answer. The answer model is asked to stream
on this route (and on this route only; /query is unchanged), on both wires:
OpenAI's (OpenAI, OpenRouter, Ollama and any OpenAI-compatible endpoint) streams
the answer's tool call as argument pieces, Anthropic's as input_json_delta
pieces, and the engine reads summary and content out of them as they grow.
How finely the text arrives is the provider's choice: a provider or a local
server that sends the whole answer in one piece gives one answer.delta per
field. The other model steps (resolver, decomposer, planner, refiner) return a
typed object the next step needs whole, so they are not streamed; their progress
is their node.started and node.finished. A streamed answer reports the same
token usage as an unstreamed one (stream_options.include_usage is sent on the
OpenAI wire).
The route answers Cache-Control: no-cache and X-Accel-Buffering: no, so a
proxy passes each event on as it comes. Closing the connection cancels the run
before its next step or model call.
Tests
packages/api/tests/test_query_routes.py, test_query_stream_route.py and test_query_response_shape.py cover these
routes with FastAPI's TestClient, a stubbed engine (get_engine override) and a stubbed caller, so no datasource, LLM
or network access is required. packages/api/tests/test_identity.py covers the identity sources and the 401.
packages/nl2sql/tests/e2e/test_stream_fake_llm.py streams a question on the demo databases through the stream route
with no key. The results, traces, feedback and approvals routes have their own pages and test files.