Skip to main content

Overview

The Client class is the main entry point for interacting with Google’s Gen AI models. It supports both the Gemini Developer API and Vertex AI API, providing synchronous and asynchronous operations.

Importing the Client

Client Initialization

Gemini Developer API

Initialize the client with an API key:

Vertex AI API

Initialize the client with project and location:

Client Parameters

str
The API key to use for authentication. Applies to the Gemini Developer API only. Can also be set via the GOOGLE_API_KEY or GEMINI_API_KEY environment variable.
bool
default:"False"
Indicates whether the client should use the Vertex AI API endpoints. Defaults to False (uses Gemini Developer API endpoints).
google.auth.credentials.Credentials
The credentials to use for authentication when calling the Vertex AI APIs. Credentials can be obtained from environment variables and default credentials. Applies to the Vertex AI API only.
str
The Google Cloud project ID to use for quota. Can be obtained from environment variables (e.g., GOOGLE_CLOUD_PROJECT). Applies to the Vertex AI API only.
str
The location to send API requests to (e.g., us-central1). Can be obtained from environment variables. Applies to the Vertex AI API only.
types.HttpOptions | dict
HTTP options to use for the client. These options will be applied to all requests made by the client.

Using Environment Variables

Gemini Developer API

Set the GEMINI_API_KEY or GOOGLE_API_KEY environment variable:
Then create the client without explicit parameters:
If both GOOGLE_API_KEY and GEMINI_API_KEY are set, GOOGLE_API_KEY takes precedence.

Vertex AI API

Set the required environment variables:
Then create the client:

Sync and Async Clients

The SDK provides both synchronous and asynchronous interfaces.

Synchronous Client

The default Client instance provides synchronous operations:

Asynchronous Client

Access the async client using the .aio property:

Context Managers

Context managers ensure proper resource cleanup when the client is no longer needed.

Synchronous Context Manager

The sync client context manager automatically closes the underlying sync client:
Using the context manager prevents “client has been closed” errors by automatically releasing resources when exiting the with block.

Asynchronous Context Manager

The async client context manager automatically closes the underlying async client:

Closing Clients

Closing the Sync Client

Explicitly close the sync client to release resources:

Closing the Async Client

Explicitly close the async client using aclose():
When you close the sync client with client.close(), it doesn’t close the async client. Similarly, when you close the async client with await client.aio.aclose(), it doesn’t close the sync client. Each must be closed separately if both are used.

Client Properties

The client provides access to various API resources:
  • client.models - Model operations (generate content, count tokens, etc.)
  • client.chats - Chat session management
  • client.files - File upload and management (Gemini API only)
  • client.caches - Cached content management
  • client.tunings - Model tuning operations (Vertex AI only)
  • client.batches - Batch prediction jobs
  • client.operations - Long-running operation management
  • client.aio - Async client interface
  • client.vertexai - Boolean indicating if using Vertex AI API

HTTP Options

Customize client behavior with HTTP options:

API Version Selection

By default, the SDK uses beta API endpoints. To use stable v1 endpoints:

Vertex AI

Gemini Developer API