- 1.122.0 (latest)
- 1.121.0
- 1.120.0
- 1.119.0
- 1.118.0
- 1.117.0
- 1.116.0
- 1.115.0
- 1.114.0
- 1.113.0
- 1.112.0
- 1.111.0
- 1.110.0
- 1.109.0
- 1.108.0
- 1.107.0
- 1.106.0
- 1.105.0
- 1.104.0
- 1.103.0
- 1.102.0
- 1.101.0
- 1.100.0
- 1.99.0
- 1.98.0
- 1.97.0
- 1.96.0
- 1.95.1
- 1.94.0
- 1.93.1
- 1.92.0
- 1.91.0
- 1.90.0
- 1.89.0
- 1.88.0
- 1.87.0
- 1.86.0
- 1.85.0
- 1.84.0
- 1.83.0
- 1.82.0
- 1.81.0
- 1.80.0
- 1.79.0
- 1.78.0
- 1.77.0
- 1.76.0
- 1.75.0
- 1.74.0
- 1.73.0
- 1.72.0
- 1.71.1
- 1.70.0
- 1.69.0
- 1.68.0
- 1.67.1
- 1.66.0
- 1.65.0
- 1.63.0
- 1.62.0
- 1.60.0
- 1.59.0
- 1.58.0
- 1.57.0
- 1.56.0
- 1.55.0
- 1.54.1
- 1.53.0
- 1.52.0
- 1.51.0
- 1.50.0
- 1.49.0
- 1.48.0
- 1.47.0
- 1.46.0
- 1.45.0
- 1.44.0
- 1.43.0
- 1.39.0
- 1.38.1
- 1.37.0
- 1.36.4
- 1.35.0
- 1.34.0
- 1.33.1
- 1.32.0
- 1.31.1
- 1.30.1
- 1.29.0
- 1.28.1
- 1.27.1
- 1.26.1
- 1.25.0
- 1.24.1
- 1.23.0
- 1.22.1
- 1.21.0
- 1.20.0
- 1.19.1
- 1.18.3
- 1.17.1
- 1.16.1
- 1.15.1
- 1.14.0
- 1.13.1
- 1.12.1
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.1
- 1.7.1
- 1.6.2
- 1.5.0
- 1.4.3
- 1.3.0
- 1.2.0
- 1.1.1
- 1.0.1
- 0.9.0
- 0.8.0
- 0.7.1
- 0.6.0
- 0.5.1
- 0.4.0
- 0.3.1
Vertex AI SDK for Python
Vertex AI: Google Vertex AI is an integrated suite of machine learning tools and services for building and using ML models with AutoML or custom code. It offers both novices and experts the best workbench for the entire machine learning development lifecycle.
Quick Start
In order to use this library, you first need to go through the following steps:
Installation
Install this library in avirtualenv using pip.virtualenv is a tool tocreate isolated Python environments. The basic problem it addresses is one ofdependencies and versions, and indirectly permissions.
Withvirtualenv, it’s possible to install this library without needing systeminstall permissions, and without clashing with the installed systemdependencies.
Mac/Linux
pip install virtualenvvirtualenv <your-env>source <your-env>/bin/activate<your-env>/bin/pip install google-cloud-aiplatformWindows
pip install virtualenvvirtualenv <your-env><your-env>\Scripts\activate<your-env>\Scripts\pip.exe install google-cloud-aiplatformOverview
This section provides a brief overview of the Vertex AI SDK for Python. You can also reference the notebooks invertex-ai-samples for examples.
Importing
SDK functionality can be used from the root of the package:
from google.cloud import aiplatformInitialization
Initialize the SDK to store common configurations that you use with the SDK.
aiplatform.init( # your Google Cloud Project ID or number # environment default used is not set project='my-project', # the Vertex AI region you will use # defaults to us-central1 location='us-central1', # Google Cloud Storage bucket in same region as location # used to stage artifacts staging_bucket='gs://my_staging_bucket', # custom google.auth.credentials.Credentials # environment default creds used if not set credentials=my_credentials, # customer managed encryption key resource name # will be applied to all Vertex AI resources if set encryption_spec_key_name=my_encryption_key_name, # the name of the experiment to use to track # logged metrics and parameters experiment='my-experiment', # description of the experiment above experiment_description='my experiment decsription')Datasets
Vertex AI provides managed tabular, text, image, and video datasets. In the SDK, datasets can be used downstream totrain models.
To create a tabular dataset:
my_dataset = aiplatform.TabularDataset.create( display_name="my-dataset", gcs_source=['gs://path/to/my/dataset.csv'])You can also create and import a dataset in separate steps:
from google.cloud import aiplatformmy_dataset = aiplatform.TextDataset.create( display_name="my-dataset")my_dataset.import( gcs_source=['gs://path/to/my/dataset.csv'] import_schema_uri=aiplatform.schema.dataset.ioformat.text.multi_label_classification)To get a previously created Dataset:
dataset = aiplatform.ImageDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}')Vertex AI supports a variety of dataset schemas. References to these schemas are available under theaiplatform.schema.dataset namespace. For more information on the supported dataset schemas please refer to thePreparing data docs.
Training
The Vertex AI SDK for Python allows you train Custom and AutoML Models.
You can train custom models using a custom Python script, custom Python package, or container.
Preparing Your Custom Code
Vertex AI custom training enables you to train on Vertex AI datasets and produce Vertex AI models. To do so yourscript must adhere to the following contract:
It must read datasets from the environment variables populated by the training service:
os.environ['AIP_DATA_FORMAT'] # provides format of dataos.environ['AIP_TRAINING_DATA_URI'] # uri to training splitos.environ['AIP_VALIDATION_DATA_URI'] # uri to validation splitos.environ['AIP_TEST_DATA_URI'] # uri to test splitPlease visitUsing a managed dataset in a custom training application for a detailed overview.
It must write the model artifact to the environment variable populated by the traing service:
os.environ['AIP_MODEL_DIR']Running Training
job = aiplatform.CustomTrainingJob( display_name="my-training-job", script_path="training_script.py", container_uri="gcr.io/cloud-aiplatform/training/tf-cpu.2-2:latest", requirements=["gcsfs==0.7.1"], model_serving_container_image_uri="gcr.io/cloud-aiplatform/prediction/tf2-cpu.2-2:latest",)model = job.run(my_dataset, replica_count=1, machine_type="n1-standard-4", accelerator_type='NVIDIA_TESLA_K80', accelerator_count=1)In the code block above my_dataset is managed dataset created in the Dataset section above. The model variable is a managed Vertex AI model that can be deployed or exported.
AutoMLs
The Vertex AI SDK for Python supports AutoML tabular, image, text, video, and forecasting.
To train an AutoML tabular model:
dataset = aiplatform.TabularDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}')job = aiplatform.AutoMLTabularTrainingJob( display_name="train-automl", optimization_prediction_type="regression", optimization_objective="minimize-rmse",)model = job.run( dataset=dataset, target_column="target_column_name", training_fraction_split=0.6, validation_fraction_split=0.2, test_fraction_split=0.2, budget_milli_node_hours=1000, model_display_name="my-automl-model", disable_early_stopping=False,)Models
To deploy a model:
endpoint = model.deploy(machine_type="n1-standard-4", min_replica_count=1, max_replica_count=5 machine_type='n1-standard-4', accelerator_type='NVIDIA_TESLA_K80', accelerator_count=1)To upload a model:
model = aiplatform.Model.upload( display_name='my-model', artifact_uri="gs://python/to/my/model/dir", serving_container_image_uri="gcr.io/cloud-aiplatform/prediction/tf2-cpu.2-2:latest",)To get a model:
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')Please visitImporting models to Vertex AI for a detailed overview:
Batch Prediction
To create a batch prediction job:
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')batch_prediction_job = model.batch_predict( job_display_name='my-batch-prediction-job', instances_format='csv', machine_type='n1-standard-4', gcs_source=['gs://path/to/my/file.csv'], gcs_destination_prefix='gs://path/to/my/batch_prediction/results/', service_account='my-sa@my-project.iam.gserviceaccount.com')You can also create a batch prediction job asynchronously by including the sync=False argument:
batch_prediction_job = model.batch_predict(..., sync=False)# wait for resource to be createdbatch_prediction_job.wait_for_resource_creation()# get the statebatch_prediction_job.state# block until job is completebatch_prediction_job.wait()Endpoints
To get predictions from endpoints:
endpoint.predict(instances=[[6.7, 3.1, 4.7, 1.5], [4.6, 3.1, 1.5, 0.2]])To create an endpoint
endpoint = endpoint.create(display_name='my-endpoint')To deploy a model to a created endpoint:
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')endpoint.deploy(model, min_replica_count=1, max_replica_count=5 machine_type='n1-standard-4', accelerator_type='NVIDIA_TESLA_K80', accelerator_count=1)To undeploy models from an endpoint:
endpoint.undeploy_all()To delete an endpoint:
endpoint.delete()Pipelines
To create a Vertex AI Pipeline run and monitor until completion:
# Instantiate PipelineJob objectpl = PipelineJob( display_name="My first pipeline", # Whether or not to enable caching # True = always cache pipeline step result # False = never cache pipeline step result # None = defer to cache option for each pipeline component in the pipeline definition enable_caching=False, # Local or GCS path to a compiled pipeline definition template_path="pipeline.json", # Dictionary containing input parameters for your pipeline parameter_values=parameter_values, # GCS path to act as the pipeline root pipeline_root=pipeline_root,)# Execute pipeline in Vertex AI and monitor until completionpl.run( # Email address of service account to use for the pipeline run # You must have iam.serviceAccounts.actAs permission on the service account to use it service_account=service_account, # Whether this function call should be synchronous (wait for pipeline run to finish before terminating) # or asynchronous (return immediately) sync=True)To create a Vertex AI Pipeline without monitoring until completion, use submit instead of run:
# Instantiate PipelineJob objectpl = PipelineJob( display_name="My first pipeline", # Whether or not to enable caching # True = always cache pipeline step result # False = never cache pipeline step result # None = defer to cache option for each pipeline component in the pipeline definition enable_caching=False, # Local or GCS path to a compiled pipeline definition template_path="pipeline.json", # Dictionary containing input parameters for your pipeline parameter_values=parameter_values, # GCS path to act as the pipeline root pipeline_root=pipeline_root,)# Submit the Pipeline to Vertex AIpl.submit( # Email address of service account to use for the pipeline run # You must have iam.serviceAccounts.actAs permission on the service account to use it service_account=service_account,)Explainable AI: Get Metadata
To get metadata in dictionary format from TensorFlow 1 models:
from google.cloud.aiplatform.explain.metadata.tf.v1 import saved_model_metadata_builderbuilder = saved_model_metadata_builder.SavedModelMetadataBuilder( 'gs://python/to/my/model/dir', tags=[tf.saved_model.tag_constants.SERVING] )generated_md = builder.get_metadata()To get metadata in dictionary format from TensorFlow 2 models:
from google.cloud.aiplatform.explain.metadata.tf.v2 import saved_model_metadata_builderbuilder = saved_model_metadata_builder.SavedModelMetadataBuilder('gs://python/to/my/model/dir')generated_md = builder.get_metadata()To use Explanation Metadata in endpoint deployment and model upload:
explanation_metadata = builder.get_metadata_protobuf()# To deploy a model to an endpoint with explanationmodel.deploy(..., explanation_metadata=explanation_metadata)# To deploy a model to a created endpoint with explanationendpoint.deploy(..., explanation_metadata=explanation_metadata)# To upload a model with explanationaiplatform.Model.upload(..., explanation_metadata=explanation_metadata)Next Steps
Read theClient Library Documentation for Vertex AIAPI to see other available methods on the client.
Read theVertex AI API Product documentation to learnmore about the product and see How-to Guides.
View thisREADME to see the full list of CloudAPIs that we cover.
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-10-30 UTC.