Skip to main content

Calling Gemini from BigQuery — BigQuery ML & Data AI

[ ABORT TO HUD ]
SEQ. 1
SEQ. 2

Calling Gemini from BigQuery

📊 BigQuery ML & Data AI 15m 300 BASE XP⌨ HANDS-ON LAB

Massive-Scale Generative SQL Analytics

BigQuery ML integrates directly with Vertex AI foundation models through Cloud Resource Connections. By linking a BigQuery connection to a remote Vertex endpoint, SQL analysts can run Gemini inference across millions of tabular rows in parallel with zero custom Python code.

-- 1. Declare remote Gemini model in BigQuery
CREATE OR REPLACE MODEL `retail_dw.gemini_remote_model`
REMOTE WITH CONNECTION `us-central1.vertex-ai-conn`
OPTIONS(ENDPOINT = 'gemini-3.8-flash');

-- 2. Execute parallel sentiment analysis & entity extraction across 500,000 customer reviews
SELECT
  review_id,
  customer_id,
  ml_generate_text_llm_result AS analysis
FROM ML.GENERATE_TEXT(
  MODEL `retail_dw.gemini_remote_model`,
  (
    SELECT
      review_id,
      customer_id,
      CONCAT('Analyze customer sentiment and extract mentioned product defects: ', review_text) AS prompt
    FROM `retail_dw.customer_reviews`
    LIMIT 10000
  ),
  STRUCT(
    0.1 AS temperature,
    256 AS max_output_tokens,
    TRUE AS flatten_json_output
  )
);

BigQuery automatically manages quota, parallelizes partition batches across worker slots, and surfaces structured results ready for Looker dashboards or downstream ETL pipelines.

Streamlined Scalar Inference: AI.GENERATE

Alongside remote models with ML.GENERATE_TEXT, BigQuery supports the streamlined AI.GENERATE function, which queries Vertex AI Gemini endpoints inline over text and multimodal columns using End User Credentials (EUC) without requiring an explicit CREATE MODEL statement:

SELECT
  review_id,
  AI.GENERATE(
    CONCAT('Classify sentiment and summarize key defect: ', review_text),
    endpoint => 'gemini-3.8-flash'
  ) AS review_summary
FROM `retail_dw.customer_reviews`
LIMIT 100;
⌨ HANDS-ON LABRun Gemini over SQL Rows
⭐ +150 XP

Your reviews table has a million rows. Run Gemini across them without leaving BigQuery, using the bq CLI and ML.GENERATE_TEXT.

1Execute a query with the bq CLI that calls ML.GENERATE_TEXT over your remote Gemini model.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 1 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 2
How can you run the Gemini model over millions of rows in BigQuery?
Export the data to Python and loop over it
Use ML.GENERATE_TEXT or AI.GENERATE directly in a BigQuery SQL query
It is not possible
Use a Cloud Function to trigger the API