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 a
text-embedding-005model. - Iterating through calls to the
ML.GENERATE_EMBEDDINGfunction, using theremote model and thebigquery-public-data.bbc_news.fulltextpublic datatable with thebqutil.procedure.bqml_generate_embeddingsstored 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.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 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.
For more information about BigQuery pricing, seeBigQuery pricing.
For more information about Vertex AI pricing, seeVertex AI pricing.
Before you begin
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
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.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 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 theBigQuery page.
In theExplorer pane, click your project name.
ClickView actions> Create dataset.
On theCreate dataset page, do the following:
ForDataset ID, enter
target_dataset.ForLocation type, selectMulti-region, and then selectUS (multiple regions in United States).
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:
In the Google Cloud console, go to theBigQuery page.
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 the
embeddingmodel appears in thesampledataset in theExplorer pane.Because the query uses aCREATE MODELstatement 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:
In the Google Cloud console, go to theBigQuery page.
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);
Replace
PROJECT_IDwith the project ID of theproject you are using for this tutorial.The stored procedure creates a
target_dataset.news_body_embeddingstableto contain the output of theML.GENERATE_EMBEDDINGfunction.When the query is finished running, confirm that there are no rowsin the
target_dataset.news_body_embeddingstable 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 message
No data to display.
Clean up
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.