If you’ve been working with Gemini, you’ve likely encountered the two separate client libraries for Gemini: the Gemini library for Google AI vs. Vertex AI in Google Cloud. Even though the two libraries are quite similar, there are slight differences that make the two libraries non-interchangeable.
I usually start my experiments in Google AI and when it is time to switch to Vertex AI on Google Cloud, I couldn’t simply copy and paste my code. I had to go through updating my Google AI libraries to Vertex AI libraries. It wasn’t difficult but it was quite annoying.
Thankfully, the release of the new Google Gen AI SDK addresses my biggest complaint about Gemini. This is the new unified library for Gemini 2.0 and it works against Google AI and Vertex AI for Gemini 2.0 (and also Gemini 1.5).
Let’s go through a quick example to see how it works.
Google AI
First, let’s install the new Google Gen AI SDK:
pip install google-genai
Get an API key on Google AI and create a client with that API key:
client = genai.Client(api_key='your-google-ai-api-key')
Generate some content:
response = client.models.generate_content(
model='gemini-2.0-flash-exp', contents='Why is sky blue?'
)
print(response.text)
So far, nothing out of the ordinary. However, let’s now try talking to Gemini on Vertex AI.
Vertex AI
For Vertex AI, you need a Google Cloud project and also pick a location for your Gen AI calls.
You still create a client but this time, you pass in the vertexai=True
flag. You also pass in your Google Cloud project
id and location:
client = genai.Client(
vertexai=True, project='your-google-cloud-project-id', location='us-central1'
)
The content generation is exactly the same:
response = client.models.generate_content(
model='gemini-2.0-flash-exp', contents='Why is sky blue?'
)
print(response.text)
Nice!
Conclusion
As you’ve seen, it’s quite easy now to talk to Gemini on Google AI or Vertex AI with the same library. It’s just a matter of configuring the client with the right parameters. If you want to try it out, I have a tutorial with full code along with links for further reading:
- Tutorial: Gemini2 on Google AI and Vertex AI with Google Gen AI SDK
- Google Gen AI SDKs
- Google Gen AI SDK Python
- Developer’s guide to getting started with Gemini 2.0 Flash on Vertex AI