Which workloads suit ai_query(), and how to run them.
From Ultra Transcenders DBX-GENAI by Tony Rough (coming December 2026)
If a workload is a table of rows rather than a person waiting, it is a batch job. ai_query is the general-purpose AI Function that sends each row to a model from SQL or Python, with no endpoint for you to manage when the model is Databricks-hosted.
Databricks recommends starting with a task-specific function such as ai_classify, ai_extract or ai_summarize when one matches, and turning to ai_query for precise control over prompt, parameters or output, or for custom, fine-tuned or external models.
SELECT text, ai_query(
"system.ai.meta-llama-3-3-70b-instruct",
"Summarise this article: " || text,
modelParameters => named_struct(
'max_tokens', 100, 'temperature', 0.2),
failOnError => false
) AS summary
FROM main.docs.articles;ai_query is not available on Classic SQL warehouses. It needs Databricks Runtime 15.4 LTS or above, with 18.2 or above recommended. These arguments decide its behaviour:
| Argument | Behaviour | Watch for |
|---|---|---|
failOnError |
Defaults to true. With false, returns a STRUCT of response and error message |
A model endpoint error nulls the response for that row; any other error still fails the whole query |
modelParameters |
Struct of parameters such as max_tokens and temperature |
Must be constant, not data dependent. temperature defaults to 0.0 here |
responseFormat |
DDL-style or JSON (text, json_object, json_schema) |
Chat foundation models only |
returnType |
Expected type from a custom model | Optional from Databricks Runtime 15.2 when the endpoint has a model schema |
files |
Binary image content for multimodal input | JPEG and PNG only |
| Model type | Requirement |
|---|---|
Databricks-hosted (system.ai.*) |
No endpoint provisioning |
| Provisioned throughput or fine-tuned | A provisioned endpoint; AI Functions doesn’t use its compute and manages batch scaling itself |
| External model | An external model serving endpoint |
| Custom or traditional ML | A custom model serving endpoint |
Requests to system.ai services route through Unity Gateway automatically, but only part of it applies.
Applies to ai_query |
Does not apply to ai_query |
|---|---|
| Usage tracking | Guardrails (service policies) |
Budget integration, including what the ai_query page calls hard spend caps (not an absolute limit; see the chapter “Monitoring and controlling cost in production”) |
Inference tables and tracing tables |
| Permission checks on the model service | Rate limits and fallbacks |
| External model providers; custom model services |
The caller needs EXECUTE on the model service in system.ai. For an endpoint, the function reference says the definer must have CAN QUERY.
Common trap: Relying on Unity Gateway guardrails to screen a batch job - guardrails, inference tables and rate limits are not applied to
ai_queryrequests. Filter and validate in the pipeline instead, and use budgets for spend control.
Common trap: Splitting a large dataset into small batches to protect the service - AI Functions automatically handle parallelisation, retries and scaling, and manual splitting can reduce throughput. Submit the full dataset in one query and set
failOnErrortofalseso that failures come back row by row.
Batch pipelines can be deployed as Lakeflow pipelines, scheduled jobs, or Structured Streaming for near-real-time or micro-batch work. A useful sequence is ingest, infer, validate, then transform. The documented pipeline checks model output with data quality expectations before later steps use it, which matters when no person reads each row.
CREATE OR REFRESH MATERIALIZED VIEW doc_tags AS
SELECT
inputs,
ai_query(
"system.ai.meta-llama-3-3-70b-instruct",
"Extract the category: " || inputs,
responseFormat => 'STRUCT<result:STRUCT<category:STRING>>'
) AS meta_data
FROM docs_raw;Select a model from the batch-optimised list in the ai_query documentation, because a non-optimised model can reduce throughput and lengthen jobs. For batch, Databricks recommends system.ai services instead of provisioned throughput endpoints.
This note is one section of Ultra Transcenders DBX-GENAI: Databricks Generative AI Engineer Associate, an independent study guide that explains every topic the exam covers by technology, with comparison tables, diagrams and the common traps, plus a glossary linked to the Databricks documentation.
Due on Amazon in December 2026, in Kindle and paperback editions.
About the book · DBX-GENAI terms in the glossary · All DBX-GENAI study notes
Which chunking method suits which kind of document.
What decides between the two endpoint types for an index.
What a reranker adds to retrieval, and what it costs.
How to choose a framework for a generative AI application.
What signatures and input examples do when logging a model.
The built-in judges that need expected answers, and those that don't.
How the evaluation and monitoring phases of a generative AI application differ.