The BigQuery bqutil.procedure.bqml_generate_text public stored
procedure lets you iterate through batch calls to the
ML.GENERATE_TEXT function
so you can process large datasets without exceeding endpoint
quotas and limits. Because
BigQuery query jobs finish even when the function fails on
individual rows, high request volumes can leave RESOURCE EXHAUSTED retryable
errors in your output table. In this tutorial, you create a remote model and run
the stored procedure to iteratively retry failed rows until all records in a
public news dataset are processed.
To review the source code for the stored procedure in GitHub, see
bqml_generate_text.sqlx.
For more information about the stored procedure parameters and usage, see the
README file.
Objectives
- Create a BigQuery dataset.
- Create a BigQuery ML
remote model
over a
gemini-3.5-flash-litemodel. - Run the
bqutil.procedure.bqml_generate_textstored procedure to iteratively generate text from thebigquery-public-data.bbc_news.fulltextpublic dataset. - Verify the generated text results and confirm that no retryable quota errors remain.
Costs
In this document, you use the following billable components of Google Cloud:
- BigQuery ML: You incur costs for the data that you process in BigQuery.
- Gemini Enterprise Agent Platform: You incur costs for calls to the Agent Platform model.
To generate a cost estimate based on your projected usage,
use the pricing calculator.
For more information, see BigQuery pricing and Agent Platform pricing.
Before you begin
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the BigQuery, BigQuery Connection, and Agent Platform API APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
Required roles
To get the permissions that you need to complete this tutorial, ask your administrator to grant you the following IAM roles:
-
Create and use BigQuery datasets, connections, and models:
BigQuery Admin (
roles/bigquery.admin) on your project -
Grant permissions to the connection's service account:
Project IAM Admin (
roles/resourcemanager.projectIamAdmin) on your project
For more information about granting roles, see Manage access to projects, folders, and organizations.
These predefined roles contain the permissions required to complete this tutorial. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to complete this tutorial:
-
Create a dataset:
bigquery.datasets.create -
Create, delegate, and use a connection:
bigquery.connections.* -
Set the default connection:
bigquery.config.* -
Set service account permissions:
-
resourcemanager.projects.getIamPolicy -
resourcemanager.projects.setIamPolicy
-
-
Create a model and run inference:
-
bigquery.jobs.create -
bigquery.models.create -
bigquery.models.getData -
bigquery.models.updateData -
bigquery.models.updateMetadata
-
You might also be able to get these permissions with custom roles or other predefined roles.
Create a dataset
To create a BigQuery dataset, select one of the following options:
Console
In the Google Cloud console, go to the BigQuery page.
In the left pane, click Explorer:

If you don't see the left pane, click Expand left pane to open the pane.
In Explorer, expand your project, and then click Datasets.
On the Datasets page, click Create dataset.
In the Create dataset pane, do the following:
For Dataset ID, enter
bqml_tutorial.For Data location, select US.
Leave the remaining default settings as they are.
Click Create dataset.
bq
To create a new dataset, use the
bq mk --dataset command.
Create a dataset named
bqml_tutorialwith the data location set toUS:bq mk --dataset \ --location=US \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial
Confirm that the dataset was created:
bq ls
API
Call the datasets.insert
method with a defined dataset resource:
{ "datasetReference": { "datasetId": "bqml_tutorial" } }
Create the remote model
Use the
CREATE MODEL statement
to create a remote model that represents a hosted
Agent Platform gemini-3.5-flash-lite model:
In the Google Cloud console, go to the BigQuery page.
In the query editor, run the following statement:
CREATE OR REPLACE MODEL `bqml_tutorial.generate_text` REMOTE WITH CONNECTION DEFAULT OPTIONS (ENDPOINT = 'gemini-3.5-flash-lite');
The query takes several seconds to complete, after which the
generate_textmodel appears in thebqml_tutorialdataset in the Explorer pane. Because the query uses aCREATE MODELstatement to create a model, there are no query results.
Run the stored procedure
Use the
CALL statement
to run the bqutil.procedure.bqml_generate_text stored procedure. This
stored procedure iterates through calls to the
ML.GENERATE_TEXT function
by using the bqml_tutorial.generate_text model and the
bigquery-public-data.bbc_news.fulltext public table:
In the Google Cloud console, go to the BigQuery page.
In the query editor, run the following statement:
CALL `bqutil.procedure.bqml_generate_text`( "bigquery-public-data.bbc_news.fulltext", -- source table "PROJECT_ID.bqml_tutorial.news_generated_text", -- destination table "PROJECT_ID.bqml_tutorial.generate_text", -- model "body", -- prompt column ["filename"], -- key columns '{"flatten_json_output": true}' -- optional arguments );
Replace
PROJECT_IDwith your project ID.The stored procedure creates the
bqml_tutorial.news_generated_texttable to contain the output of theML.GENERATE_TEXTfunction.When the query finishes running, confirm that there are no rows in the
bqml_tutorial.news_generated_texttable that contain a retryable error. In the query editor, run the following statement:SELECT * FROM `bqml_tutorial.news_generated_text` WHERE ml_generate_text_status LIKE '%A retryable error occurred%';
The query returns the message
There is no data to display.In the query editor, run the following statement to inspect a sample of the generated text results:
SELECT filename, prompt, ml_generate_text_llm_result, ml_generate_text_status FROM `bqml_tutorial.news_generated_text` LIMIT 3;
The output is similar to the following:
+------------------------+------------------------------------------+-----------------------------------------------------------+-------------------------+ | filename | prompt | ml_generate_text_llm_result | ml_generate_text_status | +------------------------+------------------------------------------+-----------------------------------------------------------+-------------------------+ | bbc/tech/209.txt | A 20-year-old US man is selling... | It looks like you shared a news article about Andrew... | | | bbc/business/375.txt | South Korea's largest credit card... | It looks like you shared a news article about the 2004... | | | bbc/sport/139.txt | A brave defensive display, led by... | It looks like you have shared a match report from a... | | +------------------------+------------------------------------------+-----------------------------------------------------------+-------------------------+
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete the project
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Delete individual resources
If you want to reuse the project, then delete the resources that you created for this tutorial:
Console
In the Google Cloud console, go to the BigQuery page.
Delete the
bqml_tutorialdataset. Deleting the dataset also deletes the remote model and the output table:In the Explorer pane, expand your project and click Datasets.
In the Datasets list, click the
bqml_tutorialdataset.In the details pane, click Delete.
In the Delete dataset dialog, click Delete.
bq
Delete the bqml_tutorial dataset, the remote model, and the output table:
bq rm --dataset --recursive bqml_tutorial
What's next
- Learn more about the
ML.GENERATE_TEXTfunction and theAI.GENERATE_TEXTfunction. - Learn more about generative AI in BigQuery.
- Learn how to choose a text generation function.