How to Intermediate · 3 min read

How to use Vertex AI for fine-tuning

Quick answer
Use Google Cloud's Vertex AI Python client library to create a fine-tuning job for Gemini models by specifying your training dataset and model parameters. The process involves uploading your dataset to Google Cloud Storage, configuring a CustomJob or FineTuneJob with the Gemini base model, and monitoring the training job via the Vertex AI API.

PREREQUISITES

  • Python 3.8+
  • Google Cloud account with Vertex AI enabled
  • Google Cloud SDK installed and configured
  • Service account with Vertex AI permissions
  • pip install google-cloud-aiplatform

Setup

Install the google-cloud-aiplatform Python package and set up authentication with a service account key that has Vertex AI permissions. Ensure your Google Cloud project and Vertex AI API are enabled.

bash
pip install google-cloud-aiplatform

Step by step

This example demonstrates how to fine-tune a Gemini model on Vertex AI using Python. It assumes you have your training data uploaded to a Google Cloud Storage bucket.

python
from google.cloud import aiplatform
import os

# Set environment variables
PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']
LOCATION = 'us-central1'
BUCKET_NAME = 'your-gcs-bucket'
TRAINING_DATA_URI = f'gs://{BUCKET_NAME}/training-data.jsonl'

# Initialize Vertex AI client
client_options = {'api_endpoint': f'{LOCATION}-aiplatform.googleapis.com'}
aiplatform.init(project=PROJECT_ID, location=LOCATION)

# Define fine-tuning job parameters
fine_tune_job = aiplatform.CustomJob(
    display_name='gemini-fine-tune-job',
    worker_pool_specs=[{
        'machine_spec': {'machine_type': 'n1-standard-4'},
        'replica_count': 1,
        'python_package_spec': {
            'executor_image_uri': 'gcr.io/cloud-aiplatform/training/tf-cpu.2-6:latest',
            'package_uris': [],  # Your training package if any
            'python_module': 'trainer.task',  # Your training module
            'args': [
                f'--training_data_uri={TRAINING_DATA_URI}',
                '--model_name=gemini-1.5-pro',
                '--output_dir=gs://' + BUCKET_NAME + '/fine-tune-output'
            ]
        }
    }]
)

# Run the fine-tuning job
fine_tune_job.run(sync=True)

print('Fine-tuning job completed.')
output
Fine-tuning job completed.

Common variations

  • Use aiplatform.FineTuneJob if available for direct Gemini fine-tuning support.
  • Adjust machine_type for larger models or faster training.
  • Use asynchronous job execution with sync=False to monitor progress separately.
  • Switch regions by changing the LOCATION variable.

Troubleshooting

  • If you see permission errors, verify your service account has Vertex AI Admin and Storage Object Viewer roles.
  • For quota errors, check your Google Cloud project quotas for Vertex AI and increase if needed.
  • If training fails, check logs in the Google Cloud Console under Vertex AI > Training jobs.

Key Takeaways

  • Use the official Google Cloud Python SDK google-cloud-aiplatform for Vertex AI fine-tuning.
  • Upload your training data to Google Cloud Storage before starting fine-tuning jobs.
  • Configure your fine-tuning job with appropriate machine types and model names for Gemini.
  • Monitor training jobs via the Google Cloud Console or programmatically with the SDK.
  • Ensure your service account has all necessary permissions to avoid common errors.
Verified 2026-04 · gemini-1.5-pro
Verify ↗