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

Legacy SDK Migration

~12 min · migration, legacy, typescript

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

If you have @google/generative-ai code, port it

The legacy @google/generative-ai reached EOL in August 2025. The migration is mechanical and the new package fits the same problem with cleaner shape.

Six things change

AspectLegacyNew
ClassGoogleGenerativeAIGoogleGenAI
Import@google/generative-ai@google/genai
ModelgenAI.getGenerativeModel({model})(none — pass model per call)
Generatemodel.generateContent(text)ai.models.generateContent({model, contents})
Response textresult.response.text() (method)response.text (property)
Chatmodel.startChat()ai.chats.create({model})
FilesSeparate FileManagerai.files.upload(...)
VertexSeparate @google-cloud/vertexaiGoogleGenAI({vertexai: true})

The .text() vs .text bug

The single biggest porting gotcha: legacy used result.response.text() (method call). The new SDK uses response.text (property access). Forgetting to drop the parentheses gives you TypeError: response.text is not a function. Grep your codebase for .text() when porting and fix all of them at once.

Code

Side-by-side migration·typescript
// ❌ BEFORE — legacy, EOL Aug 2025
import { GoogleGenerativeAI } from '@google/generative-ai';

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
const result = await model.generateContent('Tell me a story.');
console.log(result.response.text());  // METHOD CALL

// ✅ AFTER — new SDK
import { GoogleGenAI } from '@google/genai';

const ai = new GoogleGenAI({ apiKey: process.env.GOOGLE_API_KEY! });
const response = await ai.models.generateContent({
  model: 'gemini-2.5-flash',
  contents: 'Tell me a story.',
});
console.log(response.text);  // PROPERTY
Chat migration·typescript
// ❌ BEFORE
const chat = model.startChat({ history: [...] });
const r = await chat.sendMessage('Hi');
console.log(r.response.text());

// ✅ AFTER
const chat = ai.chats.create({
  model: 'gemini-2.5-flash',
  history: [...],  // optional, same shape
});
const r = await chat.sendMessage({ message: 'Hi' });
console.log(r.text);
Streaming migration·typescript
// ❌ BEFORE
const result = await model.generateContentStream('Story');
for await (const chunk of result.stream) {
  console.log(chunk.text());
}

// ✅ AFTER
const stream = await ai.models.generateContentStream({
  model: 'gemini-2.5-flash',
  contents: 'Story',
});
for await (const chunk of stream) {
  if (chunk.text) process.stdout.write(chunk.text);
}

External links

Exercise

Pick one legacy @google/generative-ai code sample (e.g. from the old Google AI cookbook) and port it to @google/genai. Use the table above as your migration checklist. Run both versions, confirm the new one returns equivalent output, then delete the legacy package from package.json.

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.