C.W.K.
Stream
Lesson 05 of 05 · published

Legacy SDK Migration

~12 min · migration, google-generativeai, eol

Level 0Spark
0 XP0/35 lessons0/10 achievements
0/140 XP to next level140 XP to go0% complete

If you have legacy code, port it

google-generativeai reached EOL in November 2025. It still installs and runs in many cases, but Google is no longer fixing bugs, and as model APIs evolve the legacy SDK silently rots. The migration is mechanical and worth doing in one sitting.

The shape changed

Six things change between the SDKs:

AspectLegacy (google-generativeai)New (google-genai)
Packagegoogle-generativeaigoogle-genai
Importimport google.generativeai as genaifrom google import genai
Initgenai.configure(api_key=...)genai.Client(api_key=...)
Model objectgenai.GenerativeModel('...')(none — pass model= per call)
Generatemodel.generate_content(text)client.models.generate_content(model=..., contents=...)
Streamstream=True kwarggenerate_content_stream()
Asyncgenerate_content_async()client.aio.models.generate_content()
Chatmodel.start_chat()client.chats.create(model=...)
VertexSeparate vertexai pkgClient(vertexai=True)

Migration order that actually works

  1. Pin google-genai in requirements.txt alongside the legacy one — they coexist.
  2. Add a new module that exports get_client() -> genai.Client.
  3. Migrate one call site at a time: replace model.generate_content(text) with client.models.generate_content(model=..., contents=text).
  4. Delete the legacy configure() call after the last call site is ported.
  5. Drop the legacy package from requirements.

Code

Side-by-side: the most common call·python
# ❌ BEFORE — legacy, EOL
import google.generativeai as genai
genai.configure(api_key='KEY')
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content('Tell me a story.')
print(response.text)

# ✅ AFTER — new SDK
from google import genai
client = genai.Client()
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents='Tell me a story.',
)
print(response.text)
Streaming·python
# ❌ BEFORE
response = model.generate_content('Tell me a story.', stream=True)
for chunk in response:
    print(chunk.text, end='')

# ✅ AFTER
for chunk in client.models.generate_content_stream(
    model='gemini-2.5-flash',
    contents='Tell me a story.',
):
    print(chunk.text, end='')
Async·python
# ❌ BEFORE
response = await model.generate_content_async('Hi')

# ✅ AFTER
response = await client.aio.models.generate_content(
    model='gemini-2.5-flash', contents='Hi')

External links

Exercise

Find a real (or write a small) Python script that uses google.generativeai. Port it to google-genai following the table above. Run both versions side-by-side against the same prompt; the outputs should be roughly equivalent (different generations because there's no shared seed). Confirm the new version uses an SDK that ships in 2026, not 2024.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.