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

Legacy SDK 마이그레이션

~12 min · migration, legacy, typescript

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

@google/generative-ai 코드 있으면 port

Legacy @google/generative-ai 가 2025 년 8 월 EOL. 마이그레이션은 mechanical 이고 새 패키지가 같은 문제를 더 깔끔한 모양으로 해결.

여섯 가지가 변함

AspectLegacyNew
클래스GoogleGenerativeAIGoogleGenAI
Import@google/generative-ai@google/genai
ModelgenAI.getGenerativeModel({model})(없음 — 호출당 model pass)
Generatemodel.generateContent(text)ai.models.generateContent({model, contents})
Response textresult.response.text() (method)response.text (property)
Chatmodel.startChat()ai.chats.create({model})
Files별도 FileManagerai.files.upload(...)
Vertex별도 @google-cloud/vertexaiGoogleGenAI({vertexai: true})

.text() vs .text 버그

port 할 때 가장 큰 함정 한 개: legacy 는 result.response.text() (method 호출) 사용. New SDK 는 response.text (property 접근). 괄호 빼는 거 잊으면 TypeError: response.text is not a function. port 할 때 코드베이스에서 .text() grep 하고 한 번에 다 고쳐.

Code

Side-by-side 마이그레이션·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 마이그레이션·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 마이그레이션·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

Legacy @google/generative-ai 코드 샘플 하나 골라 (예: 옛 Google AI cookbook) @google/genai 로 port. 위 표를 마이그레이션 체크리스트로 사용. 두 버전 실행해서 새 버전이 등가 출력 반환하는지 확인 후 package.json 에서 legacy 패키지 삭제.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.