Skip to content

Commit 3357f5f

Browse files
committed
fix(gemin): Implement _verify_and_set_api_key.
In #401 we stopped overriding `Provider.__init__` and thus we started to rely again on the default `Provider._verify_and_set_api_key`. We need to provide a custom implementation that checks for both API key options instead of the plain string with a `/`. - Added `test_gemini_initialization_with_env_var_api_key` and `test_vertexai_initialization_with_env_var_api_key`.
1 parent 90e970a commit 3357f5f

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/any_llm/providers/gemini/gemini.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ class GeminiProvider(GoogleProvider):
1515
PROVIDER_DOCUMENTATION_URL = "https://ai.google.dev/gemini-api/docs"
1616
ENV_API_KEY_NAME = "GEMINI_API_KEY/GOOGLE_API_KEY"
1717

18-
def _get_client(self, config: ClientConfig) -> "genai.Client":
19-
"""Get Gemini API client."""
20-
api_key = getattr(config, "api_key", None) or os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
18+
def _verify_and_set_api_key(self, config: ClientConfig) -> ClientConfig:
19+
# Standardized API key handling. Splitting into its own function so that providers
20+
# Can easily override this method if they don't want verification (for instance, LMStudio)
21+
if not config.api_key:
22+
config.api_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
2123

22-
if not api_key:
23-
msg = "Google Gemini Developer API"
24-
raise MissingApiKeyError(msg, "GEMINI_API_KEY/GOOGLE_API_KEY")
24+
if not config.api_key:
25+
raise MissingApiKeyError(self.PROVIDER_NAME, self.ENV_API_KEY_NAME)
26+
return config
2527

26-
return genai.Client(api_key=api_key, **(config.client_args if config.client_args else {}))
28+
def _get_client(self, config: ClientConfig) -> "genai.Client":
29+
"""Get Gemini API client."""
30+
return genai.Client(api_key=config.api_key, **(config.client_args if config.client_args else {}))

tests/unit/providers/test_google_provider.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ def mock_google_provider(): # type: ignore[no-untyped-def]
4747
yield mock_genai
4848

4949

50+
@pytest.mark.parametrize("env_var", ["GEMINI_API_KEY", "GOOGLE_API_KEY"])
51+
def test_gemini_initialization_with_env_var_api_key(env_var: str) -> None:
52+
"""Test that the provider initializes correctly with API key from environment variable."""
53+
with patch.dict("os.environ", {env_var: "env-api-key"}, clear=True):
54+
provider = GeminiProvider(ClientConfig())
55+
assert provider.config.api_key == "env-api-key"
56+
57+
58+
def test_vertexai_initialization_with_env_var_api_key() -> None:
59+
"""Test that the VertexaiProvider initializes correctly with GOOGLE_PROJECT_ID from environment variable."""
60+
with patch.dict("os.environ", {"GOOGLE_PROJECT_ID": "env-project-id"}, clear=True):
61+
provider = VertexaiProvider(ClientConfig())
62+
assert provider.config.api_key == "env-project-id"
63+
64+
5065
@pytest.mark.asyncio
5166
async def test_completion_with_system_instruction(google_provider_class: type[Provider]) -> None:
5267
"""Test that completion works correctly with system_instruction."""

0 commit comments

Comments
 (0)