Vertex AI Python SDK installation
Quick answer
Install the Vertex AI Python SDK by running
pip install google-cloud-aiplatform. Then initialize it in your Python code with import vertexai and vertexai.init() to start using Google Cloud Vertex AI services.PREREQUISITES
Python 3.8+Google Cloud account with Vertex AI enabledGoogle Cloud SDK installed and authenticatedpip install google-cloud-aiplatform
Setup
Install the official Vertex AI Python SDK package google-cloud-aiplatform using pip. Ensure you have Python 3.8 or higher and have authenticated your Google Cloud SDK with gcloud auth login. Set your Google Cloud project and region before using the SDK.
pip install google-cloud-aiplatform Step by step
Here is a complete example to initialize the Vertex AI SDK and list available models in your project.
import os
import vertexai
from vertexai import language_models
# Initialize Vertex AI with your project and location
vertexai.init(
project=os.environ.get("GOOGLE_CLOUD_PROJECT"),
location="us-central1"
)
# List available language models
client = language_models.LanguageModelsClient()
models = client.list_models()
for model in models:
print(f"Model name: {model.name}") output
Model name: projects/123456789/locations/us-central1/models/text-bison@001 Model name: projects/123456789/locations/us-central1/models/chat-bison@001 ...
Common variations
You can use the SDK asynchronously with asyncio or specify different regions and projects in vertexai.init(). For chat models, use vertexai.language_models.ChatModel. The SDK supports streaming responses and advanced configuration.
import asyncio
import os
import vertexai
from vertexai.language_models import ChatModel
async def async_chat():
vertexai.init(project=os.environ["GOOGLE_CLOUD_PROJECT"], location="us-central1")
chat_model = ChatModel.from_pretrained("chat-bison@001")
chat = chat_model.start_chat()
response = await chat.send_message_async("Hello from async Vertex AI!")
print(response.text)
asyncio.run(async_chat()) output
Hello from async Vertex AI!
Troubleshooting
- If you get authentication errors, run
gcloud auth application-default loginto set up credentials. - Ensure your Google Cloud project has Vertex AI API enabled in the Cloud Console.
- If
vertexai.init()fails, verify yourGOOGLE_CLOUD_PROJECTenvironment variable is set correctly.
Key Takeaways
- Use
pip install google-cloud-aiplatformto install the Vertex AI Python SDK. - Initialize the SDK with
vertexai.init(project=..., location=...)before making API calls. - Authenticate with Google Cloud using
gcloud auth application-default loginto avoid credential errors.