Handle quota errors by calling ML.GENERATE_TEXT iteratively

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-lite model.
  • Run the bqutil.procedure.bqml_generate_text stored procedure to iteratively generate text from the bigquery-public-data.bbc_news.fulltext public 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.

New Google Cloud users might be eligible for a free trial.

For more information, see BigQuery pricing and Agent Platform pricing.

Before you begin

  1. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. 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.enable permission. 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.

    Enable the APIs

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

  1. In the Google Cloud console, go to the BigQuery page.

    Go to BigQuery

  2. In the left pane, click Explorer:

    Highlighted button for the Explorer pane.

    If you don't see the left pane, click Expand left pane to open the pane.

  3. In Explorer, expand your project, and then click Datasets.

  4. On the Datasets page, click Create dataset.

  5. 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.

  6. Click Create dataset.

bq

To create a new dataset, use the bq mk --dataset command.

  1. Create a dataset named bqml_tutorial with the data location set to US:

    bq mk --dataset \
      --location=US \
      --description "BigQuery ML tutorial dataset." \
      bqml_tutorial
  2. 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:

  1. In the Google Cloud console, go to the BigQuery page.

    Go to BigQuery

  2. 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_text model appears in the bqml_tutorial dataset in the Explorer pane. Because the query uses a CREATE MODEL statement 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:

  1. In the Google Cloud console, go to the BigQuery page.

    Go to BigQuery

  2. 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_ID with your project ID.

    The stored procedure creates the bqml_tutorial.news_generated_text table to contain the output of the ML.GENERATE_TEXT function.

  3. When the query finishes running, confirm that there are no rows in the bqml_tutorial.news_generated_text table 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.

  4. 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

  1. In the Google Cloud console, go to the BigQuery page.

    Go to BigQuery

  2. Delete the bqml_tutorial dataset. Deleting the dataset also deletes the remote model and the output table:

    1. In the Explorer pane, expand your project and click Datasets.

    2. In the Datasets list, click the bqml_tutorial dataset.

    3. In the details pane, click Delete.

    4. 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