Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/gemini-api-key-environment.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 6 additions & 2 deletions posthog/ai/gemini/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions posthog/ai/gemini/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions posthog/ai/gemini/gemini_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
50 changes: 45 additions & 5 deletions posthog/test/ai/gemini/test_gemini_parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
Loading