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.Indicates whether the client should use the Vertex AI API endpoints. Defaults to False (uses Gemini Developer API endpoints).
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.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.
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.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.Configuration settings that control network behavior of the client. This is typically used when running test code.
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
Access to the Models API for content generation, embeddings, and model management.See generate_content for details.
aio
Access to the asynchronous client for non-blocking operations.See AsyncClient for details.
chats
Access to multi-turn conversation functionality.See chats.create for details.
files
Access to the Files API for uploading and managing media files.See files.upload for details.
caches
Access to the Caches API for context caching.See caches.create for details.
file_search_stores
Access to the File Search Stores API for semantic search.
batches
Access to the Batches API for batch processing.See batches.create for details.
tunings
Access to the Tunings API for model fine-tuning.See tunings.tune for details.
auth_tokens
Access to authentication token management.
operations
Access to long-running operations management.
interactions
Access to the experimental Interactions API for live, streaming interactions.
vertexai
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.The base URL for the AI platform service endpoint.
Specifies the version of the API to use.
Additional HTTP headers to be sent with each request.
Timeout for requests in milliseconds.
Arguments passed to the HTTP client.
Arguments passed to the async HTTP client.
Extra parameters to add to the request body. The structure must match the backend API’s request structure:
HTTP retry options for requests. See HttpRetryOptions.
A custom httpx client to be used for requests.
A custom httpx async client to be used for requests.
A custom aiohttp client session to be used for requests.
HttpRetryOptions
Configuration options for HTTP request retries.Maximum number of attempts, including the original request. If 0 or 1, it means no retries.
Initial delay before the first retry, in seconds.
Maximum delay between retries, in seconds.
Multiplier by which the delay increases after each attempt.
Randomness factor for the delay.
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
- AsyncClient - Asynchronous client for non-blocking operations
- generate_content - Generate content with models
- chats.create - Multi-turn conversations
- files.upload - Upload and manage media files