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

설치와 client 세팅

~10 min · python, google-genai, client

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

원하는 패키지는 google-genai

공식 Python SDK 는 google-genai (pip 이름은 hyphen, import 이름은 dot). legacy google-generativeai 패키지를 대체해 — 그건 2025 년 11 월 EOL. 튜토리얼이 import google.generativeai as genai 쓴다면 그건 EOL 이전이고 surface 가 그 밑에서 바뀌었어.

설치

main 패키지 한 개 + aiohttp 통한 더 빠른 async 위한 optional extra:

어디서나 쓸 import 세 개

  • from google import genai — client factory.
  • from google.genai import types — config dataclass (GenerateContentConfig, Tool, Part, HarmCategory 등).
  • from google.genai import errors — exception class (APIError, ClientError, ServerError).

Client 생성 패턴

흔한 모양 세 가지:

  1. Implicit envgenai.Client()GEMINI_API_KEY 또는 GOOGLE_API_KEY 읽음.
  2. Explicit keygenai.Client(api_key='...'). 테스트나 여러 key 다룰 때 유용.
  3. Vertex AIgenai.Client(vertexai=True, project='...', location='...').

Client instance 하나가 sync 와 async 둘 다 핸들 — async surface 는 client.aio.modelsclient.aio.chats 에 살아.

Code

설치·bash
pip install google-genai

# Optional: httpx 대신 aiohttp 통한 더 빠른 async
pip install 'google-genai[aiohttp]'
Client 생성·python
from google import genai
from google.genai import types

# 1. env 의 GEMINI_API_KEY (또는 GOOGLE_API_KEY) 읽음 — 가장 흔함
client = genai.Client()

# 2. Explicit key
client = genai.Client(api_key='AIzaSy...')

# 3. Vertex AI
client = genai.Client(
    vertexai=True,
    project='my-project',
    location='us-central1',
)

# 4. 특정 API 버전 pin (default 는 v1beta)
client = genai.Client(
    http_options=types.HttpOptions(api_version='v1'),
)
Sync 와 async 가 client 하나 공유·python
client = genai.Client()

# Sync
response = client.models.generate_content(
    model='gemini-2.5-flash', contents='Hi')
print(response.text)

# Async — 같은 client, .aio prefix
import asyncio

async def main():
    r = await client.aio.models.generate_content(
        model='gemini-2.5-flash', contents='Hi')
    print(r.text)

asyncio.run(main())

External links

Exercise

fresh venv 만들고 google-genai[aiohttp] 설치, 함수 두 개 있는 스크립트 작성: sync_hello(), async_hello(). 각자 gemini-2.5-flashgenerate_content 한 호출 하고 응답 텍스트 출력. async 거 asyncio.run() 안에서 실행. 둘 다 end-to-end 동작 확인.

Progress

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

댓글 0

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

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