Use the Vertex AI SDK for Python

Stay organized with collections Save and categorize content based on your preferences.
The Vertex AI SDK for Python helps you automate data ingestion and train models to get predictions on Vertex AI. The SDK uses Python code to access the Vertex AI API so that you can programmatically accomplish anything you can do in the Vertex AI console.

Why use the Vertex AI SDK

The SDK is recommended if you are an experienced machine learning (ML) and artificial intelligence (AI) engineer or a data scientist who wants to programmatically automate your workflow. The Vertex AI SDK for Python is similar to the Vertex AI Python client library, except the SDK is higher-level and less granular. For more information, see Understand the SDK and client library differences.

Install the Vertex AI SDK

Installation of the Vertex AI SDK for Python includes the following steps:

  1. Create an isolated Python environment
  2. Install the Vertex AI SDK package
  3. Initialize the Vertex AI SDK

Create an isolated Python environment

A Python best practice is to install the Vertex AI SDK in an isolated Python environment for each project. This helps prevent dependency, version, and permissions conflicts. You can create an isolated environment for using the command line in a shell or for using a notebook.

To create an isolated environment when you use the command line, activate a venv environment . After the venv environment is activated, you're ready to install the Vertex AI SDK and run your Python scripts. For more information, see Use venv to isolate dependencies and Set up a Python development environment.

To use a notebook for your isolated environment, create a notebook instance. After you create your notebook instance, use it to install the Vertex AI SDK and run your Python scripts. For more information, see Create a user-managed notebooks instance.

Install the Vertex AI SDK package

To install the Vertex AI SDK, run the following command in your virtual environment:

pip install google-cloud-aiplatform

Initialize the Vertex AI SDK

After you install the Vertex AI SDK for Python, you must initialize the SDK with your Vertex AI and Google Cloud details. For example, when you initialize the SDK, you specify information such as your project name, region, and your staging Cloud Storage bucket. The following method is an example of a method that initializes the Vertex AI SDK.

def init_sample(
    project: Optional[str] = None,
    location: Optional[str] = None,
    experiment: Optional[str] = None,
    staging_bucket: Optional[str] = None,
    credentials: Optional[auth_credentials.Credentials] = None,
    encryption_spec_key_name: Optional[str] = None,
):

    from google.cloud import aiplatform

    aiplatform.init(
        project=project,
        location=location,
        experiment=experiment,
        staging_bucket=staging_bucket,
        credentials=credentials,
        encryption_spec_key_name=encryption_spec_key_name,
    )

Write code with the Vertex AI SDK

To use the Vertex AI SDK for Python, you must add a line in your code to import its namespace. Next, use the Vertex AI SDK reference documentation and code samples to learn about its classes, methods, and properties.

Import the Vertex AI SDK namespace

The Vertex AI SDK for Python uses the google.cloud.aiplatform namespace. To get access to the SDK's functionality, import this namespace by adding the following line to your Python code:

from google.cloud import aiplatform

Learn about the Vertex AI SDK

The Vertex AI SDK for Python reference documents the Vertex AI SDK for Python functionality. This reference includes class definitions, methods, properties, and everything else required to use the Vertex AI SDK to programmatically automate Vertex AI. For more information, see Vertex AI SDK reference.

Try code samples and tutorials

Jupyter notebook tutorials show how to use the Vertex AI SDK for Python as part of a larger workflow. For more information, see Vertex AI notebook tutorials.

Code samples in the Vertex AI SDK for Python GitHub repository show you how to complete individual tasks. For more information, see the Vertex AI SDK for Python GitHub repository.

Understand the SDK and client library differences

When you install the Vertex AI SDK for Python, the Vertex AI Python client library is also installed. The Vertex AI SDK and the Vertex AI Python client library provide similar functionality with different levels of granularity. The Vertex AI SDK operates at a higher level of abstraction than the client library and is suitable for most common data science workflows. If you need lower-level functionality, then use the Vertex AI Python client library.

The Vertex AI SDK is available for Python and a Vertex AI client library is available for Python, Java, and Node.js. To learn how to install the Java or Node.js client library, see Install the Vertex AI client libraries. If a client library isn't available in your preferred programming language, you can use the Vertex AI REST API. For more information, see the Vertex AI REST reference.

Use Vertex AI Python client library and SDK together

If you use the Vertex AI SDK for Python and discover you need greater flexibility or control, or if you need a method not included in the SDK, you can use the Vertex AI Python client library in the same workflow. The Vertex AI Python client library uses a different namespace to access the Vertex AI API. The client library and the Python SDK namespaces can be used in the same Python script by adding an import line for each in your Python script.

To learn about the Vertex AI SDK namespace, see Import the Vertex AI SDK namespace earlier in this page.

Import the Vertex AI Python client library namespace

The Vertex AI Python client library namespace is google.cloud.aiplatform.gapic. This namespace maps to the google.cloud.aiplatform_v1 namespace. These two namespaces can be used interchangeably. To import the Python client library, include one of the following in your Python script:

from google.cloud import aiplatform_gapic
from google.cloud import aiplatform_v1

What's next