Repository navigation
fix(gemini): honor GEMINI_API_KEY environment variable - #1258
Conversation
marandaneto
left a comment
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
Thank you for your contribution!
SDK performance: posthog-pythonBenchmarks completed. No changes flagged for review. Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate. View all results
Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals. Each shard uses its own runner; compare Before PR against With PR within a row, not across shards. Compared 102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in |
💡 Motivation and Context
The Gemini wrapper raises
ValueErrorduring initialization when onlyGEMINI_API_KEYis set, even though the underlying Google Gen AI SDK accepts that environment variable. BothClientandAsyncClientresolve credentials through_build_gemini_client_args, whose fallback chain only checkedGOOGLE_API_KEYandAPI_KEY.This adds
GEMINI_API_KEYto the existing chain, matching the SDK's own order: explicitapi_keyfirst, thenGOOGLE_API_KEY,GEMINI_API_KEY, and the legacyAPI_KEY. Explicit empty keys and the Vertex AI path keep their current behavior. No constructor options, methods, or types are added; the constructor docstrings and the error message now mention the new variable.💚 How did you test it?
python -m pytest -q posthog/test/ai/gemini/test_gemini_parity.pycovers both wrapper classes with Gemini-only credentials, the Google/Gemini/legacy precedence, and an explicit key. It also constructs both wrappers against the real google-genai 3.3.0 provider with a fictitious key, without network requests. Onmain: 6 failed, 24 passed; with this change: 30 passed. The full Gemini test suite passes (132 passed, 5 skipped for missing credentials), and Ruff reports no new findings on the changed files.📝 Checklist
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Fully autonomous
Prepared by an AI agent (Codex with local shell tools, pytest and Ruff), reviewed by a second agent. The change keeps the existing authentication chain and only adds the SDK-compatible
GEMINI_API_KEYfallback in the position the SDK itself uses. No manual testing is claimed; human review is required before merging.