Vertex AI SFT Gemini Migration Recipe
Source notebook
Repo path:
gemini/tuning/sft_gemini_migration_recipe.ipynb· Open on GitHub · intermediate
Migrates explicit SFT hyperparameters from gemini-1.5-flash-002 tuning jobs to gemini-2.5-flash.
Summary
This notebook shows how to inspect an existing Vertex AI Gemini supervised tuning job and transform its explicitly set hyperparameters for migration. It initializes the Google Gen AI SDK with Vertex AI, retrieves a legacy tuning job, derives new epoch and learning-rate settings, then creates a new tuning job on gemini-2.5-flash using the same Cloud Storage datasets.
Key code patterns
Vertex AI GenAI client
from google import genai
client = genai.Client(
vertexai=True,
project=PROJECT_ID,
location=LOCATION,
)Uses google-genai against Vertex AI with project and location configuration.
Extract tuning specs
def get_tuning_job_hparams(tuning_job):
spec = tuning_job.supervised_tuning_spec
hparams = spec.hyper_parameters
stats = tuning_job.tuning_data_stats.supervised_tuning_data_stats
return {
"base_model": tuning_job.base_model,
"training_dataset_uri": spec.training_dataset_uri,
"epoch_count": hparams.epoch_count,
"learning_rate_multiplier": hparams.learning_rate_multiplier,
"sft_num_examples": stats.tuning_dataset_example_count,
}Reads legacy tuning metadata and data statistics needed for migration decisions.
Map legacy hyperparameters
def gemini_1_5_flash_to_gemini_2_5_flash(old_specs):
if old_specs.get("base_model") != "gemini-1.5-flash-002":
return None
new_specs = {
"base_model": "gemini-2.5-flash",
"training_dataset_uri": old_specs["training_dataset_uri"],
"validation_dataset_uri": old_specs["validation_dataset_uri"],
}
new_specs["learning_rate_multiplier"] = lrm_1_5_flash_to_2_5_flash(old_specs["learning_rate_multiplier"])
return new_specsApplies notebook-defined migration logic instead of reusing legacy values blindly.
Create migrated tuning job
tuning_job = client.tunings.tune(
base_model=new_job_specs["base_model"],
training_dataset=types.TuningDataset(gcs_uri=new_job_specs["training_dataset_uri"]),
config=types.CreateTuningJobConfig(
epoch_count=new_job_specs["epoch_count"],
adapter_size=new_job_specs["adapter_size"],
learning_rate_multiplier=new_job_specs["learning_rate_multiplier"],
),
)Starts a new tuning job on the migrated base model with converted hyperparameters.
Models & APIs used
- Models: gemini-1.5-flash-002, gemini-2.5-flash
- APIs / services: Vertex AI, Cloud Storage
- SDKs / libraries:
google-genai
When to use this
Use this pattern when migrating explicitly configured Gemini 1.5 Flash supervised tuning jobs to Gemini 2.5 Flash.
Gotchas & caveats
- Vertex AI API must be enabled for the Google Cloud project.
- Colab requires explicit user authentication with google.colab.auth.authenticate_user().
- The notebook says default legacy hyperparameters can rely on API or SDK defaults instead of this recipe.
- PROJECT_NUMBER is used to build the tuning job resource name but is not defined in the shown setup cells.
- The API does not provide max_seq_len, so the notebook estimates it from max input plus max output token distributions.
- LOCATION defaults to global from GOOGLE_CLOUD_REGION when not supplied.
Best practices
- Do not apply the same hyperparameters from legacy Gemini models to latest Gemini models because model architecture and tuning infrastructure changed.
- Use API or SDK defaults for new models when the legacy tuning job did not explicitly set hyperparameters.
- Reuse the existing training and validation dataset URIs when creating the migrated tuning job.
- Check tuning data statistics before changing epoch count for migration.
Related
- Concepts: Tuning & Customization · MLOps & Deployment
- Entities: Vertex AI · Google GenAI SDK · Cloud Storage · Gemini
- Area: Gemini Notebooks
- Best practices: Tuning & Customization - Best Practices · MLOps & Deployment - Best Practices