Background
Recently, the LINE business card assistant robot (linebot-namecard-python) deployed on Google Cloud Run suddenly went down. After checking the logs with gcloud logging read, the following ruthless error appeared:
google.api_core.exceptions.ResourceExhausted: 429 Your billing account has exceeded its monthly spending cap.
It turned out that we used the API Key provided by Google AI Studio (google.generativeai package) for rapid development, and as a result, we silently maxed out the monthly free quota.
As a developer who needs to launch a service, it's time to "level up" the architecture and migrate the model calls to the enterprise-grade Google Cloud Vertex AI, directly using GCP's IAM permissions and billing system. This article will share the migration process and the various pitfalls encountered along the way.
Technical Upgrade: From AI Studio to Vertex AI
To migrate a project from the Google AI Studio SDK to Vertex AI, there are three main steps:
Replace the dependency package: In
requirements.txt, remove the oldgoogle.generativeaiand replace it withgoogle-cloud-aiplatform.Update environment variable settings: In
config.py, we no longer needGEMINI_API_KEY, but instead use GCP'sPROJECT_IDandLOCATION:
PROJECT_ID = os.getenv("PROJECT_ID", None)
LOCATION = os.getenv("LOCATION", "global") # Default to global
- Core code rewriting (gemini_utils.py): Although the SDK interface of Vertex AI is similar, the handling of multimodal data (such as images) is slightly stricter. We need to convert
PIL.Imageto thevertexai.generative_models.Partformat:
Pitfall 1: Residual Old SDK Causing Cloud Run Startup Failure
Happily, I updated the environment variables with gcloud run services update, but the Cloud Run deployment failed, and the container couldn't even start.
After checking the logs, I found:
ModuleNotFoundError: No module named 'google.generativeai'
Reason: Although gemini_utils.py has been rewritten, the main program app/main.py still contains import google.generativeai as genai and the initialization code genai.configure(api_key=...). Since the package has been removed from requirements.txt, the container will naturally fail to find the module and crash during startup.
Solution: Globally grep the project, completely remove all references to the old SDK, and then repackage the Docker image using Cloud Build and push it again.
Pitfall 2: Vertex AI Model Name and Region Restrictions (404 Not Found)
The code is cleaned up, and the container also starts successfully, but when I send a business card image on LINE, the robot throws a 500 error. After reviewing the logs again, this time it's:
google.api_core.exceptions.NotFound: 404 Publisher Model ... gemini-1.5-flash was not found or your project does not have access to it.
This is the biggest pit I encountered this time! In Google AI Studio, you can casually use the alias gemini-1.5-flash; but in certain regions of Vertex AI (such as asia-east1 Taiwan), you must specify the exact version number, such as gemini-1.5-flash-002, otherwise the API will directly tell you that the model cannot be found.
Advanced Challenge: I want to try Gemini 3.0 Flash Preview!
To solve this problem, I had an idea. Since I'm going to change it, why not upgrade directly to the latest gemini-3-flash-preview!
As a result, I wrote a test script and found:
- ❌
asia-east1(Taiwan): 404 Not Found - ❌
us-central1(Central US): 404 Not Found - ✅
global(Global): SUCCESS!
That's right, currently this preview model on Vertex AI is only available in the global region.
Final Solution:
- Change the default region in
config.pytoglobal. - Call
vertexai.init(project="line-vertex", location="global"). - The Cloud Run environment variable
--update-env-vars="LOCATION=global"must also be aligned.
Summary: Changes Brought by Vertex AI
After some effort, the business card robot finally came back to life and used the latest Gemini 3 Flash model. After migrating from AI Studio to Vertex AI, several significant benefits have been brought:
- Get rid of Quota Anxiety: No longer limited by AI Studio's free quota or Spending Cap, directly deduct through GCP billing, suitable for production environments.
- Security Enhancement: Removed the plaintext API Key in the environment variables and used GCP's Default Application Credentials (IAM) for authentication, making the architecture more secure.
- Stability: Enterprise-grade SLA guarantee.
This experience also reminded me that when using Vertex AI on GCP, you must first check the official documentation to confirm the correspondence between "Region" and "Model Name" to avoid being overwhelmed by 404 errors after deployment.
If you also have a project that is about to move from AI Studio to Vertex AI, I hope this pitfall record can help you avoid some detours!


Top comments (0)