Skip to content

Commit a152fe8

Browse files
committed
Fix shawoding caused by an empty GEMINI_API_KEY
Change-Id: I1df220253baf1b2171891ba380a0a3dde8e09a8b
1 parent cc129fe commit a152fe8

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

google/generativeai/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ def configure(
185185
"Invalid configuration: Please set either `api_key` or `client_options['api_key']`, but not both."
186186
)
187187
else:
188-
if api_key is None:
188+
if not api_key:
189189
# If no key is provided explicitly, attempt to load one from the
190190
# environment.
191191
api_key = os.getenv("GEMINI_API_KEY")
192192

193-
if api_key is None:
193+
if not api_key:
194194
# If the GEMINI_API_KEY doesn't exist, attempt to load the
195195
# GOOGLE_API_KEY from the environment.
196196
api_key = os.getenv("GOOGLE_API_KEY")

tests/test_client.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ def test_api_key_passed_via_client_options(self):
2929
client_opts = client._client_manager.client_config["client_options"]
3030
self.assertEqual(client_opts.api_key, "AIzA_client_opts")
3131

32+
@mock.patch.dict(os.environ, {"GEMINI_API_KEY": "AIzA_env"})
33+
def test_api_key_from_environment(self):
34+
# Default to API key loaded from environment.
35+
client.configure()
36+
client_opts = client._client_manager.client_config["client_options"]
37+
self.assertEqual(client_opts.api_key, "AIzA_env")
38+
39+
# But not when a key is provided explicitly.
40+
client.configure(api_key="AIzA_client")
41+
client_opts = client._client_manager.client_config["client_options"]
42+
self.assertEqual(client_opts.api_key, "AIzA_client")
43+
3244
@mock.patch.dict(os.environ, {"GOOGLE_API_KEY": "AIzA_env"})
3345
def test_api_key_from_environment(self):
3446
# Default to API key loaded from environment.
@@ -41,6 +53,30 @@ def test_api_key_from_environment(self):
4153
client_opts = client._client_manager.client_config["client_options"]
4254
self.assertEqual(client_opts.api_key, "AIzA_client")
4355

56+
@mock.patch.dict(os.environ, {"GEMINI_API_KEY": "", "GOOGLE_API_KEY": "AIzA_env"})
57+
def test_empty_gemini_api_key_doesnt_shadow(self):
58+
# Default to API key loaded from environment.
59+
client.configure()
60+
client_opts = client._client_manager.client_config["client_options"]
61+
self.assertEqual(client_opts.api_key, "AIzA_env")
62+
63+
# But not when a key is provided explicitly.
64+
client.configure(api_key="AIzA_client")
65+
client_opts = client._client_manager.client_config["client_options"]
66+
self.assertEqual(client_opts.api_key, "AIzA_client")
67+
68+
@mock.patch.dict(os.environ, {"GEMINI_API_KEY": "", "GOOGLE_API_KEY": "AIzA_env"})
69+
def test_empty_google_api_key_doesnt_shadow(self):
70+
# Default to API key loaded from environment.
71+
client.configure()
72+
client_opts = client._client_manager.client_config["client_options"]
73+
self.assertEqual(client_opts.api_key, "AIzA_env")
74+
75+
# But not when a key is provided explicitly.
76+
client.configure(api_key="AIzA_client")
77+
client_opts = client._client_manager.client_config["client_options"]
78+
self.assertEqual(client_opts.api_key, "AIzA_client")
79+
4480
def test_api_key_cannot_be_set_twice(self):
4581
client_opts = client_options.ClientOptions(api_key="AIzA_client_opts")
4682

0 commit comments

Comments
 (0)