How to use service account with Vertex AI
Quick answer
To use a
service account with Vertex AI, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your service account JSON key file. Then initialize the vertexai SDK in Python, which will automatically use these credentials for authentication.PREREQUISITES
Python 3.8+Google Cloud project with Vertex AI enabledService account JSON key file with Vertex AI permissionspip install vertexai google-cloud-aiplatform
Setup
Install the required Python packages and set the environment variable for your service account key.
- Install the
vertexaiandgoogle-cloud-aiplatformpackages. - Download your service account JSON key from Google Cloud Console with Vertex AI permissions.
- Set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to the key file path.
pip install vertexai google-cloud-aiplatform
# On Linux/macOS
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account.json"
# On Windows (PowerShell)
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\your\service-account.json" Step by step
This example shows how to initialize the vertexai SDK using the service account credentials and generate a text completion with the gemini-2.0-flash model.
import os
import vertexai
from vertexai.generative_models import GenerativeModel
# Initialize Vertex AI SDK (auto-uses service account credentials)
vertexai.init(
project=os.environ.get("GOOGLE_CLOUD_PROJECT"),
location="us-central1"
)
# Load the Gemini model
model = GenerativeModel("gemini-2.0-flash")
# Generate text
response = model.generate_content("Explain how to use a service account with Vertex AI.")
print(response.text) output
Explain how to use a service account with Vertex AI. A service account allows secure authentication to Google Cloud services including Vertex AI. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to your service account JSON key file path. Then initialize the Vertex AI SDK in Python, which will use these credentials automatically.
Common variations
You can also authenticate using google.auth libraries or use asynchronous calls with the vertexai SDK. For streaming or chat models, use the respective classes like GenerativeModel with streaming or chat methods.
import asyncio
import os
import vertexai
from vertexai.generative_models import GenerativeModel
vertexai.init(
project=os.environ.get("GOOGLE_CLOUD_PROJECT"),
location="us-central1"
)
async def main():
model = GenerativeModel("gemini-2.0-flash")
response = model.generate_content("Hello, how do I use a service account with Vertex AI?")
print(response.text)
asyncio.run(main()) output
Hello! To use a service account with Vertex AI, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to your service account key file path. The Vertex AI SDK will then authenticate automatically using those credentials.
Troubleshooting
- If you get authentication errors: Verify the
GOOGLE_APPLICATION_CREDENTIALSpath is correct and the service account has the necessary Vertex AI roles. - If the project or location is not set: Ensure you pass
projectandlocationtovertexai.init()or set them in your environment variables. - Permission denied errors: Check IAM permissions for the service account in Google Cloud Console.
Key Takeaways
- Set the GOOGLE_APPLICATION_CREDENTIALS env var to your service account JSON key file path.
- Initialize the vertexai SDK with your Google Cloud project and location to authenticate automatically.
- Use vertexai.generative_models classes like GenerativeModel for inference.
- Verify IAM permissions and environment variables if authentication fails.