본문 바로가기
C.W.K.
Stream
Lesson 04 of 04 · published

예전 SDK에서 이전하기

~12 min · migration, legacy, typescript

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

@google/generative-ai 코드가 있다면 옮겨

예전 @google/generative-ai는 2025년 8월에 수명이 끝났어. 이전은 대부분 기계적인 작업이고, 새 패키지는 같은 문제를 더 깔끔한 구조로 풀어.

달라진 지점

항목예전 SDK새 SDK
클래스GoogleGenerativeAIGoogleGenAI
Import@google/generative-ai@google/genai
모델genAI.getGenerativeModel({model})없음 — 호출할 때마다 model 전달
생성model.generateContent(text)ai.models.generateContent({model, contents})
응답 텍스트result.response.text()(메서드)response.text(속성)
채팅model.startChat()ai.chats.create({model})
파일별도 FileManagerai.files.upload(...)
Vertex별도 @google-cloud/vertexaiGoogleGenAI({vertexai: true})

.text()와 .text의 함정

이전할 때 가장 큰 함정은 하나야. 예전 SDK는 메서드인 result.response.text()를 쓰고, 새 SDK는 속성인 response.text를 써. 괄호를 빼지 않으면 TypeError: response.text is not a function이 나. 코드를 옮길 때 전체 코드베이스에서 .text()를 검색해 한 번에 고쳐.

Code

이전 전후를 나란히 비교하기·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
채팅 이전·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);
스트리밍 이전·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

예전 @google/generative-ai 코드 예제 하나를 골라 새 @google/genai로 옮겨. 옛 Google AI 쿡북 예제도 좋아. 위 표를 점검표로 쓰고, 두 버전을 실행해 새 버전이 같은 뜻의 출력을 내는지 확인한 뒤 package.json에서 예전 패키지를 삭제해.

Progress

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

댓글 0

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

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