The AI.GENERATE_TABLE function

This document describes theAI.GENERATE_TABLE function, which lets you performgenerative natural language tasks by using any combination of text andunstructured data from BigQuerystandard tables, and alsospecify a schema to format the response from the model.

The function works by sending requests to a BigQuery ML remote modelthat represents a Vertex AI Gemini model, and then returningthat model's response. The function supports remote models over any of thegenerally availableorpreviewGemini models.

Several of theAI.GENERATE_TABLE function's arguments provide theparameters that shape the Vertex AI model's response.

You can use theAI.GENERATE_TABLE function to perform tasks such asclassification, sentiment analysis, image captioning, and transcription.

Prompt design can strongly affect the responses returned by theVertex AI model. For more information, seeIntroduction to prompting.

Input

Using theAI.GENERATE_TABLE function, you can use the following typesof input:

When you analyze unstructured data, that data must meet the followingrequirements:

  • Content must be in one of the supported formats that aredescribed in the Gemini API modelmimeType parameter.
  • If you are analyzing a video, the maximum supported length is two minutes.If the video is longer than two minutes,AI.GENERATE_TABLE only returnsresults for the first two minutes.

Syntax

AI.GENERATE_TABLE(MODEL `PROJECT_ID.DATASET.MODEL`,{ TABLE `PROJECT_ID.DATASET.TABLE` | (QUERY_STATEMENT) },STRUCT(OUTPUT_SCHEMA AS output_schema  [,MAX_OUTPUT_TOKENS AS max_output_tokens]  [,TOP_P AS top_p]  [,TEMPERATURE AS temperature]  [,STOP_SEQUENCES AS stop_sequences]  [,SAFETY_SETTINGS AS safety_settings]  [,REQUEST_TYPE AS request_type]))

Arguments

AI.GENERATE_TABLE takes the following arguments:

Show additional optional parameters

Examples

The following examples demonstrate how to useAI.GENERATE_TABLE.

Format text input

The following example shows a request thatprovides a SQL schema to format the model's response. It specifies a descriptionin theOPTIONS clause for theweight field to indicate that the resultshould be given in kilograms.

SELECTaddress,age,is_married,name,phone_number,weightFROMAI.GENERATE_TABLE(MODEL`mydataset.gemini_model`,(SELECT'''John Smith is a 20-year old single man living at 1234 NW 45th St, Kirkland WA, 98033.        He has two phone numbers 123-123-1234, and 234-234-2345. He is 200.5 pounds.'''ASprompt),STRUCT('''address STRING, age INT64, is_married BOOL, name STRING, phone_number ARRAY<STRING>,            weight FLOAT64 OPTIONS(description = "in kilograms")'''ASoutput_schema,8192ASmax_output_tokens));

The results look similar to the following:

+-------------------------------------+-----+------------+------------+---------------+-----------+| address                             | age | is_married | name       | phone_number  | weight    |+-------------------------------------+-----+------------+------------+---------------+-----------+| 1234 NW 45th St, Kirkland WA, 98033 | 20  | No         | John Smith | 123-123-1234  | 90.947236 ||                                     |     |            |            | 234-234-2345  |           ||                                     |     |            |            |               |           |+-------------------------------------+-----+------------+------------+---------------+-----------+

Create a column based on image data

The following example shows how to to create and populate animage_description column by analyzing a product image that is stored asanObjectRef value in a standard table:

CREATEORREPLACETABLE`mydataset.products`ASSELECTproduct_id,product_name,image,image_descriptionFROMAI.GENERATE_TABLE(MODEL`mydataset.gemini`,(SELECT('Can you describe the following image?',OBJ.GET_ACCESS_URL(image,'r'))ASprompt,*FROM`mydataset.products`),STRUCT("image_description STRING"ASoutput_schema));

Details

The model and input table must be in the same region.

Output

AI.GENERATE_TABLE returns the following columns:

  • All columns in the input table.

  • All columns specified in theoutput_response argument.

  • full_response: this is theJSON responsefrom theprojects.locations.endpoints.generateContentcall to the model. The generated data is in thetext element.

  • status: aSTRING value that contains the API responsestatus for the corresponding row. This value is empty if the operation wassuccessful.

Locations

AI.GENERATE_TABLE must run in the sameregion or multi-region as the remote model that thefunction references.

You can create remote models over Gemini models in thesupported regionsfor the given Gemini model, and also in theUS andEU multi-regions.

Quotas

SeeVertex AI and Cloud AI service functions quotas and limits.

Known issues

This section contains information about known issues.

Resource exhausted errors

Sometimes after a query job that uses this function finishes successfully,some returned rows contain the following error message:

Aretryableerroroccurred:RESOURCEEXHAUSTEDerrorfrom<remoteendpoint>

This issue occurs because BigQuery query jobs finish successfullyeven if the function fails for some of the rows. The function fails when thevolume of API calls to the remote endpoint exceeds the quota limits for thatservice. This issue occurs most often when you are running multiple parallelbatch queries. BigQuery retries these calls, but if the retriesfail, theresource exhausted error message is returned.

To iterate through inference calls until all rows are successfully processed,you can use theBigQuery remote inference SQL scriptsor theBigQuery remote inference pipeline Dataform package.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.