When migrating a Python-based AI application from the Vertex AI SDK to the Gen AI SDK, I made an interesting discovery: the Gen AI SDK uses a Pydantic-based response model (GenerateContentResponse
), which means you can serialize it with model_dump()
or model_dump_json()
.
For anyone unfamiliar with the current landscape, it can be confusing that Google offers multiple official SDKs for working with the Gemini API. Below is some background before we dive in. At the moment, Gemini exposes two main APIs and three Python SDKs.
Gemini APIs
1. Gemini API in Vertex AI
- Access Gemini models via Google Cloud’s Vertex AI
- Requires a Google Cloud project
- IAM-based authentication and access control
- Per-project quotas that throttle usage as needed
See: “Migrate from the Gemini Developer API to the Vertex AI Gemini API” (Google Cloud Docs)
2. Gemini Developer API
- Access Gemini through Google AI Studio
- Works even without a Google Cloud project
- Generous free tier—ideal for learning and prototyping
- Enterprise-grade features and advanced settings are limited
See: “Get a Gemini API key | Google AI for Developers”
Official Python SDKs
Google currently maintains three Python SDKs:
1. Google Gen AI SDK (google-genai
)
- Supports both the Gemini Developer API and the Vertex AI Gemini API
- A single code base that handles API-key auth (AI Studio) and IAM auth (Vertex AI)
- Newer than the others and updated most frequently
Docs: https://googleapis.github.io/python-genai/
2. Vertex AI SDK (google-cloud-aiplatform
)
- Dedicated to the Gemini API in Vertex AI
- Lets you use Gemini models through Vertex AI
Repo: https://github.com/googleapis/python-aiplatform
3. Google AI Python SDK (google-generativeai
)
- Targets the Gemini Developer API only
- Does not work with Vertex AI
- Now deprecated; scheduled for EOL at the end of August 2025
Repo: https://github.com/google-gemini/deprecated-generative-ai-python
How the Response Models Differ
Both the Vertex AI SDK and the Gen AI SDK can call the Gemini API in Vertex AI, but their usage patterns differ slightly. Below are minimal examples.
Example with the Vertex AI SDK
from vertexai.generative_models import GenerativeModel
import vertexai
PROJECT_ID = "*****************"
REGION = "us-central1"
vertexai.init(project=PROJECT_ID, location=REGION)
model = GenerativeModel("gemini-2.0-flash")
prompt = "What is the capital of Japan?"
response = model.generate_content(prompt)
print(response.text) # -> Tokyo is the capital of Japan.
Example with the Gen AI SDK
from google import genai
from google.genai.types import HttpOptions
client = genai.Client(
vertexai=True,
project="*****************",
location="us-central1",
http_options=HttpOptions(api_version="v1"),
)
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="What is the capital of Japan?",
)
print(response.text) # -> Tokyo is the capital of Japan.
Although you still retrieve the generated text via response.text
, the underlying response object differs:
SDK | Response class |
---|---|
Vertex AI SDK | GenerationResponse |
Gen AI SDK | GenerateContentResponse |
Each response bundles the generated content plus rich metadata. If you want to dump everything to JSON—for example, to inspect intermediate artifacts—here’s how you do it with each SDK.
Serializing with the Vertex AI SDK
import json
import vertexai
from vertexai.generative_models import GenerativeModel
vertexai.init(project="*****************", location="us-central1")
model = GenerativeModel("gemini-2.0-flash")
response = model.generate_content("What is the capital of Japan?")
print(json.dumps(response.to_dict(), indent=2, ensure_ascii=False))
GenerationResponse
exposes a handy to_dict()
method.
Serializing with the Gen AI SDK
GenerateContentResponse
is built on Pydantic’s BaseModel
, so you use model_dump()
(or model_dump_json()
) instead:
from google import genai
from google.genai.types import HttpOptions
import json
client = genai.Client(
vertexai=True,
project="*****************",
location="us-central1",
http_options=HttpOptions(api_version="v1"),
)
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="What is the capital of Japan?",
)
print(json.dumps(response.model_dump(mode='json'), indent=2, ensure_ascii=False))
Tip
If you callto_dict()
on aGenerateContentResponse
, you’ll get an error—one of those “gotchas” to watch for when migrating.
Google’s migration guide and GitHub issues both mention this explicitly:
- Migration guide: Generate-content section (ai.google.dev)
- Issue tracker:
googleapis/python-genai#709
Interestingly, the Gen AI SDK surfaces more generation-time metadata than the Vertex AI SDK (though that’s unrelated to the serialization method itself).
Takeaways
-
When moving from the Vertex AI SDK to the Gen AI SDK, remember that
GenerateContentResponse
is Pydantic-based.- Serialize with
model_dump()
ormodel_dump_json()
, notto_dict()
.
- Serialize with
Some features are still exclusive to the Vertex AI SDK, so the Gen AI SDK isn’t yet a drop-in replacement for every use case.
That said, Google’s docs now recommend the Gen AI SDK, and it’s seeing the most active development—so consolidation onto this SDK feels inevitable.
Looking forward to future updates!
Top comments (0)