Handle quota errors by calling ML.GENERATE_TEXT iteratively
This tutorial shows you how to use the BigQuery
bqutil.procedure.bqml_generate_text public stored procedure to iterate
through calls to the
ML.GENERATE_TEXT function.
Calling the function iteratively lets you address any retryable errors that occur
due to exceeding the
quotas and limits that apply to
the function.
To review the source code for the bqutil.procedure.bqml_generate_text
stored procedure in GitHub, see
bqml_generate_text.sqlx.
For more information about the stored procedure parameters and usage, see the
README file.
This tutorial guides you through the following tasks:
- Creating a
remote model over a
gemini-2.0-flashmodel. - Iterating through calls to the
ML.GENERATE_TEXTfunction, using the remote model and thebigquery-public-data.bbc_news.fulltextpublic data table with thebqutil.procedure.bqml_generate_textstored procedure.
Required permissions
To run this tutorial, you need the following Identity and Access Management (IAM) roles:
- Create and use BigQuery datasets, connections, and models:
BigQuery Admin (
roles/bigquery.admin). - Grant permissions to the connection's service account: Project IAM Admin
(
roles/resourcemanager.projectIamAdmin).
These predefined roles contain the permissions required to perform the tasks in this document. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
- 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.getIamPolicyandresourcemanager.projects.setIamPolicy - Create a model and run inference:
bigquery.jobs.createbigquery.models.createbigquery.models.getDatabigquery.models.updateDatabigquery.models.updateMetadata
You might also be able to get these permissions with custom roles or other predefined roles.
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.
- Vertex AI: You incur costs for calls to the Vertex AI model.
To generate a cost estimate based on your projected usage,
use the pricing calculator.
For more information about BigQuery pricing, see BigQuery pricing.
For more information about Vertex AI pricing, see Vertex AI 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
(
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 Vertex AI APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
Create a dataset
Create a BigQuery dataset to store your models and sample data:
In the Google Cloud console, go to the BigQuery page.
In the Explorer pane, click your project name.
Click View actions > Create dataset.
On the Create dataset page, do the following:
For Dataset ID, enter
sample.For Location type, select Multi-region, and then select US (multiple regions in United States).
Leave the remaining default settings as they are, and click Create dataset.
Create the text generation model
Create a remote model that represents a hosted Vertex AI
gemini-2.0-flash model:
In the Google Cloud console, go to the BigQuery page.
In the query editor, run the following statement:
CREATE OR REPLACE MODEL `sample.generate_text` REMOTE WITH CONNECTION DEFAULT OPTIONS (ENDPOINT = 'gemini-2.0-flash');
The query takes several seconds to complete, after which the
generate_textmodel appears in thesampledataset in the Explorer pane. Because the query uses aCREATE MODELstatement to create a model, there are no query results.
Run the stored procedure
Run the bqutil.procedure.bqml_generate_text stored procedure,
which iterates through calls to the ML.GENERATE_TEXT function
using the sample.generate_text model and the
bigquery-public-data.bbc_news.fulltext public data 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.sample.news_generated_text", -- destination table "PROJECT_ID.sample.generate_text", -- model "body", -- content column ["filename"], -- key columns '{}' -- optional arguments );
Replace
PROJECT_IDwith the project ID of the project you are using for this tutorial.The stored procedure creates a
sample.news_generated_texttable to contain the output of theML.GENERATE_TEXTfunction.When the query is finished running, confirm that there are no rows in the
sample.news_generated_texttable that contain a retryable error. In the query editor, run the following statement:SELECT * FROM `sample.news_generated_text` WHERE ml_generate_text_status LIKE '%A retryable error occurred%';
The query returns the message
No data to display.
Clean up
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.