The AI.IF function
Preview
This product or feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. Pre-GA products and features are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.
Note: For support during the preview, contactbqml-feedback@google.com.This document describes theAI.IF function, which uses aVertex AI Gemini model toevaluate a condition described in natural language and returns aBOOL.
You can use theAI.IF function to filter and join data based on conditionsdescribed in natural language or multimodal input.The following are common use cases:
- Sentiment analysis: Find customer reviews with negative sentiment.
- Topic analysis: Identify news articlesrelated to a specific subject.
- Image analysis: Select images thatcontain a specific item.
- Security: Identify suspicious emails.
Input
AI.IF accepts 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 followingrequirements:
- Content must be in one of the supported formats that aredescribed in the Gemini API model
mimeTypeparameter. - For more information about accepted multimodal input, see thetechnical specificationsfor Gemini.
This function passes your input to a Gemini model andincurs charges in Vertex AI each time it's called.For information about how to view these charges, seeTrack costs.
Syntax
AI.IF([prompt=>]'PROMPT',connection_id=>'CONNECTION')
Arguments
AI.IF takes the following arguments:
PROMPT: aSTRINGorSTRUCTvalue that specifiesthe prompt to send tothe 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,('Is Seattle a US city?'). Specify a
STRUCTvalue that contains one or more fields. You can usethe following types of fields within theSTRUCTvalue:Field type Description Examples STRINGA string literal, or the name of a STRINGcolumn.String literal: 'Is Seattle a US city?'
String column name:my_string_columnARRAY<STRING>You can only use string literals in the array. Array of string literals: ['Is ', 'Seattle', ' a US city?']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.Your input can contain at most one video object.
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.Your input can contain at most one video object.
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. Thesame is true for the elements of any arrays used within the struct.The following table shows some examples ofSTRUCTprompt values and howthey are interpreted:Struct field types Struct value Semantic equivalent STRUCT<STRING>('Is Seattle a US city?')'Is Seattle a US city?' STRUCT<STRING, STRING, STRING>('Is the city of ', my_city_column, ' in India?')'Is the city ofmy_city_column_value in India?' STRUCT<STRING, ARRAY<STRING>>('Is the city of ', ['Oslo', ' in India?'])'Is the city of Oslo in India?' STRUCT<STRING, ObjectRefRuntime>('Is the city in the following image in Canada?', OBJ.GET_ACCESS_URL(image_objectref_column, 'r'))'Is the city in the following image in Canada?'image STRUCT<STRING, ObjectRefRuntime, ObjectRefRuntime>('Is the city in the first image within the country of the second image?',
OBJ.GET_ACCESS_URL(city_image_objectref_column, 'r'),
OBJ.GET_ACCESS_URL(country_image_objectref_column, 'r'))'Is the city in the first image within the country of the second image?'city_imagecountry_image
- Specify a
CONNECTION: aSTRINGvalue specifying theCloud resource connectionto use. The following forms are accepted:Connection name:
[PROJECT_ID].LOCATION.CONNECTION_IDFor example,
myproject.us.myconnection.Fully qualified connection ID:
projects/PROJECT_ID/locations/LOCATION/connections/CONNECTION_IDFor example,
projects/myproject/locations/us/connections/myconnection.
Replace the following:
PROJECT_ID: the project ID of the project that contains the connection.LOCATION: thelocation used by the connection.CONNECTION_ID: the connection ID—for example,myconnection.You can get this value byviewing 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 inConnection ID. For example,
projects/myproject/locations/connection_location/connections/myconnection.
Important
You need to grant theVertex AI User role to the connection's service account. For more information, seeGrant or revoke a single IAM role.
Output
AI.IF returns aBOOL based on evaluation of the condition in theinput prompt.
If the call to Vertex AI is unsuccessful for any reason,such as exceeding quota or model unavailability, then the function returnsNULL.
Examples
The following examples show how to use theAI.IF function to filter textand join multimodal data.
Filter text by topic
The following query uses theAI.IF function to filter newsstories to those that cover a natural disaster:
SELECTtitle,bodyFROM`bigquery-public-data.bbc_news.fulltext`WHEREAI.IF(('The following news story is about a natural disaster: ',body),connection_id=>'us.example_connection');Combine filters
BigQuery optimizes queries to reduce the number of calls toGemini. The following query first filters by theequality operator, and then filters using theAI.IF function:
SELECTtitle,bodyFROM`bigquery-public-data.bbc_news.fulltext`WHEREAI.IF(('This news is related to Google: ',body),connection_id=>'us.example_connection')ANDcategory='tech';-- Non-AI filters are evaluated firstFilter images
The following query creates an external table from images of pet productsstored in a publicly available Cloud Storage bucket. Then, it filters theresults to images that contain a ball.
-- Create a datasetCREATESCHEMAIFNOTEXISTScymbal_pets;-- Create an object tableCREATEORREPLACEEXTERNALTABLEcymbal_pets.product_imagesWITHCONNECTIONus.example_connectionOPTIONS(object_metadata='SIMPLE',uris=['gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/images/*.png']);-- Filter images in the object tableSELECTSTRING(OBJ.GET_ACCESS_URL(ref,'r').access_urls.read_url)ASsigned_url,FROM`cymbal_pets.product_images`WHEREAI.IF(('The image contains a ball.',OBJ.GET_ACCESS_URL(ref,'r')),connection_id=>'us.example_connection');Join tables based on image content
The following queries create a table of product data and a table of productimages. The tables are joined based on whether the image is of the product.
-- Create a datasetCREATESCHEMAIFNOTEXISTScymbal_pets;-- Load a non-object tableLOADDATAOVERWRITEcymbal_pets.productsFROMFILES(format='avro',uris=['gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/tables/products/products_*.avro']);-- Create an object tableCREATEORREPLACEEXTERNALTABLEcymbal_pets.product_imagesWITHCONNECTIONus.example_connectionOPTIONS(object_metadata='SIMPLE',uris=['gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/images/*.png']);-- Join the standard table and object tableSELECTproduct_name,brand,signed_urlFROMcymbal_pets.productsINNERJOINEXTERNAL_OBJECT_TRANSFORM(TABLE`cymbal_pets.product_images`,['SIGNED_URL'])asimagesONAI.IF(("""You will be provided an image of a pet product. Determine if the image is of the following pet toy:""",products.product_name,images.ref),connection_id=>'us.example_connection')WHEREproducts.category="Toys"ANDproducts.brand="Fluffy Buns";Filter audio by speech topic
The following queries create a table of audio data stored in a publiclyavailable Cloud Storage bucket. The query filters the audio samples to thosethat contain speech discussing a large language model.
-- Create a datasetCREATESCHEMAIFNOTEXISTSaudio_repo;-- Create an object table with audiosCREATEORREPLACEEXTERNALTABLEaudio_repo.prompt_audioWITHCONNECTIONus.test_connectionOPTIONS(object_metadata='SIMPLE',uris=['gs://cloud-samples-data/generative-ai/audio/*.mp3']);-- Filter audios in the object tableSELECTSTRING(OBJ.GET_ACCESS_URL(ref,'r').access_urls.read_url)ASsigned_url,FROM`audio_repo.prompt_audio`WHEREAI.IF(('Does the audio talk about large language models? ',OBJ.GET_ACCESS_URL(ref,'r')),connection_id=>'us.test_connection');Related functions
TheAI.IF andAI.GENERATE_BOOLfunctions both use models to generate aboolean value in response to a prompt. The following differences can help youchoose which function to use:
- Prompt Optimization:
AI.IFautomaticallystructuresyour prompts to improve the quality of the output. - Input:
AI.IFautomatically selects a Gemini model.AI.GENERATE_BOOLlets you specify a specific model endpoint and modelparameters to use. - Output:
AI.IFreturns aBOOLvalue, which makes it easier towork with in queries.AI.GENERATE_BOOLreturns aSTRUCTthat containsaBOOLvalue, as well as additional information about the model call, whichis useful if you need to view details such as thesafety ratingor API response status. - Error handling: If
AI.IFproduces an error for any input, then thefunction returnsNULL.AI.GENERATE_BOOLrecords details about theerrors in its output.
Locations
You can runAI.IF in all of theregionsthat support Gemini models, and also in theUS andEUmulti-regions.
Quotas
SeeGenerative AI functions quotas and limits.
What's next
- For more information about using Vertex AI models togenerate text and embeddings, seeGenerative AI overview.
- For more information about using Cloud AI APIs to perform AI tasks, seeAI application overview.
- For more information about supported SQL statements and functions forgenerative AI models, seeEnd-to-end user journeys for generative AI models.
- To use this function in a tutorial, seePerform semantic analysis with managed AI functions.
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 2025-12-16 UTC.