@@ -42,6 +42,12 @@ class Client:
4242 def __init__ (
4343 self ,
4444 api_key : Optional [str ] = None ,
45+ vertexai : Optional [bool ] = None ,
46+ credentials : Optional [Any ] = None ,
47+ project : Optional [str ] = None ,
48+ location : Optional [str ] = None ,
49+ debug_config : Optional [Any ] = None ,
50+ http_options : Optional [Any ] = None ,
4551 posthog_client : Optional [PostHogClient ] = None ,
4652 posthog_distinct_id : Optional [str ] = None ,
4753 posthog_properties : Optional [Dict [str , Any ]] = None ,
@@ -51,7 +57,13 @@ def __init__(
5157 ):
5258 """
5359 Args:
54- api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY or API_KEY environment variable
60+ api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY or API_KEY environment variable (not required for Vertex AI)
61+ vertexai: Whether to use Vertex AI authentication
62+ credentials: Vertex AI credentials object
63+ project: GCP project ID for Vertex AI
64+ location: GCP location for Vertex AI
65+ debug_config: Debug configuration for the client
66+ http_options: HTTP options for the client
5567 posthog_client: PostHog client for tracking usage
5668 posthog_distinct_id: Default distinct ID for all calls (can be overridden per call)
5769 posthog_properties: Default properties for all calls (can be overridden per call)
@@ -66,6 +78,12 @@ def __init__(
6678
6779 self .models = Models (
6880 api_key = api_key ,
81+ vertexai = vertexai ,
82+ credentials = credentials ,
83+ project = project ,
84+ location = location ,
85+ debug_config = debug_config ,
86+ http_options = http_options ,
6987 posthog_client = self ._ph_client ,
7088 posthog_distinct_id = posthog_distinct_id ,
7189 posthog_properties = posthog_properties ,
@@ -85,6 +103,12 @@ class Models:
85103 def __init__ (
86104 self ,
87105 api_key : Optional [str ] = None ,
106+ vertexai : Optional [bool ] = None ,
107+ credentials : Optional [Any ] = None ,
108+ project : Optional [str ] = None ,
109+ location : Optional [str ] = None ,
110+ debug_config : Optional [Any ] = None ,
111+ http_options : Optional [Any ] = None ,
88112 posthog_client : Optional [PostHogClient ] = None ,
89113 posthog_distinct_id : Optional [str ] = None ,
90114 posthog_properties : Optional [Dict [str , Any ]] = None ,
@@ -94,7 +118,13 @@ def __init__(
94118 ):
95119 """
96120 Args:
97- api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY or API_KEY environment variable
121+ api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY or API_KEY environment variable (not required for Vertex AI)
122+ vertexai: Whether to use Vertex AI authentication
123+ credentials: Vertex AI credentials object
124+ project: GCP project ID for Vertex AI
125+ location: GCP location for Vertex AI
126+ debug_config: Debug configuration for the client
127+ http_options: HTTP options for the client
98128 posthog_client: PostHog client for tracking usage
99129 posthog_distinct_id: Default distinct ID for all calls
100130 posthog_properties: Default properties for all calls
@@ -113,16 +143,40 @@ def __init__(
113143 self ._default_privacy_mode = posthog_privacy_mode
114144 self ._default_groups = posthog_groups
115145
116- # Handle API key - try parameter first, then environment variables
117- if api_key is None :
118- api_key = os .environ .get ("GOOGLE_API_KEY" ) or os .environ .get ("API_KEY" )
146+ # Build genai.Client arguments
147+ client_args = {}
148+
149+ # Add Vertex AI parameters if provided
150+ if vertexai is not None :
151+ client_args ["vertexai" ] = vertexai
152+ if credentials is not None :
153+ client_args ["credentials" ] = credentials
154+ if project is not None :
155+ client_args ["project" ] = project
156+ if location is not None :
157+ client_args ["location" ] = location
158+ if debug_config is not None :
159+ client_args ["debug_config" ] = debug_config
160+ if http_options is not None :
161+ client_args ["http_options" ] = http_options
162+
163+ # Handle API key authentication
164+ if vertexai :
165+ # For Vertex AI, api_key is optional
166+ if api_key is not None :
167+ client_args ["api_key" ] = api_key
168+ else :
169+ # For non-Vertex AI mode, api_key is required (backwards compatibility)
170+ if api_key is None :
171+ api_key = os .environ .get ("GOOGLE_API_KEY" ) or os .environ .get ("API_KEY" )
119172
120- if api_key is None :
121- raise ValueError (
122- "API key must be provided either as parameter or via GOOGLE_API_KEY/API_KEY environment variable"
123- )
173+ if api_key is None :
174+ raise ValueError (
175+ "API key must be provided either as parameter or via GOOGLE_API_KEY/API_KEY environment variable"
176+ )
177+ client_args ["api_key" ] = api_key
124178
125- self ._client = genai .Client (api_key = api_key )
179+ self ._client = genai .Client (** client_args )
126180 self ._base_url = "https://generativelanguage.googleapis.com"
127181
128182 def _merge_posthog_params (
0 commit comments