Overview
TheClient 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
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.Indicates whether the client should use the Vertex AI API endpoints.
Defaults to False (uses Gemini Developer API endpoints).
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.
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.The location to send API requests to (e.g.,
us-central1).
Can be obtained from environment variables. Applies to the Vertex AI API only.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 theGEMINI_API_KEY or GOOGLE_API_KEY environment variable:
If both
GOOGLE_API_KEY and GEMINI_API_KEY are set, GOOGLE_API_KEY takes precedence.Vertex AI API
Set the required environment variables:Sync and Async Clients
The SDK provides both synchronous and asynchronous interfaces.Synchronous Client
The defaultClient 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 usingaclose():
Client Properties
The client provides access to various API resources:client.models- Model operations (generate content, count tokens, etc.)client.chats- Chat session managementclient.files- File upload and management (Gemini API only)client.caches- Cached content managementclient.tunings- Model tuning operations (Vertex AI only)client.batches- Batch prediction jobsclient.operations- Long-running operation managementclient.aio- Async client interfaceclient.vertexai- Boolean indicating if using Vertex AI API