Run Google's Models on Vertex AI with Python + EU Data Residency Tips
By NeuralNine
Key Concepts
- Generative AI Models: AI models capable of creating new content, such as text, images, or code.
- Google Vertex AI: Google Cloud's unified platform for building, deploying, and scaling machine learning models.
- EU Data Residency: The requirement for data to be stored and processed within the European Union for compliance purposes.
- Service Account: An identity that an application or virtual machine can use to authenticate to Google Cloud APIs.
- Vertex AI User Role: A predefined role in Google Cloud that grants permissions to use Vertex AI services.
- JSON Web Token (JWT): A compact, URL-safe means of representing claims to be transferred between two parties. In this context, it's used for authentication credentials.
- Google GenAI Library: A Python library for interacting with Google's generative AI models.
- Python Dotenv: A Python library for loading environment variables from a
.envfile. - Project ID: A unique identifier for a Google Cloud project.
- Location Parameter: A setting in Vertex AI that specifies the geographical region where resources are deployed and data is processed.
- Gemini 2.0 Flash: A specific generative AI model offered by Google.
Using Generative AI Models on Google Vertex AI with EU Data Residency
This video tutorial demonstrates how to leverage Google's generative AI models, such as Gemini, through Google Vertex AI, with a specific focus on enabling EU data residency for compliance. Unlike the simpler Google AI Studio, this method involves setting up a proper Google Cloud project with the Vertex AI API enabled, allowing for explicit control over data location.
1. Setting Up a Google Cloud Project
The process begins with creating a new Google Cloud project.
- Project Creation: Navigate to the Google Cloud Console and click "New Project."
- Project Naming: Assign a descriptive name to the project (e.g., "vertex testing tutorial").
- Billing Account: Connect a billing account to the project. This is necessary as usage of Vertex AI incurs costs. A credit card or other payment method will be required.
- Project Selection: After creation, ensure the newly created project is selected.
2. Enabling Vertex AI API and Creating a Service Account
To interact with Vertex AI programmatically, the API needs to be enabled, and a service account with appropriate permissions must be created.
- Enable Vertex AI API:
- Go to the "Vertex AI API" section within the Google Cloud Console (distinct from the interactive Vertex AI environment).
- Click "Enable" to activate the API for the project.
- Create Service Account:
- Navigate to "IAM & Admin" > "Service Accounts" in the sidebar.
- Click "Create Service Account."
- Provide a name for the service account (e.g., "vertex test account").
- Assign Role: The crucial step is to grant the "Vertex AI User" role to this service account. This role provides the necessary permissions to utilize Vertex AI services.
- Skip optional steps for description and further configuration for this tutorial.
- Generate Service Account Key:
- After creating the service account, click on it to access its details.
- Go to the "Keys" tab.
- Click "Create Key" or "Add Key" and select "Create new key."
- Choose "JSON" as the key type.
- Click "Create." This will download a JSON file containing the service account's credentials.
- Secure Storage: Save this JSON file in your project's working directory (e.g., a "tutorial" directory). This file will be used for authentication.
3. Project Initialization and Authentication
The next steps involve setting up a Python environment and configuring authentication.
- Environment Setup:
- Open a terminal and navigate to your project directory.
- Initialize a Python virtual environment (e.g., using
uv initif usinguv, orpython -m venv venvfor standardvenv).
- Install Dependencies:
- Install the necessary Python packages:
google-generativeai: For interacting with Google's generative AI models.python-dotenv: For loading environment variables from a.envfile.
- Install the necessary Python packages:
- Configure Authentication:
- Create a
.envfile in your project directory. - Add the following line to the
.envfile, pointing to the downloaded JSON key file:
(ReplaceGOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service_account_key.json/path/to/your/service_account_key.jsonwith the actual path to your downloaded JSON file). - Alternative Authentication: The video mentions that
gcloudCLI commands likegcloud auth application-default logincan also be used for system-wide authentication, eliminating the need for a.envfile.
- Create a
4. Coding the Vertex AI Interaction
The core of the tutorial involves writing Python code to interact with Vertex AI.
- Create
main.py: Create a Python file (e.g.,main.py). - Import Libraries:
from dotenv import load_dotenv import google.generativeai as genai - Load Environment Variables:
load_dotenv() - Initialize Vertex AI Client:
- The client initialization differs from using a simple API key.
- Set
vertex_ai=Trueto indicate usage of Vertex AI. - Provide the
projectidentifier (the Project ID, not necessarily the project name, which can be a number). - Specify the
locationparameter to enforce data residency. For EU data residency, use a European region like"europe-west4"(Netherlands) or"europe-west9"(Paris).
genai.configure( # If you are using a service account key file, you don't need to set the API key. # The GOOGLE_APPLICATION_CREDENTIALS environment variable will be used. vertex_ai=True, project="your-project-id", # Replace with your actual Project ID location="europe-west4" # Example: Netherlands for EU data residency )- Note: The video emphasizes that the
projectparameter should be the globally unique Project ID, which might include numbers, not just the display name.
- Generate Content:
- Use the
client.models.generate_contentmethod. - Specify the model (e.g.,
"gemini-2.0-flash"). - Provide the prompt content.
client = genai.GenerativeModel('gemini-2.0-flash') # Or other Gemini models response = client.generate_content("Hello, what is Python?") print(response.text) - Use the
- Running the Code:
- Execute the script using your Python environment manager (e.g.,
uv run main.py).
- Execute the script using your Python environment manager (e.g.,
5. Key Arguments and Perspectives
- EU Data Residency for Compliance: The primary argument for using Vertex AI with a specified location is to meet regulatory requirements, particularly for businesses operating in the EU, where data must remain within the continent. The speaker stresses that this is a programmatic way to achieve this, but legal advice should be sought for full compliance.
- Vertex AI vs. AI Studio: The tutorial highlights Vertex AI as a more robust solution for production environments, offering better integration with Google Cloud services and explicit control over data location, compared to the simpler, free-tier access of Google AI Studio.
- Programmatic Control: The method described provides programmatic control over AI model deployment and data handling, making it suitable for integration into larger applications and workflows.
6. Notable Quotes
- "This is going to be especially interesting for those of you guys who are in the EU and need to rely on EU data residency, for example, for compliance reasons."
- "We're going to learn how to set this up properly as a Google Cloud project with the Vert.ex X AI API enabled which of course has the benefit that we can also specify a location specifically interesting for those of you guys who are for example from the EU like me or maybe other more regulated areas where you have to make sure that the data doesn't leave the continent or area."
- "Now I need to mention though that none of this is legal advice. So, I'm not taking any responsibility here for any legal uh stuff. I'm just showing you how to do it programmatically. Whether that conforms 100% with the compliance of your specific use case, that is something that a lawyer has to answer."
- "Make sure that you're actually copying this ID here and not the name of your project."
- "And then here is where the EU data residency comes in. You can specify a location."
- "So, as far as I understand it, this means that the data never left the European Union, which means it should be at least better for compliance. But again, none of this is legal advice."
7. Logical Connections and Synthesis
The tutorial logically progresses from project setup in Google Cloud to code implementation.
- Project Foundation: Creating a Google Cloud project and enabling the Vertex AI API establishes the necessary infrastructure.
- Authentication Mechanism: Setting up a service account with the "Vertex AI User" role and generating a JSON key file provides a secure method for programmatic access.
- Environment Configuration: Using
.envfiles and installing libraries ensures the Python environment is ready for interaction. - Client Initialization: The specific parameters (
vertex_ai=True,project,location) ingenai.configureare crucial for directing the AI requests to Vertex AI and ensuring data residency. - Model Interaction: The standard
generate_contentmethod is then used, but now it operates within the configured Vertex AI environment, respecting the specified location.
The core takeaway is that by using Google Vertex AI and explicitly setting the location parameter, users can direct their generative AI model requests to specific geographical regions, thereby facilitating compliance with data residency requirements, particularly within the EU. This method offers a more controlled and integrated approach compared to using standalone AI Studio.
8. Data, Research Findings, or Statistics
No specific data, research findings, or statistics were presented in this transcript. The focus was on the technical implementation of using Vertex AI.
9. Conclusion
This video provides a practical, step-by-step guide on how to utilize Google's generative AI models through Google Vertex AI, with a strong emphasis on achieving EU data residency. By setting up a Google Cloud project, enabling the Vertex AI API, creating a service account with the "Vertex AI User" role, and configuring the genai.configure function with the location parameter, users can ensure their AI processing occurs within specified European regions. This approach is vital for organizations needing to comply with data residency regulations, offering a robust and integrated solution within the Google Cloud ecosystem. The tutorial explicitly states that while this method facilitates data residency, it does not constitute legal advice and professional legal consultation is recommended for full compliance.
Chat with this Video
AI-PoweredLoad the transcript when you're ready to chat so the initial page stays lighter.
Related Videos

Mad Money 06/26/26 | Audio Only
CNBC Television

Your Agent Is Wasting Tokens and You Don't Know It - Erik Hanchett, AWS
AI Engineer

Cerebras CEO Says Capacity Is Largest Constraint Right Now
Bloomberg Technology

Portfolio Analysis in Python with QuantStats
NeuralNine

The most loved stock on Wall Street
BNN Bloomberg

Public cloud vs. on-prem: Summit on where each workload belongs
The New Stack

Docker Explained in 6 Minutes (for beginners)
corbin