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

Installation & Client Setup

~10 min · python, google-genai, client

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

The package you want is google-genai

The official Python SDK is google-genai (pip name with a hyphen, import name with a dot). It replaces the legacy google-generativeai package, which reached end-of-life in November 2025. If a tutorial uses import google.generativeai as genai, it's pre-EOL and the surface has changed underneath it.

Install

One main package; one optional extra for faster async via aiohttp:

Three imports you'll use everywhere

  • from google import genai — the client factory.
  • from google.genai import types — config dataclasses (GenerateContentConfig, Tool, Part, HarmCategory, etc.).
  • from google.genai import errors — exception classes (APIError, ClientError, ServerError).

Client construction patterns

Three common shapes:

  1. Implicit envgenai.Client() reads GEMINI_API_KEY or GOOGLE_API_KEY.
  2. Explicit keygenai.Client(api_key='...'). Useful in tests or when juggling multiple keys.
  3. Vertex AIgenai.Client(vertexai=True, project='...', location='...').

One client instance handles both sync and async — the async surface lives at client.aio.models and client.aio.chats.

Code

Install·bash
pip install google-genai

# Optional: faster async via aiohttp instead of httpx
pip install 'google-genai[aiohttp]'
Client construction·python
from google import genai
from google.genai import types

# 1. Reads GEMINI_API_KEY (or GOOGLE_API_KEY) from env — most common
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. Pin a specific API version (defaults to v1beta)
client = genai.Client(
    http_options=types.HttpOptions(api_version='v1'),
)
Sync and async share one client·python
client = genai.Client()

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

# Async — same 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

Create a fresh venv, install google-genai[aiohttp], and write a script with two functions: sync_hello() and async_hello(). Each makes one generate_content call to gemini-2.5-flash and prints the response text. Run the async one inside asyncio.run(). Confirm both work end-to-end.

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.