From 4f60c5fd8f8c5f8e13ce5477cbd4b6ae3474e681 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Mon, 16 Nov 2026 04:00:08 -0400 Subject: [PATCH] fix(gemini): honor GEMINI_API_KEY environment variable --- .../changesets/gemini-api-key-environment.md | 5 ++ posthog/ai/gemini/_shared.py | 8 ++- posthog/ai/gemini/gemini.py | 4 +- posthog/ai/gemini/gemini_async.py | 4 +- posthog/test/ai/gemini/test_gemini_parity.py | 50 +++++++++++++++++-- 5 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 .sampo/changesets/gemini-api-key-environment.md diff --git a/.sampo/changesets/gemini-api-key-environment.md b/.sampo/changesets/gemini-api-key-environment.md new file mode 100644 index 000000000..c076fbc42 --- /dev/null +++ b/.sampo/changesets/gemini-api-key-environment.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Honor `GEMINI_API_KEY` when initializing the synchronous and asynchronous Gemini clients, after `GOOGLE_API_KEY` and before the legacy `API_KEY` fallback. diff --git a/posthog/ai/gemini/_shared.py b/posthog/ai/gemini/_shared.py index cdff0fc30..367b7ba22 100644 --- a/posthog/ai/gemini/_shared.py +++ b/posthog/ai/gemini/_shared.py @@ -70,10 +70,14 @@ def _build_gemini_client_args( resolved_api_key = api_key if resolved_api_key is None: - resolved_api_key = os.environ.get("GOOGLE_API_KEY") or os.environ.get("API_KEY") + resolved_api_key = ( + os.environ.get("GOOGLE_API_KEY") + or os.environ.get("GEMINI_API_KEY") + or os.environ.get("API_KEY") + ) if resolved_api_key is None: raise ValueError( - "API key must be provided either as parameter or via GOOGLE_API_KEY/API_KEY environment variable" + "API key must be provided either as parameter or via GOOGLE_API_KEY/GEMINI_API_KEY/API_KEY environment variable" ) client_args["api_key"] = resolved_api_key diff --git a/posthog/ai/gemini/gemini.py b/posthog/ai/gemini/gemini.py index 42d5cb688..5c5c8fd60 100644 --- a/posthog/ai/gemini/gemini.py +++ b/posthog/ai/gemini/gemini.py @@ -81,7 +81,7 @@ def __init__( ): """ Args: - api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY or API_KEY environment variable (not required for Vertex AI) + api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY, GEMINI_API_KEY, or API_KEY environment variables in that order (not required for Vertex AI) vertexai: Whether to use Vertex AI authentication credentials: Vertex AI credentials object project: GCP project ID for Vertex AI @@ -160,7 +160,7 @@ def __init__( ): """ Args: - api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY or API_KEY environment variable (not required for Vertex AI) + api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY, GEMINI_API_KEY, or API_KEY environment variables in that order (not required for Vertex AI) vertexai: Whether to use Vertex AI authentication credentials: Vertex AI credentials object project: GCP project ID for Vertex AI diff --git a/posthog/ai/gemini/gemini_async.py b/posthog/ai/gemini/gemini_async.py index 3b0d45c69..7463318ab 100644 --- a/posthog/ai/gemini/gemini_async.py +++ b/posthog/ai/gemini/gemini_async.py @@ -76,7 +76,7 @@ def __init__( ): """ Args: - api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY or API_KEY environment variable (not required for Vertex AI) + api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY, GEMINI_API_KEY, or API_KEY environment variables in that order (not required for Vertex AI) vertexai: Whether to use Vertex AI authentication credentials: Vertex AI credentials object project: GCP project ID for Vertex AI @@ -144,7 +144,7 @@ def __init__( ): """ Args: - api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY or API_KEY environment variable (not required for Vertex AI) + api_key: Google AI API key. If not provided, will use GOOGLE_API_KEY, GEMINI_API_KEY, or API_KEY environment variables in that order (not required for Vertex AI) vertexai: Whether to use Vertex AI authentication credentials: Vertex AI credentials object project: GCP project ID for Vertex AI diff --git a/posthog/test/ai/gemini/test_gemini_parity.py b/posthog/test/ai/gemini/test_gemini_parity.py index e4605e49c..9f7166f7e 100644 --- a/posthog/test/ai/gemini/test_gemini_parity.py +++ b/posthog/test/ai/gemini/test_gemini_parity.py @@ -11,16 +11,56 @@ @pytest.mark.parametrize("client_class", [Client, AsyncClient]) -def test_sync_and_async_clients_share_api_key_environment_precedence( +def test_clients_initialize_with_gemini_api_key_without_network( client_class, monkeypatch ): - monkeypatch.setenv("GOOGLE_API_KEY", "google-key") - monkeypatch.setenv("API_KEY", "legacy-key") + for name in ("GOOGLE_API_KEY", "API_KEY", "GOOGLE_GENAI_USE_VERTEXAI"): + monkeypatch.delenv(name, raising=False) + monkeypatch.setenv("GEMINI_API_KEY", "test-gemini-key") + + client = client_class(posthog_client=MagicMock()) + try: + assert client.models is not None + assert isinstance(client._provider_client, google_genai.Client) + finally: + client._provider_client.close() + + +@pytest.mark.parametrize("client_class", [Client, AsyncClient]) +@pytest.mark.parametrize( + "environment, explicit_key, expected_key", + [ + ({"GEMINI_API_KEY": "gemini-key"}, None, "gemini-key"), + ({"API_KEY": "legacy-key"}, None, "legacy-key"), + ( + {"GEMINI_API_KEY": "gemini-key", "API_KEY": "legacy-key"}, + None, + "gemini-key", + ), + ( + { + "GOOGLE_API_KEY": "google-key", + "GEMINI_API_KEY": "gemini-key", + "API_KEY": "legacy-key", + }, + None, + "google-key", + ), + ({"GEMINI_API_KEY": "gemini-key"}, "explicit-key", "explicit-key"), + ], +) +def test_sync_and_async_clients_share_api_key_environment_precedence( + client_class, monkeypatch, environment, explicit_key, expected_key +): + for name in ("GOOGLE_API_KEY", "GEMINI_API_KEY", "API_KEY"): + monkeypatch.delenv(name, raising=False) + for name, value in environment.items(): + monkeypatch.setenv(name, value) with patch.object(google_genai, "Client") as provider_client: - client_class(posthog_client=MagicMock()) + client_class(api_key=explicit_key, posthog_client=MagicMock()) - provider_client.assert_called_once_with(api_key="google-key") + provider_client.assert_called_once_with(api_key=expected_key) @pytest.mark.parametrize("client_class", [Client, AsyncClient])