ALL INTELLIGENCE
Google Gen AI Python SDK 发布T2·Tools

Google Releases Gen AI Python SDK Version 3.2.0

By release-please[bot]Original title: v3.2.0
42AI Score

Google released version 3.2.0 of its Gen AI Python SDK, part of the major 3.x line. The update switches the default Gemini Developer API version from v1beta to v1 and replaces the default HTTP client httpx with httpx2, which is no longer installed by default. Developers should note that the latest model aliases exist only on v1beta and can revert via environment variables or HttpOptions.

Full Report

Contents7 sections

Google Gen AI SDK


Documentation: https://googleapis.github.io/python-genai/

Google Gen AI Python SDK provides an interface for developers to integrate Google's generative models into their Python applications. It supports the Gemini Developer API and Gemini platform APIs.

[!WARNING]

Major version 3 changes:

  • Default Gemini Developer API version is now v1 (was v1beta). Gemini platform (formerly Gemini Enterprise Agent Platform and Vertex AI) clients are unaffected and still default to v1beta1.
    • The gemini-flash-latest, gemini-flash-lite-latest and gemini-pro-latest aliases exist only on v1beta. Use a specific model version, or switch back to v1beta.
    • To restore the previous default, set GOOGLE_GENAI_API_VERSION=beta (or an explicit version such as v1beta), or pass types.HttpOptions(api_version='v1beta'), which takes precedence.
  • Default HTTP client is now httpx2 (was httpx), and httpx is no longer installed by default.
    • To restore httpx as the default, do both: install google-genai[httpx] and set GOOGLE_GENAI_HTTP_CLIENT=httpx. Alternatively, pass httpx_client / httpx_async_client in HttpOptions.
    • To migrate to httpx2, import httpx2 as httpx is sufficient in most cases.
    • httpx2 verifies SSL certificates against the OS trust store instead of certifi when SSL_CERT_FILE and SSL_CERT_DIR are not set.
    • Objects passed in HttpOptions(client_args=..., async_client_args=...) (such as Timeout, Limits, transports, Auth subclasses) and transport exceptions you catch (TimeoutException, ConnectError, HTTPError) must come from the active HTTP client package.
  • interactions: CodeExecutionResultStep.result changes from string to the CodeExecutionResultStepResult union. Check which variant is present and read the string from it. The JSON for text-only results is unchanged.

To avoid unexpected updates, pin the SDK version to < 3.0.0.

Agent Skills

Large Language Models (LLMs) and generative AI coding assistants are often trained on static datasets. As a result, they may be unaware of recent updates and suggest outdated or legacy libraries.

To ensure your AI coding helper (such as Antigravity, Claude Code, Cursor, or other IDE extensions) generates up-to-date code using the correct SDK syntax and best practices, we recommend equipping your assistant with Gemini API Skills. Loading these skills injects the correct patterns and guidelines directly into your AI assistant's context.

Depending on your target platform, use the corresponding Agent Skill repository:

Installation

BASH
pip install google-genai

<small>With uv:</small>

BASH
uv pip install google-genai

Imports

PYTHON
from google import genai
from google.genai import types

Create a client

Please run one of the following code blocks to create a client for different services (Gemini Developer API or Gemini platform).

PYTHON
from google import genai

# Only run this block for Gemini Developer API
client = genai.Client(api_key='GEMINI_API_KEY')
PYTHON
from google import genai

# Only run this block for Gemini platform
client = genai.Client(
    enterprise=True, project='your-project-id', location='global'
)

Using types

All API methods support Pydantic types and dictionaries, which you can access from google.genai.types. You can import the types module with the following:

PYTHON
from google.genai import types

Below is an example generate_content() call using Part from the types module:

PYTHON
response = client.models.generate_content(
    model='gemini-4-hydrogen',
    contents=types.Part.from_text(text='Why is the sky blue?')
)

Alternatively, you can accomplish the same request using dictionaries instead of types:

PYTHON
response = client.models.generate_content(
    model='gemini-4-hydrogen',
    contents={'text': 'Why is the sky blue?'}
)

(Optional) Using environment variables:

You can create a client by configuring the necessary environment variables. Configuration setup instructions depends on whether you're using the Gemini Developer API or the Gemini API in Gemini platform.

Gemini Developer API: Set the GEMINI_API_KEY or GOOGLE_API_KEY. It will automatically be picked up by the client. It's recommended that you set only one of those variables, but if both are set, GOOGLE_API_KEY takes precedence.

BASH
export GEMINI_API_KEY='your-api-key'

Gemini API in Gemini platform: Set GOOGLE_GENAI_USE_ENTERPRISE, GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION, as shown below:

BASH
export GOOGLE_GENAI_USE_ENTERPRISE=true
export GOOGLE_CLOUD_PROJECT='your-project-id'
export GOOGLE_CLOUD_LOCATION='global'
PYTHON
from google import genai

client = genai.Client()

Close a client

Explicitly close the sync client to ensure that resources, such as the underlying HTTP connections, are properly cleaned up and closed.

PYTHON
from google.genai import Client

client = Client()
response_1 = client.models.generate_content(
    model=MODEL_ID,
    contents='Hello',
)
response_2 = client.models.generate_content(
    model=MODEL_ID,
    contents='Ask a question',
)
# Close the sync client to release resources.
client.close()

To explicitly close the async client:

PYTHON
from google.genai import Client

aclient = Client(
    enterprise=True, project='my-project-id', location='global'
).aio
response_1 = await aclient.models.generate_content(
    model=MODEL_ID,
    contents='Hello',
)
response_2 = await aclient.models.generate_content(
    model=MODEL_ID,
    contents='Ask a question',
)
# Close the async client to release resources.
await aclient.aclose()

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)