26
26
from ... import Client
27
27
28
28
29
- def test_ml_dev_from_env (monkeypatch ):
29
+ def test_ml_dev_from_google_api_key_env (monkeypatch ):
30
30
api_key = "google_api_key"
31
31
monkeypatch .setenv ("GOOGLE_API_KEY" , api_key )
32
+ monkeypatch .setenv ("GEMINI_API_KEY" , "" )
32
33
33
34
client = Client ()
34
35
35
36
assert not client .models ._api_client .vertexai
36
37
assert client .models ._api_client .api_key == api_key
37
38
assert isinstance (client .models ._api_client , api_client .BaseApiClient )
38
39
40
+ def test_ml_dev_from_gemini_api_key_env (monkeypatch ):
41
+ api_key = "gemini_api_key"
42
+ monkeypatch .setenv ("GOOGLE_API_KEY" , "" )
43
+ monkeypatch .setenv ("GEMINI_API_KEY" , api_key )
44
+
45
+ client = Client ()
46
+
47
+ assert not client .models ._api_client .vertexai
48
+ assert client .models ._api_client .api_key == api_key
49
+ assert isinstance (client .models ._api_client , api_client .BaseApiClient )
50
+
51
+ def test_ml_dev_from_env_precedence (monkeypatch ):
52
+ # Gemini API key takes precedence over Google API key.
53
+ gemini_api_key = "gemini_api_key"
54
+ monkeypatch .setenv ("GEMINI_API_KEY" , gemini_api_key )
55
+ google_api_key = "google_api_key"
56
+ monkeypatch .setenv ("GOOGLE_API_KEY" , google_api_key )
57
+
58
+ client = Client ()
59
+
60
+ assert not client .models ._api_client .vertexai
61
+ assert client .models ._api_client .api_key == gemini_api_key
62
+ assert isinstance (client .models ._api_client , api_client .BaseApiClient )
63
+
39
64
40
65
def test_ml_dev_from_constructor ():
41
66
api_key = "google_api_key"
@@ -271,6 +296,7 @@ def test_invalid_vertexai_constructor_empty(monkeypatch):
271
296
monkeypatch .setenv ("GOOGLE_CLOUD_PROJECT" , "" )
272
297
monkeypatch .setenv ("GOOGLE_CLOUD_LOCATION" , "" )
273
298
monkeypatch .setenv ("GOOGLE_API_KEY" , "" )
299
+ monkeypatch .setenv ("GEMINI_API_KEY" , "" )
274
300
def mock_auth_default (scopes = None ):
275
301
return None , None
276
302
@@ -281,6 +307,7 @@ def mock_auth_default(scopes=None):
281
307
def test_invalid_mldev_constructor_empty (monkeypatch ):
282
308
with pytest .raises (ValueError ):
283
309
monkeypatch .setenv ("GOOGLE_API_KEY" , "" )
310
+ monkeypatch .setenv ("GEMINI_API_KEY" , "" )
284
311
Client ()
285
312
286
313
@@ -389,8 +416,8 @@ def test_mldev_explicit_arg_precedence(monkeypatch):
389
416
390
417
391
418
def test_replay_client_ml_dev_from_env (monkeypatch , use_vertex : bool ):
392
- api_key = "google_api_key "
393
- monkeypatch .setenv ("GOOGLE_API_KEY " , api_key )
419
+ gemini_api_key = "gemini_api_key "
420
+ monkeypatch .setenv ("GEMINI_API_KEY " , gemini_api_key )
394
421
monkeypatch .setenv ("GOOGLE_GENAI_CLIENT_MODE" , "replay" )
395
422
api_type = "vertex" if use_vertex else "mldev"
396
423
monkeypatch .setenv ("GOOGLE_GENAI_REPLAY_ID" , "test_replay_id." + api_type )
@@ -399,7 +426,7 @@ def test_replay_client_ml_dev_from_env(monkeypatch, use_vertex: bool):
399
426
client = Client ()
400
427
401
428
assert not client .models ._api_client .vertexai
402
- assert client .models ._api_client .api_key == api_key
429
+ assert client .models ._api_client .api_key == gemini_api_key
403
430
assert isinstance (
404
431
client .models ._api_client , replay_api_client .ReplayApiClient
405
432
)
@@ -461,10 +488,11 @@ def test_vertexai_apikey_from_constructor(monkeypatch):
461
488
assert isinstance (client .models ._api_client , api_client .BaseApiClient )
462
489
463
490
464
- def test_vertexai_apikey_from_env (monkeypatch ):
491
+ def test_vertexai_apikey_from_google_api_key_env (monkeypatch ):
465
492
# Vertex AI Express mode uses API key on Vertex AI.
466
493
api_key = "vertexai_api_key"
467
494
monkeypatch .setenv ("GOOGLE_API_KEY" , api_key )
495
+ monkeypatch .setenv ("GEMINI_API_KEY" , "" )
468
496
469
497
# Due to proj/location taking precedence, need to clear proj/location env
470
498
# variables.
@@ -480,6 +508,27 @@ def test_vertexai_apikey_from_env(monkeypatch):
480
508
assert "aiplatform" in client ._api_client ._http_options ["base_url" ]
481
509
assert isinstance (client .models ._api_client , api_client .BaseApiClient )
482
510
511
+ def test_vertexai_apikey_from_env_precedence (monkeypatch ):
512
+ # Vertex AI Express mode uses gemnai API key on Vertex AI.
513
+ gemini_api_key = "gemini_api_key"
514
+ monkeypatch .setenv ("GEMINI_API_KEY" , gemini_api_key )
515
+ google_api_key = "google_api_key"
516
+ monkeypatch .setenv ("GOOGLE_API_KEY" , google_api_key )
517
+
518
+ # Due to proj/location taking precedence, need to clear proj/location env
519
+ # variables.
520
+ monkeypatch .setenv ("GOOGLE_CLOUD_LOCATION" , "" )
521
+ monkeypatch .setenv ("GOOGLE_CLOUD_PROJECT" , "" )
522
+
523
+ client = Client (vertexai = True )
524
+
525
+ assert client .models ._api_client .vertexai
526
+ assert client .models ._api_client .api_key == gemini_api_key
527
+ assert not client .models ._api_client .project
528
+ assert not client .models ._api_client .location
529
+ assert "aiplatform" in client ._api_client ._http_options ["base_url" ]
530
+ assert isinstance (client .models ._api_client , api_client .BaseApiClient )
531
+
483
532
484
533
def test_vertexai_apikey_invalid_constructor1 ():
485
534
# Vertex AI Express mode uses API key on Vertex AI.
@@ -504,6 +553,7 @@ def test_vertexai_apikey_combo1(monkeypatch):
504
553
monkeypatch .setenv ("GOOGLE_CLOUD_PROJECT" , project_id )
505
554
monkeypatch .setenv ("GOOGLE_CLOUD_LOCATION" , location )
506
555
monkeypatch .setenv ("GOOGLE_API_KEY" , "" )
556
+ monkeypatch .setenv ("GEMINI_API_KEY" , "" )
507
557
508
558
# Explicit api_key takes precedence over implicit project/location.
509
559
client = Client (vertexai = True , api_key = api_key )
0 commit comments