Category: General-Purpose AI Function - Table-Valued Function (TVF)
Description
Table-valued function that generates text using remote models deployed to Vertex AI. Supports Gemini models, partner models (Anthropic Claude, Mistral, Llama), and open models. Unlike scalar functions, this returns a table with multiple columns including the generated text and metadata.
Use Cases
Content Generation: Create product descriptions, summaries, or marketing copy
Translation: Translate text between languages
Summarization: Generate summaries of long documents
Question Answering: Answer questions about your data
Code Generation: Generate SQL queries or code snippets
Syntax
AI.GENERATE_TEXT(
MODEL remote_model_name,
prompt_column_or_literal,[, STRUCT(param => value, ...)])
AI.GENERATE_TEXT(
MODEL remote_model_name,
prompt_column_or_literal,[, STRUCT(param => value, ...)])
AI.GENERATE_TEXT(
MODEL remote_model_name,
prompt_column_or_literal,[, STRUCT(param => value, ...)])
Parameters
MODEL: Remote model created with CREATE REMOTE MODEL
prompt: Column name or string literal containing the prompt
STRUCT (optional): Model parameters like temperature, max_output_tokens, top_p, etc.
Code Examples
Example 1: Create Remote Model and Generate Text
-- Step 1: Create remote modelCREATEOR REPLACE MODEL my_dataset.gemini_model
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-2.0-flash-exp');
-- Step 2: Generate textSELECT
product_id,
product_name,
ml_generate_text_llm_result AS generated_description
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,
CONCAT('Write a compelling product description for: ', product_name,'. Features: ', features),
STRUCT(0.7AS temperature,256AS max_output_tokens
)),
products
WHERE description ISNULL
-- Step 1: Create remote modelCREATEOR REPLACE MODEL my_dataset.gemini_model
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-2.0-flash-exp');
-- Step 2: Generate textSELECT
product_id,
product_name,
ml_generate_text_llm_result AS generated_description
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,
CONCAT('Write a compelling product description for: ', product_name,'. Features: ', features),
STRUCT(0.7AS temperature,256AS max_output_tokens
)),
products
WHERE description ISNULL
-- Step 1: Create remote modelCREATEOR REPLACE MODEL my_dataset.gemini_model
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-2.0-flash-exp');
-- Step 2: Generate textSELECT
product_id,
product_name,
ml_generate_text_llm_result AS generated_description
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,
CONCAT('Write a compelling product description for: ', product_name,'. Features: ', features),
STRUCT(0.7AS temperature,256AS max_output_tokens
)),
products
WHERE description ISNULL
Example 2: Analyze Images from Object Table
-- Create object tableCREATEOR REPLACE EXTERNALTABLE product_images.photos
WITHCONNECTION us.my_vertex_connection
OPTIONS (
object_metadata = 'SIMPLE',
uris = ['gs://my-bucket/products/*.jpg']);
-- Analyze imagesSELECT
signed_url,
ml_generate_text_llm_result AS image_description
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,'Describe this product image in detail, focusing on key features and colors.',
STRUCT(0.5AS temperature)),
EXTERNAL_OBJECT_TRANSFORM(TABLE product_images.photos,['SIGNED_URL'])
-- Create object tableCREATEOR REPLACE EXTERNALTABLE product_images.photos
WITHCONNECTION us.my_vertex_connection
OPTIONS (
object_metadata = 'SIMPLE',
uris = ['gs://my-bucket/products/*.jpg']);
-- Analyze imagesSELECT
signed_url,
ml_generate_text_llm_result AS image_description
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,'Describe this product image in detail, focusing on key features and colors.',
STRUCT(0.5AS temperature)),
EXTERNAL_OBJECT_TRANSFORM(TABLE product_images.photos,['SIGNED_URL'])
-- Create object tableCREATEOR REPLACE EXTERNALTABLE product_images.photos
WITHCONNECTION us.my_vertex_connection
OPTIONS (
object_metadata = 'SIMPLE',
uris = ['gs://my-bucket/products/*.jpg']);
-- Analyze imagesSELECT
signed_url,
ml_generate_text_llm_result AS image_description
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,'Describe this product image in detail, focusing on key features and colors.',
STRUCT(0.5AS temperature)),
EXTERNAL_OBJECT_TRANSFORM(TABLE product_images.photos,['SIGNED_URL'])
Example 3: Batch Question Answering
SELECT
question_id,
question_text,
ml_generate_text_llm_result AS answer
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,
CONCAT('Answer this question based on the context: ', question_text,'. Context: ', context_text)),
customer_questions
WHERE answered = FALSE
SELECT
question_id,
question_text,
ml_generate_text_llm_result AS answer
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,
CONCAT('Answer this question based on the context: ', question_text,'. Context: ', context_text)),
customer_questions
WHERE answered = FALSE
SELECT
question_id,
question_text,
ml_generate_text_llm_result AS answer
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_model,
CONCAT('Answer this question based on the context: ', question_text,'. Context: ', context_text)),
customer_questions
WHERE answered = FALSE
Example 4: Use Grounding for Factual Accuracy
-- Create model with groundingCREATEOR REPLACE MODEL my_dataset.gemini_grounded
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-1.5-pro',
grounding = STRUCT('google_search_retrieval'AS sources
));
-- Generate grounded responsesSELECT
query,
ml_generate_text_llm_result AS grounded_response
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_grounded,
query
),
-- Create model with groundingCREATEOR REPLACE MODEL my_dataset.gemini_grounded
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-1.5-pro',
grounding = STRUCT('google_search_retrieval'AS sources
));
-- Generate grounded responsesSELECT
query,
ml_generate_text_llm_result AS grounded_response
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_grounded,
query
),
-- Create model with groundingCREATEOR REPLACE MODEL my_dataset.gemini_grounded
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-1.5-pro',
grounding = STRUCT('google_search_retrieval'AS sources
));
-- Generate grounded responsesSELECT
query,
ml_generate_text_llm_result AS grounded_response
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.gemini_grounded,
query
),
Example 5: Supervised Tuning
-- Create tuned modelCREATEOR REPLACE MODEL my_dataset.tuned_gemini
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-1.5-flash',
supervised_tuning = STRUCT('bq://my_project.my_dataset.training_data'AS training_dataset_uri,5AS epochs
));
-- Use tuned modelSELECT
input_text,
ml_generate_text_llm_result AS custom_output
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.tuned_gemini,
input_text
),
-- Create tuned modelCREATEOR REPLACE MODEL my_dataset.tuned_gemini
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-1.5-flash',
supervised_tuning = STRUCT('bq://my_project.my_dataset.training_data'AS training_dataset_uri,5AS epochs
));
-- Use tuned modelSELECT
input_text,
ml_generate_text_llm_result AS custom_output
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.tuned_gemini,
input_text
),
-- Create tuned modelCREATEOR REPLACE MODEL my_dataset.tuned_gemini
REMOTE WITHCONNECTION `us.my_vertex_connection`
OPTIONS (
endpoint = 'gemini-1.5-flash',
supervised_tuning = STRUCT('bq://my_project.my_dataset.training_data'AS training_dataset_uri,5AS epochs
));
-- Use tuned modelSELECT
input_text,
ml_generate_text_llm_result AS custom_output
FROM
AI.GENERATE_TEXT(
MODEL my_dataset.tuned_gemini,
input_text
),
Data Output Examples
Product Descriptions
product_name
generated_description
"Wireless Earbuds Pro"
"Experience premium sound quality with these cutting-edge wireless earbuds. Featuring active noise cancellation, 24-hour battery life, and crystal-clear call quality, they're perfect for music lovers and professionals alike."
"Smart Home Hub"
"Control your entire smart home from one central device. This intuitive hub supports 100+ devices and features voice control, scheduling, and energy monitoring for the ultimate convenience."
Image Descriptions
image_url
image_description
"gs://products/img001.jpg"
"A sleek black laptop with a thin aluminum body, shown open at a 45-degree angle on a white desk. The screen displays a vibrant blue interface. A wireless mouse is visible to the right."
Best Practices
Choose the right model: Gemini for multimodal, Claude for reasoning, Llama for open source
Use grounding for facts: Enable grounding when factual accuracy is critical
Tune for specific tasks: Use supervised tuning for domain-specific applications
Control generation: Adjust temperature (lower for factual, higher for creative)
Batch processing: Process multiple rows efficiently in a single query
Monitor costs: Track Vertex AI API usage
Supported Models
Gemini Models:
gemini-2.0-flash-exp
gemini-1.5-pro
gemini-1.5-flash
Partner Models:
Anthropic Claude (claude-3-sonnet, claude-3-opus)
Mistral AI (mistral-large, mistral-small)
Llama (llama-3-70b, llama-3-8b)
Open Models:
Gemma
And other supported open models
When to Use
✅ Use for text generation from remote models
✅ Use when you need Gemini model features (grounding, safety, multimodal)
✅ Use for batch text processing
✅ Use when you want to leverage partner or open models
Alternatives
AI.GENERATE: For inline inference with structured output
AI.GENERATE_TEXT (Vertex AI endpoint): Direct endpoint calls
Partner APIs: Direct API calls outside BigQuery
Platform Support
Regions: Model-dependent, see Vertex AI documentation
Preview Status: Generally Available (GA) for Gemini models
Cost: Charged per Vertex AI API call based on model pricing
Provisioned Throughput: Available for supported Gemini models
*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.
*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.
*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.