Implementation:Googleapis Python genai Client Init
| Knowledge Sources | |
|---|---|
| Domains | API_Client, Authentication |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for establishing authenticated client connections to Google Gemini and Vertex AI endpoints provided by the google-genai SDK.
Description
The Client class is the main entry point for the google-genai SDK. It supports two backends: the Gemini Developer API (authenticated via API key) and Vertex AI (authenticated via Google Cloud credentials, project, and location). Upon initialization, it composes sub-modules for models, chats, files, caches, tunings, batches, tokens, operations, and live streaming, all sharing the same authenticated transport.
Usage
Import and instantiate this class as the first step in any google-genai workflow. Use api_key for quick prototyping with the Gemini Developer API, or set vertexai=True with project and location for production Vertex AI deployments.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/client.py
- Lines: L312-445
Signature
class Client:
def __init__(
self,
*,
vertexai: Optional[bool] = None,
api_key: Optional[str] = None,
credentials: Optional[google.auth.credentials.Credentials] = None,
project: Optional[str] = None,
location: Optional[str] = None,
debug_config: Optional[DebugConfig] = None,
http_options: Optional[Union[HttpOptions, HttpOptionsDict]] = None,
) -> None:
"""
Args:
vertexai: If True, use Vertex AI endpoints. If False/None, use Gemini Developer API.
api_key: API key for the Gemini Developer API.
credentials: Google Cloud credentials for Vertex AI.
project: Google Cloud project ID (required for Vertex AI).
location: Google Cloud location (required for Vertex AI).
debug_config: Debug configuration options.
http_options: HTTP transport configuration overrides.
"""
Import
from google import genai
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| vertexai | Optional[bool] | No | If True, connect to Vertex AI; if False/None, connect to Gemini Developer API |
| api_key | Optional[str] | No* | API key for Gemini Developer API (*required if vertexai is not True) |
| credentials | Optional[google.auth.credentials.Credentials] | No | Google Cloud credentials for Vertex AI |
| project | Optional[str] | No* | GCP project ID (*required for Vertex AI) |
| location | Optional[str] | No* | GCP location (*required for Vertex AI) |
| debug_config | Optional[DebugConfig] | No | Debug settings |
| http_options | Optional[Union[HttpOptions, HttpOptionsDict]] | No | HTTP transport overrides (timeout, headers, etc.) |
Outputs
| Name | Type | Description |
|---|---|---|
| Client instance | Client | Authenticated client with sub-modules: .models, .chats, .files, .caches, .tunings, .batches, .tokens, .operations, .live, .aio |
Usage Examples
Gemini Developer API (API Key)
from google import genai
# Initialize with API key for Gemini Developer API
client = genai.Client(api_key="YOUR_API_KEY")
# Access sub-modules
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="Hello, world!"
)
print(response.text)
Vertex AI (Google Cloud Credentials)
from google import genai
# Initialize for Vertex AI
client = genai.Client(
vertexai=True,
project="my-gcp-project",
location="us-central1"
)
# Same API surface as Gemini Developer API
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="Explain quantum computing."
)
print(response.text)
Related Pages
Implements Principle
Requires Environment
- Environment:Googleapis_Python_genai_Python_3_10_SDK_Runtime
- Environment:Googleapis_Python_genai_Gemini_API_Key_Authentication
- Environment:Googleapis_Python_genai_Vertex_AI_Service_Account