How to authenticate with Vertex AI
Quick answer
Authenticate with
Vertex AI by setting up Google Cloud Application Default Credentials using gcloud auth application-default login or by providing a service account key JSON file via the GOOGLE_APPLICATION_CREDENTIALS environment variable. Then initialize the vertexai SDK in Python, which automatically uses these credentials for API calls.PREREQUISITES
Python 3.8+Google Cloud SDK installedGoogle Cloud project with Vertex AI enabledService account with Vertex AI permissions or user credentialspip install vertexai
Setup
Install the vertexai Python SDK and configure authentication by either logging in with your user account or setting a service account key.
- Install the SDK:
pip install vertexai - Authenticate with user credentials:
gcloud auth application-default login - Or set the environment variable for service account key JSON:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
pip install vertexai Step by step
This example shows how to initialize the vertexai SDK and generate a text completion using the gemini-2.0-flash model. The SDK automatically uses your authenticated credentials.
import os
import vertexai
from vertexai.generative_models import GenerativeModel
# Initialize Vertex AI with your Google Cloud project and location
vertexai.init(project=os.environ["GOOGLE_CLOUD_PROJECT"], location="us-central1")
# Load the Gemini model
model = GenerativeModel("gemini-2.0-flash")
# Generate content
response = model.generate_content("Explain quantum computing in simple terms.")
print(response.text) output
Quantum computing is a type of computing that uses quantum bits, or qubits, which can represent both 0 and 1 at the same time, allowing computers to solve certain problems much faster than traditional computers.
Common variations
You can authenticate asynchronously or use different models by changing the model parameter. The vertexai SDK supports streaming responses by setting stream=True in generate_content.
import asyncio
import os
import vertexai
from vertexai.generative_models import GenerativeModel
async def async_generate():
vertexai.init(project=os.environ["GOOGLE_CLOUD_PROJECT"], location="us-central1")
model = GenerativeModel("gemini-2.5-pro")
response = await model.generate_content("Summarize the benefits of AI.", stream=True)
async for chunk in response:
print(chunk.text, end="", flush=True)
asyncio.run(async_generate()) output
AI improves efficiency, automates tasks, enables new insights through data analysis, and enhances decision-making across industries.
Troubleshooting
- If you see
PermissionDeniederrors, verify your service account has theVertex AI Userrole. - If authentication fails, ensure
GOOGLE_APPLICATION_CREDENTIALSpoints to a valid JSON key file or rungcloud auth application-default login. - Check your
GOOGLE_CLOUD_PROJECTenvironment variable is set correctly.
Key Takeaways
- Use Application Default Credentials or service account keys to authenticate with Vertex AI.
- Initialize the
vertexaiSDK with your project and location to make authenticated API calls. - For async or streaming, use
generate_contentwithstream=True. - Ensure your Google Cloud project and roles are correctly configured to avoid permission errors.