The AI.GENERATE_DOUBLE function
This document describes the AI.GENERATE_DOUBLE function, which lets you
analyze any combination of text and unstructured data
from BigQuery
standard tables. For each row
in the table, the function generates a STRUCT that contains a FLOAT64 value.
The function works by sending requests to a Vertex AI Gemini model, and then returning that model's response.
You can use the AI.GENERATE_DOUBLE function to perform tasks such as
classification and sentiment analysis.
Prompt design can strongly affect the responses returned by the model. For more information, see Introduction to prompting.
Input
Using the AI.GENERATE_DOUBLE function, you can use the following types
of input:
- Text data from standard tables.
ObjectRefRuntimevalues that are generated by theOBJ.GET_ACCESS_URLfunction. You can useObjectRefvalues from standard tables as input to theOBJ.GET_ACCESS_URLfunction. (Preview)
When you analyze unstructured data, that data must meet the following requirements:
- Content must be in one of the supported formats that are
described in the Gemini API model
mimeTypeparameter. - If you are analyzing a video, the maximum supported length is two minutes.
If the video is longer than two minutes,
AI.GENERATE_DOUBLEonly returns results based on the first two minutes.
To minimize Vertex AI charges when you use AI.GENERATE_DOUBLE
in a query, use a table rather than a subquery in the FROM clause. For
example, the first of the following examples is preferable to the second one:
SELECT city, AI.GENERATE_DOUBLE( ('What is the population of ', city, ' in millions?'), connection_id => 'us.test_connection', endpoint => 'gemini-2.0-flash').result FROM mydataset.cities;
SELECT city, AI.GENERATE_DOUBLE( ('What is the population of ', city, ' in millions?'), connection_id => 'us.test_connection', endpoint => 'gemini-2.0-flash').result FROM (SELECT city_name from mydataset.customers WHERE...);
Writing the query results to a table beforehand helps you to ensure that you are sending as few rows as possible to the model.
Syntax
AI.GENERATE_DOUBLE( [ prompt => ] 'PROMPT', connection_id => 'CONNECTION' [, endpoint => 'ENDPOINT'] [, model_params => MODEL_PARAMS] )
Arguments
AI.GENERATE_DOUBLE takes the following arguments:
PROMPT: aSTRINGorSTRUCTvalue that specifies the prompt to send to the model. The prompt must be the first argument that you specify. You can provide the prompt value in the following ways:- Specify a
STRINGvalue. For example,('What is the square root of 10?'). Specify a
STRUCTvalue that contains one or more fields. You can use the following types of fields within theSTRUCTvalue:Field type Description Examples STRINGA string literal, or the name of a STRINGcolumn.String literal: 'What is the square root of 10?'
String column name:my_string_columnARRAY<STRING>You can only use string literals in the array. Array of string literals: ['What is ', 'the square root', ' of 10?']ObjectRefRuntimeAn
ObjectRefRuntimevalue returned by theOBJ.GET_ACCESS_URLfunction. TheOBJ.GET_ACCESS_URLfunction takes anObjectRefvalue as input, which you can provide by either specifying the name of a column that containsObjectRefvalues, or by constructing anObjectRefvalue.ObjectRefRuntimevalues must have theaccess_url.read_urlanddetails.gcs_metadata.content_typeelements of the JSON value populated.Function call with ObjectRefcolumn:OBJ.GET_ACCESS_URL(my_objectref_column, 'r')
Function call with constructedObjectRefvalue:OBJ.GET_ACCESS_URL(OBJ.MAKE_REF('gs://image.jpg', 'myconnection'), 'r')ARRAY<ObjectRefRuntime>ObjectRefRuntimevalues returned from multiple calls to theOBJ.GET_ACCESS_URLfunction. TheOBJ.GET_ACCESS_URLfunction takes anObjectRefvalue as input, which you can provide by either specifying the name of a column that containsObjectRefvalues, or by constructing anObjectRefvalue.ObjectRefRuntimevalues must have theaccess_url.read_urlanddetails.gcs_metadata.content_typeelements of the JSON value populated.Function calls with ObjectRefcolumns:[OBJ.GET_ACCESS_URL(my_objectref_column1, 'r'), OBJ.GET_ACCESS_URL(my_objectref_column2, 'r')]
Function calls with constructedObjectRefvalues:[OBJ.GET_ACCESS_URL(OBJ.MAKE_REF('gs://image1.jpg', 'myconnection'), 'r'), OBJ.GET_ACCESS_URL(OBJ.MAKE_REF('gs://image2.jpg', 'myconnection'), 'r')]The function combines
STRUCTfields similarly to aCONCAToperation and concatenates the fields in their specified order. The same is true for the elements of any arrays used within the struct. The following table shows some examples ofSTRUCTprompt values and how they are interpreted:Struct field types Struct value Semantic equivalent STRUCT<STRING>('What is the population of Seattle in millions?')'What is the population of Seattle in millions?' STRUCT<STRING, STRING, STRING>('What is the population of ', my_city_column, ' in millions?')'What is the population of my_city_column_value in millions?' STRUCT<STRING, ARRAY<STRING>>('What is the population of ', ['Seattle', ' in millions?'])'What is the population of Seattle in millions?' STRUCT<STRING, ObjectRefRuntime>('What is the population in millions of the city shown in the following image?', OBJ.GET_ACCESS_URL(image_objectref_column, 'r'))'What is the population in millions of the city shown in the following image?' image STRUCT<STRING, ObjectRefRuntime, ObjectRefRuntime>('If the city in the first image is within the country of the second image, provide the city's population in millions',
OBJ.GET_ACCESS_URL(city_image_objectref_column, 'r'),
OBJ.GET_ACCESS_URL(country_image_objectref_column, 'r'))'If the city in the first image is within the country of the second image, provide the city's population in millions' city_image country_image
- Specify a
CONNECTION: aSTRINGvalue specifying the connection to use to communicate with the model, in the format[PROJECT_ID].[LOCATION].[CONNECTION_ID]. For example,myproject.us.myconnection.Replace the following:
PROJECT_ID: the project ID of the project that contains the connection.LOCATION: the location used by the connection. The connection must be in the same location as the dataset that contains the model.CONNECTION_ID: the connection ID—for example,myconnection.You can get this value by viewing the connection details in the Google Cloud console and copying the value in the last section of the fully qualified connection ID that is shown in Connection ID. For example,
projects/myproject/locations/connection_location/connections/myconnection.
ENDPOINT: aSTRINGvalue that specifies the Vertex AI endpoint to use for the model. You can specify any generally available or preview Gemini model. If you specify the model name, BigQuery ML automatically identifies and uses the full endpoint of the model. If you don't specify anENDPOINTvalue, BigQuery ML selects a recent stable version of Gemini to use.MODEL_PARAMS: aJSONliteral that provides additional parameters to the model. TheMODEL_PARAMSvalue must conform to thegenerateContentrequest body format. You can provide a value for any field in the request body except for thecontentsfield; thecontentsfield is populated with thePROMPTargument value.
Output
AI.GENERATE_DOUBLE returns a STRUCT value for each row in the table. The struct
contains the following fields:
result: aDOUBLEvalue containing the model's response to the prompt. The result isNULLif the request fails or is filtered by responsible AI.full_response: a JSON value containing the response from theprojects.locations.endpoints.generateContentcall to the model. The generated text is in thetextelement.status: aSTRINGvalue that contains the API response status for the corresponding row. This value is empty if the operation was successful.
Examples
The following examples demonstrate how to use the
AI.GENERATE_DOUBLE function.
Use string input
Suppose you have the following table called mydataset.cities with a single
city column:
+---------+ | city | +---------+ | Seattle | | Beijing | | Paris | | London | +---------+
To determine the population of each city in millions, you can call the
AI.GENERATE_DOUBLE function and select the result field in the output
by running the following query:
SELECT city, AI.GENERATE_DOUBLE(('What is the population of ', city, ' in millions?'), connection_id => 'us.test_connection', endpoint => 'gemini-2.0-flash').result FROM mydataset.cities;
The result is similar to the following:
+---------+--------+ | city | result | +---------+--------+ | Seattle | 0.753 | | Beijing | 21.89 | | Paris | 2.14 | | London | 9.5 | +---------+--------+
Use ObjectRefRuntime input
Suppose you have the following table called mydataset.elements with a single
STRUCT column that uses the ObjectRef format and contains PDF files that
describe different elements:
+--------------------------------+-----------------+--------------------+---------------------------------------------------------------+
| element.uri | element.version | element.authorizer | element.details |
+--------------------------------+-----------------+--------------------+---------------------------------------------------------------+
| gs://mybucket/cadmium.pdf | 12345678 | us.conn | {"gcs_metadata":{"content_type":"application/pdf","md5_hash"… |
+--------------------------------+-----------------+--------------------+---------------------------------------------------------------+
| gs://mybucket/lead.pdf | 23456789 | us.conn | {"gcs_metadata":{"content_type":"application/pdf","md5_hash"… |
+--------------------------------+-----------------+--------------------+---------------------------------------------------------------+
| gs://mybucket/krypton.pdf | 234567890 | us.conn | {"gcs_metadata":{"content_type":"application/pdf","md5_hash"… |
+--------------------------------+-----------------+--------------------+---------------------------------------------------------------+
To get the atomic weight for each element, call the
AI.GENERATE_DOUBLE function and select the result field in the output
by running the following query:
SELECT AI.GENERATE_DOUBLE(('What is the atomic weight of the element described in ', OBJ.GET_ACCESS_URL(element, 'r') , '?'), connection_id => 'us.test_connection', endpoint => 'gemini-2.0-flash').result FROM mydataset.elements;
The result is similar to the following:
+---------+ | result | +---------+ | 112.41 | | 207.20 | | 83.798 | +---------+
Set the thinking budget for a Gemini 2.5 Flash model
The following query shows how to set the model_params argument to set the
model's thinking budget to 0 for the request:
SELECT city, AI.GENERATE_DOUBLE(('What is the population of ', city, ' in millions?'), connection_id => 'us.test_connection', endpoint => 'gemini-2.5-flash', model_params => JSON '{"generation_config":{"thinking_config": {"thinking_budget": 0}}}') FROM mydataset.cities;
Locations
You can run AI.GENERATE_DOUBLE in all of the
regions
that support Gemini models, and also in the US and EU
multi-regions.
Quotas
See Vertex AI and Cloud AI service functions quotas and limits.
What's next
- For more information about using Vertex AI models to generate text and embeddings, see Generative AI overview.
- For more information about using Cloud AI APIs to perform AI tasks, see AI application overview.
- For more information about supported SQL statements and functions for generative AI models, see End-to-end user journeys for generative AI models.