Handle quota errors by calling ML.GENERATE_EMBEDDING iteratively

This tutorial shows you how to use the BigQuerybqutil.procedure.bqml_generate_embeddings public stored procedure to iteratethrough calls to theML.GENERATE_EMBEDDING function.Calling the function iteratively lets you address any retryable errors thatoccur due to exceeding thequotas and limits that apply tothe function.

To review the source code for thebqutil.procedure.bqml_generate_embeddingsstored procedure in GitHub, seebqml_generate_embeddings.sqlx.For more information about the stored procedure parameters and usage, see theREADME file.

This tutorial guides you through the following tasks:

  • Creating aremote model over atext-embedding-005 model.
  • Iterating through calls to theML.GENERATE_EMBEDDING function, using theremote model and thebigquery-public-data.bbc_news.fulltext public datatable with thebqutil.procedure.bqml_generate_embeddings stored 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 inthis document. To see the exact permissions that are required, expand theRequired 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.getIamPolicy andresourcemanager.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 withcustom roles or otherpredefined 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 thepricing calculator.

New Google Cloud users might be eligible for afree trial.

For more information about BigQuery pricing, seeBigQuery pricing.

For more information about Vertex AI pricing, seeVertex AI 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 theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

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

  3. 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.enable permission.Learn how to grant roles.

    Enable the APIs

Create a dataset

Create a BigQuery dataset to store your models and sample data:

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

    Go to theBigQuery page

  2. In theExplorer pane, click your project name.

  3. ClickView actions> Create dataset.

  4. On theCreate dataset page, do the following:

    1. ForDataset ID, entertarget_dataset.

    2. ForLocation type, selectMulti-region, and then selectUS (multiple regions in United States).

    3. Leave the remaining default settings as they are, and clickCreate dataset.

Create the text embedding generation model

Create a remote model that represents a hosted Vertex AItext-embedding-005 model:

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

    Go to BigQuery

  2. In the query editor, run the following statement:

    CREATEORREPLACEMODEL`target_dataset.embedding_model`REMOTEWITHCONNECTIONDEFAULTOPTIONS(ENDPOINT='text-embedding-005');

    The query takes several seconds to complete, after which theembedding model appears in thesample dataset in theExplorer pane.Because the query uses aCREATE MODEL statement to create a model, thereare no query results.

Run the stored procedure

Run thebqutil.procedure.bqml_generate_embeddings stored procedure, whichiterates through calls to theML.GENERATE_EMBEDDING functionusing thetarget_dataset.embedding_model model and thebigquery-public-data.bbc_news.fulltext public data table:

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

    Go to BigQuery

  2. In the query editor, run the following statement:

    CALL`bqutil.procedure.bqml_generate_embeddings`("bigquery-public-data.bbc_news.fulltext",-- source table"PROJECT_ID.target_dataset.news_body_embeddings",-- destination table"PROJECT_ID.target_dataset.embedding_model",-- model"body",-- content column["filename"],-- key columns'{}'-- optional arguments encoded as a JSON string);

    ReplacePROJECT_ID with the project ID of theproject you are using for this tutorial.

    The stored procedure creates atarget_dataset.news_body_embeddings tableto contain the output of theML.GENERATE_EMBEDDING function.

  3. When the query is finished running, confirm that there are no rowsin thetarget_dataset.news_body_embeddings table that contain a retryableerror. In the query editor, run the following statement:

    SELECT*FROM`target_dataset.news_body_embeddings`WHEREml_generate_embedding_statusLIKE'%A retryable error occurred%';

    The query returns the messageNo data to display.

Clean up

    Caution: Deleting a project has the following effects:
    • Everything in the project is deleted. If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
    • Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as anappspot.com URL, delete selected resources inside the project instead of deleting the whole project.

    If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects can help you avoid exceeding project quota limits.

  1. In the Google Cloud console, go to theManage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then clickDelete.
  3. In the dialog, type the project ID, and then clickShut down to delete the project.

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.