Skip to main content
The Client class is the main entry point for making synchronous requests to the Gemini Developer API or Vertex AI API. It provides access to all SDK features including models, files, caches, tunings, and more.

Constructor

Create a new client instance for either the Gemini API or Vertex AI API.
bool
default:"False"
Indicates whether the client should use the Vertex AI API endpoints. Defaults to False (uses Gemini Developer API endpoints).
str
The API key to use for authentication. Applies to the Gemini Developer API only. Can also be set via the GOOGLE_API_KEY environment variable.
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. For more information, see Set up Application 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. Find your project ID.
str
The location to send API requests to (e.g., us-central1). Can be obtained from environment variables (e.g., GOOGLE_CLOUD_LOCATION). Applies to the Vertex AI API only.
DebugConfig
Configuration settings that control network behavior of the client. This is typically used when running test code.
types.HttpOptions | dict
HTTP options to use for the client. These options will be applied to all requests made by the client.See HttpOptions for available fields.

Usage Examples

Gemini Developer API

Vertex AI API

With HTTP Options

Using Context Manager

Properties

models

Models
Access to the Models API for content generation, embeddings, and model management.See generate_content for details.

aio

AsyncClient
Access to the asynchronous client for non-blocking operations.See AsyncClient for details.

chats

Chats
Access to multi-turn conversation functionality.See chats.create for details.

files

Files
Access to the Files API for uploading and managing media files.See files.upload for details.

caches

Caches
Access to the Caches API for context caching.See caches.create for details.

file_search_stores

FileSearchStores
Access to the File Search Stores API for semantic search.

batches

Batches
Access to the Batches API for batch processing.See batches.create for details.

tunings

Tunings
Access to the Tunings API for model fine-tuning.See tunings.tune for details.

auth_tokens

Tokens
Access to authentication token management.

operations

Operations
Access to long-running operations management.

interactions

InteractionsResource
Access to the experimental Interactions API for live, streaming interactions.
This API is experimental and may change in future versions.

vertexai

bool
Returns whether the client is using the Vertex AI API.

Methods

close()

Closes the synchronous client explicitly and releases resources.
This method does not close the async client. Use client.aio.aclose() or the async context manager to close the async client.

Context Manager Methods

The Client supports the context manager protocol for automatic resource cleanup.

__enter__()

Enters the runtime context and returns the client.

__exit__(exc_type, exc_value, traceback)

Exits the runtime context and closes the client.

HttpOptions

HTTP configuration options for customizing client behavior.
str
The base URL for the AI platform service endpoint.
str
Specifies the version of the API to use.
dict[str, str]
Additional HTTP headers to be sent with each request.
int
Timeout for requests in milliseconds.
dict[str, Any]
Arguments passed to the HTTP client.
dict[str, Any]
Arguments passed to the async HTTP client.
dict[str, Any]
Extra parameters to add to the request body. The structure must match the backend API’s request structure:
HttpRetryOptions
HTTP retry options for requests. See HttpRetryOptions.
httpx.Client
A custom httpx client to be used for requests.
httpx.AsyncClient
A custom httpx async client to be used for requests.
aiohttp.ClientSession
A custom aiohttp client session to be used for requests.

HttpRetryOptions

Configuration options for HTTP request retries.
int
default:"5"
Maximum number of attempts, including the original request. If 0 or 1, it means no retries.
float
default:"1.0"
Initial delay before the first retry, in seconds.
float
default:"60.0"
Maximum delay between retries, in seconds.
float
default:"2.0"
Multiplier by which the delay increases after each attempt.
float
default:"1.0"
Randomness factor for the delay.
list[int]
List of HTTP status codes that should trigger a retry. If not specified, a default set of retryable codes (408, 429, and 5xx) may be used.

See Also