---
title: ai_query function
description: Learn the syntax of the ai\_query function of the SQL language in Databricks SQL. See examples of querying foundation models and traditional ML models for inference.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/ai_query
---

# `ai_query` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Invokes an existing Databricks [Model Serving endpoint](https://docs.databricks.com/aws/en/machine-learning/model-serving/index.md) and parses and returns its response.

`ai_query` is a general purpose [AI Function](https://docs.databricks.com/aws/en/large-language-models/ai-functions.md) that enables you to query existing endpoints for real-time inference or batch inference workloads.

* See [ Supported models](https://docs.databricks.com/aws/en/large-language-models/ai-query.md#supported-models) for supported models and required endpoint configurations.
* You can also use `ai_query` to query an agent deployed on ML model serving endpoint, see [Query a legacy agent on Model Serving](https://docs.databricks.com/aws/en/agents/query-llms.md#model-serving)
* To use `ai_query` in production workflows, see [Deploy batch inference pipelines](https://docs.databricks.com/aws/en/large-language-models/batch-inference-pipelines.md).

## <a id="requirement" />Requirements

* This function is not available on Databricks SQL Classic.
* Databricks Runtime 15.4 LTS or above is required. For an upcoming change to this requirement, see [AI Functions require Databricks Runtime 17.3 LTS or above](https://docs.databricks.com/aws/en/release-notes/whats-coming.md#ai-functions-runtime-support). Databricks Runtime 18.2 or above is recommended for the best performance and access to the latest features.
* Your workspace must be in a supported [Model Serving region](https://docs.databricks.com/aws/en/machine-learning/model-serving/model-serving-limits.md#regions).
* An existing model serving endpoint with your model loaded. If you are using a Databricks hosted foundation model, an endpoint is created for you. Otherwise, see [Create custom model serving endpoints](https://docs.databricks.com/aws/en/machine-learning/model-serving/create-manage-serving-endpoints.md) or [Create foundation model serving endpoints](https://docs.databricks.com/aws/en/machine-learning/model-serving/create-foundation-model-endpoints.md).
* Querying [custom model](https://docs.databricks.com/aws/en/machine-learning/model-serving/index.md) endpoints and [external model](https://docs.databricks.com/aws/en/machine-learning/foundation-models/external-models/index.md) endpoints is enabled by default in supported regions. If `ai_query()` returns a permission error against a custom or external endpoint, your workspace admin may have disabled it in [Manage previews](https://docs.databricks.com/aws/en/admin/workspace-settings/manage-previews.md).

## Syntax

To query an endpoint that serves a [foundation model](https://docs.databricks.com/aws/en/machine-learning/model-serving/foundation-model-overview.md):

```
ai_query(endpoint, request)
```

To query a custom model serving endpoint with a [model schema](https://docs.databricks.com/aws/en/machine-learning/model-serving/manage-serving-endpoints.md#get-schema):

```
ai_query(endpoint, request)
```

To query a custom model serving endpoint without a model schema:

```
ai_query(endpoint, request, returnType, failOnError)
```

## <a id="returns" /> Arguments and returns

| Argument          | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | Returns                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `endpoint`        | The name of a Databricks Foundation Model serving endpoint, an external model serving endpoint or a custom model endpoint in the same workspace for invocations as a `STRING` literal. The definer must have `CAN QUERY` permission on the endpoint.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `request`         | The request used to invoke the endpoint as an expression.<br /><ul><li>If the endpoint is an external model serving endpoint or Databricks Foundation Model APIs endpoint, the request must be a `STRING`.</li><li>If the endpoint is a custom model serving endpoint, the request can be a single column or a STRUCT expression. The STRUCT field names should match the input feature names expected by the endpoint.</li></ul>                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `returnType`      | (Optional for Databricks Runtime 15.2 and above) The expected `returnType` from the endpoint as an expression. This is similar to the schema parameter in [`from_json` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/from_json.md), which accepts both a `STRING` expression or invocation of [`schema_of_json` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/schema_of_json.md).<br /><ul><li>For Databricks Runtime 15.2 and above, this expression is *optional*. If it is not provided, `ai_query()` automatically infers the return type from the model schema of the *custom model serving endpoint*.</li><li>For Databricks Runtime 15.1 and below, this expression is required for querying a *custom model serving endpoint*.</li></ul><br />Use the `responseFormat` parameter to specify response formats for chat foundation models. |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `failOnError`     | (Optional) A boolean literal that defaults to true. Requires Databricks Runtime 15.3 or above. This flag indicates whether to include an error message in the `ai_query` response.<br />See [Handle errors using `failOnError`](https://docs.databricks.com/aws/en/sql/language-manual/functions/ai_query.md#errors) for an example.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | The following describes the return behavior based on the `failOnError` scenario:<br /><ul><li>If `failOnError => true`, the function returns the same result as the existing behavior, which is the parsed response from the endpoint. The data type of the parsed response is inferred from the model type, the model schema endpoint, or the `returnType` parameter in the `ai_query` function.</li><li>If `failOnError => false`, the function returns a `STRUCT` object that contains the parsed response and the error message string.<br /><ul><li>If the inference of the row succeeds, the `errorMessage` field is `null`.</li><li>If the inference of the row fails due to model endpoint errors, the `response` field is `null`.</li><li>If the inference of the row fails due to other errors, the whole query fails.</li></ul></li></ul> |
| `modelParameters` | (Optional) A struct field that contains chat, completion and embedding model parameters for serving foundation models or external models. These model parameters must be constant parameters and not data dependent.<br /><ul><li>Requires Databricks Runtime 15.3 or above.</li><li>When these model parameters are not specified or set to `null` the default value is used. With the exception of `temperature` which has a default value of `0.0`, the default values for these model parameters are the same as those listed in [Foundation model REST API reference](https://docs.databricks.com/aws/en/machine-learning/foundation-model-apis/api-reference.md).</li></ul><br />See [ Configure a model by passing model parameters](https://docs.databricks.com/aws/en/sql/language-manual/functions/ai_query.md#model-params) for an example.                                                     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `responseFormat`  | (Optional) Specify the response format you want the chat foundation model to follow.<br /><ul><li>Requires Databricks Runtime 15.4 LTS or above. For an upcoming change to this requirement, see [AI Functions require Databricks Runtime 17.3 LTS or above](https://docs.databricks.com/aws/en/release-notes/whats-coming.md#ai-functions-runtime-support). Only available for querying chat foundation models.</li><li>Two styles of response format are supported.<br /><ul><li>A DDL style JSON string</li><li>A JSON string. Three JSON string types of response format are supported:`text`, `json_object`, `json_schema`</li></ul></li></ul><br />See [Enforce output schema with structured output](https://docs.databricks.com/aws/en/sql/language-manual/functions/ai_query.md#structured-output) for examples.                                                                                  | The following describes what happens when `failOnError` is also set when `responseFormat` is specified:<br /><ul><li>If `failOnError => false` and you have specified `responseFormat`, the function returns the parsed response and the error message string as a `STRUCT` object.</li><li>Depending on the JSON string type specified in `responseFormat`, the following response is returned:<br /><ul><li>For `responseFormat => '{"type": "text"}'`, the response is a string such as, `“Here is the response”`.</li><li>For `responseFormat => '{"type": "json_object"}'`, the response is a key-value pair JSON string, such as `{“key”: “value”}`.</li><li>For `responseFormat => '{"type": "json_schema", "json_schema"...}'`, the response is a JSON string.</li></ul></li></ul>                                                           |
| `files`           | (Optional) Specify which files and content to use in your multimodal input requests using `files=>content`. `files` is the parameter name expected by the model for multimodal input, and `content` refers to the column in the DataFrame which contains the binary content of the image files you want to use in your query.<br /><ul><li>Required for multimodal requests.</li><li>Only image inputs: `JPEG` or `PNG` are supported.<br />See [ Multimodal inputs](https://docs.databricks.com/aws/en/sql/language-manual/functions/ai_query.md#multimodal) for an example. As seen in this example, you can specify the `content` column of the output of `read_files` to the `files` parameter.</li></ul>                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |

## <a id="foundation-model" /> Example: Query a foundation model

To query an external model serving endpoint:

```sql
> SELECT ai_query(
    'my-external-model-openai-chat',
    'Describe Databricks SQL in 30 words.'
  ) AS summary

  "Databricks SQL is a cloud-based platform for data analytics and machine learning, providing a unified workspace for collaborative data exploration, analysis, and visualization using SQL queries."
```

To query a foundation model supported by Databricks Foundation Model APIs:

```sql
> SELECT *,
  ai_query(
    'system.ai.meta-llama-3-3-70b-instruct',
    "Can you tell me the name of the US state that serves the provided ZIP code? zip code: " || pickup_zip
    )
  FROM samples.nyctaxi.trips
  LIMIT 10
```

Optionally, you can also wrap a call to `ai_query()` in a UDF for function calling as follows:

```sql
 CREATE FUNCTION correct_grammar(text STRING)
  RETURNS STRING
  RETURN ai_query(
    'system.ai.meta-llama-3-3-70b-instruct',
    CONCAT('Correct this to standard English:\n', text));
> GRANT EXECUTE ON correct_grammar TO ds;
- DS fixes grammar issues in a batch.
> SELECT
    * EXCEPT text,
    correct_grammar(text) AS text
  FROM articles;
```

### <a id="multimodal" /> Multimodal inputs

`ai_query` natively supports multimodal image inputs. See [ Foundation model types](https://docs.databricks.com/aws/en/machine-learning/model-serving/score-foundation-models.md#model-types) for the supported Databricks-hosted vision models.

The following are supported input types:

* `JPEG`
* `PNG`

The following example shows how to query a foundation model supported by Databricks Foundation Model APIs for multimodal input. In this example, the `files => content` parameter is used to pass the image file data to `ai_query`

* `files`: The parameter name expected by the model for multimodal input
* `content`: The column in the DataFrame returned by `READ_FILES`, which contains the binary content of the image file.

```sql

> SELECT *, ai_query(
  'system.ai.llama-4-maverick',
 'what is this image about?', files => content)
as output FROM READ_FILES("/Volumes/main/multimodal/unstructured/image.jpeg");

```

To query a foundation model supported by Databricks Foundation Model APIs for multimodal input and specify structured output:

```sql
> SELECT *, ai_query(
  'system.ai.llama-4-maverick', 'What is interesting or important about this image?',
    responseFormat => ‘{
      "type": "json_schema",
        "json_schema": {
          "name": "output",
          "schema": {
            "type": "object",
            "properties": {
              "summary": {"type": "string"},
              "num_people": {"type": "integer"},
              "num_animals": {"type": "integer"},
              "interesting_fact": {"type": "string"},
              "possible_context": {"type": "string"}
            }
        },
        "strict": true
      }
    }’,
    files => content
  )
  as output FROM READ_FILES("/Volumes/main/user/volume1/image.jpeg");

```

The following example uses `ai_query` with `read_files` in `binaryFile` format to run inference on image files from cloud storage. The `WHERE` clause filters on `_metadata` fields to target specific files by size and name.

```sql
SELECT
  path AS file_path,
  ai_query(
    'system.ai.llama-4-maverick',
    'Describe this image in ten words or less: ',
    files => content
  ) AS result
FROM read_files(
  's3://my-s3-bucket/path/to/images/',
  format => 'binaryFile',
  fileNamePattern => '*.{jpg,jpeg,png,JPG,JPEG,PNG}'
)
WHERE _metadata.file_size < 1000000
  AND _metadata.file_name LIKE '%robots%';
```

## Example: Query a traditional ML model

To query a custom model or a traditional ML model serving endpoint:

```sql

> SELECT text, ai_query(
    endpoint => 'spam-classification-endpoint',
    request => named_struct(
      'timestamp', timestamp,
      'sender', from_number,
      'text', text),
    returnType => 'BOOLEAN') AS is_spam
  FROM messages
  LIMIT 10

> SELECT ai_query(
    'weekly-forecast',
    request => struct(*),
    returnType => 'FLOAT') AS predicted_revenue
  FROM retail_revenue

```

## <a id="advanced" /> Examples for advanced scenarios

The following sections provide examples for advanced use cases like error handling or how to incorporate `ai_query` into a user-defined function.

### Pass a messages array

The following example shows how to pass a messages array to your model or agent application using `ai_query`.

```sql
> SELECT ai_query(
    'custom-llama-chat',
    request => named_struct("messages",
        ARRAY(named_struct("role", "user", "content", "What is ML?"))),
    returnType => 'STRUCT<candidates:ARRAY<STRING>>')

  {"candidates":["ML stands for Machine Learning. It's a subfield of Artificial Intelligence that involves the use of algorithms and statistical models to enable machines to learn from data, make decisions, and improve their performance on a specific task over time."]}
```

### <a id="concatenate" /> Concatenate the prompt and inference column

There are multiple ways to concatenate the prompt and the inference column, such as using `||`, `CONCAT()`, or `format_string()`:

```sql
SELECT
CONCAT('${prompt}', ${input_column_name}) AS concatenated_prompt
FROM ${input_table_name};
```

Alternatively:

```sql
SELECT
'${prompt}' || ${input_column_name} AS concatenated_prompt
FROM ${input_table_name};
```

Or using `format_string()`:

```sql
SELECT
format_string('%s%s', '${prompt}', ${input_column_name}) AS concatenated_prompt
FROM ${input_table_name};
```

### <a id="model-params" /> Configure a model by passing model parameters

Customize model behavior by passing specific parameters such as maximum tokens and temperature. For example:

```sql
SELECT text, ai_query(
    "system.ai.meta-llama-3-3-70b-instruct",
    "Please summarize the following article: " || text,
    modelParameters => named_struct('max_tokens', 100, 'temperature', 0.7)
) AS summary
FROM uc_catalog.schema.table;
```

### <a id="errors" />Handle errors using `failOnError`

Use the `failOnError` argument for `ai_query` to handle errors. The following example shows how to make sure that if one row has an error, it won't stop the whole query from running. See [Arguments and returns](https://docs.databricks.com/aws/en/sql/language-manual/functions/ai_query.md#returns) for expected behaviors based on how this argument is set.

```sql

SELECT text, ai_query(
    "system.ai.meta-llama-3-3-70b-instruct",
    "Summarize the given text comprehensively, covering key points and main ideas concisely while retaining relevant details and examples. Ensure clarity and accuracy without unnecessary repetition or omissions: " || text,
failOnError => false
) AS summary
FROM uc_catalog.schema.table;
```

### <a id="structured-output" />Enforce output schema with structured output

Ensure that the output conforms to a specific schema for easier downstream processing using `responseFormat`. See [Structured outputs on Databricks](https://docs.databricks.com/aws/en/machine-learning/model-serving/structured-outputs.md).

The following example enforces a DDL style JSON string schema:

```sql
SELECT ai_query(
    "system.ai.gpt-oss-20b",
    "Extract research paper details from the following abstract: " || abstract,
    responseFormat => 'STRUCT<research_paper_extraction:STRUCT<title:STRING, authors:ARRAY<STRING>, abstract:STRING, keywords:ARRAY<STRING>>>'
)
FROM research_papers;
```

Alternatively, using a JSON schema response format:

```sql
SELECT ai_query(
    "system.ai.gpt-oss-20b",
    "Extract research paper details from the following abstract: " || abstract,
    responseFormat => '{
      "type": "json_schema",
      "json_schema": {
        "name": "research_paper_extraction",
        "schema": {
          "type": "object",
          "properties": {
            "title": {"type": "string"},
            "authors": {"type": "array", "items": {"type": "string"}},
            "abstract": {"type": "string"},
            "keywords": {"type": "array", "items": {"type": "string"}}
          }
      },
      "strict": true
    }
  }'
)
FROM research_papers;
```

An expected output might look like:

```sql
{ "title": "Understanding AI Functions in Databricks", "authors": ["Alice Smith", "Bob Jones"], "abstract": "This paper explains how AI functions can be integrated into data workflows.", "keywords": ["Databricks", "AI", "LLM"] }

```

### Use `ai_query` in user-defined functions

You can wrap a call to `ai_query` in a UDF, making it easy to use functions across different workflows and share them.

```sql
CREATE FUNCTION correct_grammar(text STRING)
  RETURNS STRING
  RETURN ai_query(
    'system.ai.meta-llama-3-3-70b-instruct',
    CONCAT('Correct this to standard English:\n', text));

GRANT EXECUTE ON correct_grammar TO ds;

SELECT
    * EXCEPT text,
    correct_grammar(text) AS text
  FROM articles;

```
