2.3 Fine-Tuning & Evaluating Gemini Models from BigQuery

Key Takeaways

  • BigQuery ML tunes Gemini by creating a remote model with an AS SELECT clause that supplies prompt and label columns.
  • For a tuning remote model, the BigQuery connection's service account needs the Vertex AI Service Agent role in the project that creates the model.
  • BigQuery ML converts MAX_ITERATIONS to epochs: the number of input rows equals one epoch, and values round up to the nearest whole epoch.
  • ML.EVALUATE with the TEXT_GENERATION task type returns BLEU4 and ROUGE-L precision, recall, and F1 scores for the model.
  • Supervised tuning in BigQuery is billed as BigQuery bytes processed plus Agent Platform charges for tuning tokens.
Last updated: September 2026

The June 2026 exam guide lists "fine-tuning Gemini models using BigQuery" under low-code AI. The scenario: an organization's examples (support replies, product descriptions, labeled tickets) already sit in BigQuery, and prompting alone doesn't consistently produce the right style or labels. BigQuery ML runs a supervised tuning job on Agent Platform without the team writing Python or moving data to Cloud Storage.

Step 1: Connect BigQuery to Agent Platform

BigQuery reaches Agent Platform models through a Cloud resource connection. You can reference a specific connection (project.region.connection_id) or use DEFAULT. The connection must be in the same location as the dataset that holds the model.

Remote model useRole for the connection's service account
Inference with a pre-trained modelAgent Platform User
Supervised tuningVertex AI Service Agent (in the project where you create the model)

Step 2: Create a Baseline Remote Model

CREATE OR REPLACE MODEL `support.gemini_baseline`
REMOTE WITH CONNECTION DEFAULT
OPTIONS (ENDPOINT = 'gemini-2.5-pro');

You can name the model (BigQuery picks the regional endpoint for the dataset's location) or give a full endpoint URL. Supported Gemini models can also use the global endpoint. It improves availability and cuts 429 resource-exhausted errors, but you can't control which region processes the request, so skip it when data-processing location matters.

Run the baseline over held-out rows with AI.GENERATE_TEXT. It is table-valued and supports Gemini, partner, and open models. AI.GENERATE_TABLE returns output that follows a schema you define.

Step 3: Create the Tuned Model

Tuning uses the same CREATE MODEL ... REMOTE statement plus an AS SELECT that supplies training pairs:

CREATE OR REPLACE MODEL `support.gemini_tuned`
REMOTE WITH CONNECTION DEFAULT
OPTIONS (
  endpoint = 'gemini-2.5-pro',
  prompt_col = 'prompt',
  input_label_cols = ['label'],
  max_iterations = 1500,        -- 3 epochs for 500 rows
  learning_rate_multiplier = 1.0,
  data_split_method = 'RANDOM',
  data_split_eval_fraction = 0.1,
  evaluation_task = 'CLASSIFICATION')
AS
SELECT CONCAT('Classify this ticket as BILLING, TECH, or ACCOUNT: ', body) AS prompt,
       category AS label
FROM `support.labeled_tickets`;
OptionMeaning
PROMPT_COL / INPUT_LABEL_COLSColumns holding the prompt and the ideal response. The defaults are columns named prompt and label. Both must be STRING
MAX_ITERATIONSConverted to epochs. The number of input rows equals one epoch, so 100 rows and a value of 200 gives two epochs. A value that isn't a multiple rounds up (101 gives two epochs)
LEARNING_RATE_MULTIPLIERScales the recommended learning rate (default 1.0)
DATA_SPLIT_METHODAUTO_SPLIT (default), RANDOM, CUSTOM, SEQ, NO_SPLIT. Evaluation rows enable early stopping to reduce overfitting
DATA_SPLIT_EVAL_FRACTIONEvaluation fraction for RANDOM or SEQ (default 0.2)
EVALUATION_TASKTEXT_GENERATION, CLASSIFICATION, SUMMARIZATION, QUESTION_ANSWERING, or UNSPECIFIED (default)

With AUTO_SPLIT, fewer than 500 rows all go to training. From 500 to 50,000 rows, 20% is held out for evaluation. Above 50,000 rows, 10,000 rows are held out. After splitting, the training set needs at least 10 rows.

Cost: you pay BigQuery for bytes processed from the training table, and Agent Platform for tokens processed during tuning.

Step 4: Evaluate Baseline vs. Tuned

SELECT * FROM ML.EVALUATE(
  MODEL `support.gemini_tuned`,
  (SELECT prompt AS input_text, label AS output_text FROM `support.holdout_tickets`),
  STRUCT('classification' AS task_type));
task_typeMetrics returned
TEXT_GENERATIONbleu4_score, rouge-l_precision, rouge-l_recall, rouge-l_f1
CLASSIFICATIONprecision, recall, f1 per label
SUMMARIZATIONrouge-l_precision, rouge-l_recall, rouge-l_f1
QUESTION_ANSWERINGexact_match (share of outputs identical to the ground truth)

Run the same ML.EVALUATE query against the baseline model and the tuned model on identical holdout rows. Only keep the tuned model if it's clearly better. Not every use case improves with tuning.

Preparing Tuning Data That Actually Helps

Tuning copies patterns from your examples, including mistakes. Before running CREATE MODEL:

  • Use the production prompt template. If inference will send "Classify this ticket as BILLING, TECH, or ACCOUNT: ...", the training prompts need that same prefix. A template that differs between tuning and inference is a quiet form of training-serving skew.
  • Make labels exact and consistent. Normalize casing and spelling (BILLING, not Billing or billing issue) so the model learns one output form.
  • Cover the real distribution. Include rare categories, long and short inputs, and edge cases you see in production, not only easy examples.
  • Remove duplicates and leakage. Duplicate rows inflate effective epochs, and rows copied into both training and holdout inflate evaluation scores.
  • Hold out a separate evaluation table from the same time period and distribution, so baseline and tuned models are judged on identical inputs.
  • Redact sensitive data before tuning if the use case doesn't need it (see Chapter 5 on Sensitive Data Protection).

Running the tuned model in production

A tuned remote model is used like any other BigQuery ML model. Scheduled queries or pipeline steps call AI.GENERATE_TEXT or AI.GENERATE_TABLE over new rows and write results back to BigQuery. Because the tuned model is tied to a specific base model version, plan to re-tune and re-evaluate when that base version reaches its retirement date. Treat the evaluation query as a reusable regression test.

Tune, Prompt, or Ground?

SymptomBetter first move
Output style, tone, or format is inconsistent despite clear instructionsSupervised tuning with examples of the desired output
Classification labels drift or ignore your label setFew-shot prompting, then tuning on labeled examples
Answers need current or proprietary facts that change oftenGrounding / retrieval-augmented generation (RAG), not tuning
Only a handful of examples existPrompt engineering and few-shot examples, since tuning needs enough good data

Exam Traps

  • Recommending an export to Cloud Storage and a Python tuning script when the data is in BigQuery and the team wants low-code SQL.
  • Forgetting the IAM grant on the connection's service account, a common cause of failed tuning statements.
  • Judging tuning by a few eyeballed outputs instead of comparing ML.EVALUATE metrics for baseline and tuned models on the same holdout set.
  • Using the global endpoint when regulations require processing in a specific region.
Loading diagram...
Gemini Tuning Workflow in BigQuery ML
Test Your Knowledge

A BigQuery ML statement that creates a tuned Gemini remote model fails with a permissions error, although baseline inference with the same connection works. What is the most likely fix?

A
B
C
D
Test Your Knowledge

A training table has 400 rows, and the team wants three epochs of Gemini supervised tuning in BigQuery ML. What MAX_ITERATIONS value should they set?

A
B
C
D
Test Your Knowledge

After tuning Gemini in BigQuery ML for a text rewriting task, the team calls ML.EVALUATE with task_type TEXT_GENERATION. Which metrics does it return?

A
B
C
D