Skip to content

Commit 037ec12

Browse files
DeanChensjcopybara-github
authored andcommitted
feat(models): Add configuration options to Gemini class
Allows passing enterprise, vertexai, api_key, credentials, project, and location directly to the Gemini class constructor, avoiding the need for environment variables or subclassing. These parameters are passed through to the underlying google.genai.Client. Imported from GitHub PR google#3813 (with modifications to support 'enterprise' parameter and preserve auto-detection). Merges google#3813 Co-authored-by: Shangjie Chen <deanchen@google.com> PiperOrigin-RevId: 934084452
1 parent 0e263f1 commit 037ec12

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

src/google/adk/models/google_llm.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from google.genai import types
3333
from google.genai.errors import ClientError
34+
from pydantic import Field
3435
from typing_extensions import override
3536

3637
from ..utils._google_client_headers import get_tracking_headers
@@ -114,6 +115,11 @@ def api_client(self) -> Client:
114115

115116
model: str = 'gemini-2.5-flash'
116117

118+
client_kwargs: Optional[dict[str, Any]] = Field(
119+
default=None, exclude=True, repr=False
120+
)
121+
"""Extra arguments to pass to the google.genai.Client constructor."""
122+
117123
base_url: Optional[str] = None
118124
"""The base URL for the AI platform service endpoint."""
119125

@@ -349,6 +355,9 @@ def api_client(self) -> Client:
349355
if self.model.startswith('projects/'):
350356
kwargs['enterprise'] = True
351357

358+
if self.client_kwargs:
359+
kwargs.update(self.client_kwargs)
360+
352361
return Client(**kwargs)
353362

354363
@cached_property
@@ -394,6 +403,9 @@ def _live_api_client(self) -> Client:
394403
if self.model.startswith('projects/'):
395404
kwargs['enterprise'] = True
396405

406+
if self.client_kwargs:
407+
kwargs.update(self.client_kwargs)
408+
397409
return Client(**kwargs)
398410

399411
@contextlib.asynccontextmanager

tests/unittests/models/test_google_llm.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,65 @@ def test_gemini_live_api_client_creation_with_projects_prefix():
182182
assert kwargs["enterprise"] is True
183183

184184

185+
def test_gemini_api_client_creation_with_client_kwargs():
186+
mock_credentials = mock.MagicMock()
187+
model = Gemini(
188+
model="gemini-2.5-flash",
189+
client_kwargs={
190+
"enterprise": True,
191+
"project": "my-project",
192+
"location": "my-location",
193+
"api_key": "my-key",
194+
"credentials": mock_credentials,
195+
},
196+
)
197+
with mock.patch("google.genai.Client", autospec=True) as mock_client:
198+
_ = model.api_client
199+
mock_client.assert_called_once()
200+
_, kwargs = mock_client.call_args
201+
assert kwargs["enterprise"] is True
202+
assert kwargs["project"] == "my-project"
203+
assert kwargs["location"] == "my-location"
204+
assert kwargs["api_key"] == "my-key"
205+
assert kwargs["credentials"] == mock_credentials
206+
207+
with mock.patch("google.genai.Client", autospec=True) as mock_client:
208+
_ = model._live_api_client
209+
mock_client.assert_called_once()
210+
_, kwargs = mock_client.call_args
211+
assert kwargs["enterprise"] is True
212+
assert kwargs["project"] == "my-project"
213+
assert kwargs["location"] == "my-location"
214+
assert kwargs["api_key"] == "my-key"
215+
assert kwargs["credentials"] == mock_credentials
216+
217+
218+
def test_gemini_serialization_excludes_client_kwargs():
219+
mock_credentials = mock.MagicMock()
220+
model = Gemini(
221+
model="gemini-2.5-flash",
222+
client_kwargs={
223+
"enterprise": True,
224+
"credentials": mock_credentials,
225+
},
226+
)
227+
dumped = model.model_dump()
228+
assert "client_kwargs" not in dumped
229+
230+
231+
def test_gemini_repr_excludes_client_kwargs():
232+
mock_credentials = mock.MagicMock()
233+
model = Gemini(
234+
model="gemini-2.5-flash",
235+
client_kwargs={
236+
"enterprise": True,
237+
"credentials": mock_credentials,
238+
},
239+
)
240+
repr_str = repr(model)
241+
assert "client_kwargs" not in repr_str
242+
243+
185244
def test_client_version_header():
186245
model = Gemini(model="gemini-2.5-flash")
187246
client = model.api_client

0 commit comments

Comments
 (0)