Skip to content

fix(gemini): honor GEMINI_API_KEY environment variable - #1258

Merged
marandaneto merged 1 commit into
PostHog:mainfrom
pcbeingused333:fix/gemini-env-key-c3594
Nov 16, 2026
Merged

marandaneto merged 1 commit into
PostHog:mainfrom
pcbeingused333:fix/gemini-env-key-c3594

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

The Gemini wrapper raises ValueError during initialization when only GEMINI_API_KEY is set, even though the underlying Google Gen AI SDK accepts that environment variable. Both Client and AsyncClient resolve credentials through _build_gemini_client_args, whose fallback chain only checked GOOGLE_API_KEY and API_KEY.

This adds GEMINI_API_KEY to the existing chain, matching the SDK's own order: explicit api_key first, then GOOGLE_API_KEY, GEMINI_API_KEY, and the legacy API_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.py covers 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. On main: 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

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to 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_KEY fallback in the position the SDK itself uses. No manual testing is claimed; human review is required before merging.

@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 16, 2026 08:28

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good; no qualifying findings. Verdict: correct.

Thank you for your contribution!

@github-actions

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks 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
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 115.72 KiB/op 115.72 KiB/op <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,107.6 count/op 1,107.6 count/op <0.1% more Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 63.42 µs/op 63.99 µs/op 0.9% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 36.24 MiB 36.23 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 37.97 MiB 37.96 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 37.97 MiB 37.96 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 68.26 µs/op 68.09 µs/op 0.3% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 40.31 MiB 40.31 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 168.28 KiB/op 168.28 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,100.57 count/op 1,100.59 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 86.58 µs/op 87.59 µs/op 1.2% more time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41 MiB 41.1 MiB 0.3% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 46.07 MiB 46.18 MiB 0.2% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 46.07 MiB 46.18 MiB 0.2% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 85.21 µs/op 85.48 µs/op 0.3% more time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 50.91 MiB 50.99 MiB 0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 119.85 KiB/op 119.86 KiB/op <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,147.46 count/op 1,147.49 count/op <0.1% more Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 73.52 µs/op 73.48 µs/op <0.1% less time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 36.17 MiB 36.19 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 36.25 MiB 36.28 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 36.25 MiB 36.28 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 76 µs/op 76.29 µs/op 0.4% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 40.31 MiB 40.31 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 172.74 KiB/op 172.74 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,140.32 count/op 1,140.33 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 91.82 µs/op 92.16 µs/op 0.4% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 40.95 MiB 40.97 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 40.95 MiB 40.97 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 40.94 MiB 40.96 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 90.96 µs/op 91.07 µs/op 0.1% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.24 MiB 41.37 MiB 0.3% more memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 42.86 µs/op 42.94 µs/op 0.2% more time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 42.9 µs/op 42.93 µs/op <0.1% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 43.44 µs/op 43.47 µs/op <0.1% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 44.25 µs/op 43.46 µs/op 1.8% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 226.56 KiB/op 226.56 KiB/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,240.44 count/op 2,240.44 count/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 138.14 µs/op 138.75 µs/op 0.4% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 138.14 µs/op 138.75 µs/op 0.4% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.17 MiB/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,014.88 count/op 22,014.88 count/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 1.3 ms/op 1.3 ms/op 0.5% less time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 1.3 ms/op 1.3 ms/op 0.5% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.69 MiB/op 21.69 MiB/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 219,656.62 count/op 219,656.62 count/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 12.82 ms/op 12.84 ms/op 0.1% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 12.82 ms/op 12.84 ms/op 0.1% more time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 42.55 MiB 42.98 MiB 1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 42.81 MiB 43.29 MiB 1.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 42.81 MiB 43.29 MiB 1.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 43.2 MiB 43.11 MiB 0.2% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 42.11 MiB 42.1 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 42.14 MiB 42.21 MiB 0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 41.67 MiB 42.16 MiB 1.2% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 42.55 MiB 42.58 MiB <0.1% more memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 16.63 KiB/op 16.63 KiB/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 164.77 count/op 164.77 count/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 8.76 µs/op 8.7 µs/op 0.7% less time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 8.76 µs/op 8.7 µs/op 0.7% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 25.49 KiB/op 25.49 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 241.28 count/op 241.28 count/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 12.16 µs/op 12.08 µs/op 0.7% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 12.16 µs/op 12.08 µs/op 0.7% less time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 40.7 KiB/op 40.7 KiB/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 390.27 count/op 390.27 count/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 16.8 µs/op 16.86 µs/op 0.3% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 16.8 µs/op 16.86 µs/op 0.3% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 61.48 KiB/op 61.48 KiB/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 586.27 count/op 586.27 count/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 23.99 µs/op 24.08 µs/op 0.4% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 23.99 µs/op 24.08 µs/op 0.4% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 60.11 KiB/op 60.11 KiB/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 569.77 count/op 569.77 count/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 22.76 µs/op 22.68 µs/op 0.3% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 22.76 µs/op 22.68 µs/op 0.3% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.93 KiB/op 19.93 KiB/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 194.28 count/op 194.28 count/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 9.75 µs/op 9.73 µs/op 0.2% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 9.75 µs/op 9.73 µs/op 0.2% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 21.56 KiB/op 21.56 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 199.78 count/op 199.78 count/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 10.23 µs/op 10.25 µs/op 0.2% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 10.23 µs/op 10.25 µs/op 0.2% more time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 15.79 KiB/op 15.79 KiB/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 154.77 count/op 154.77 count/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 8.21 µs/op 8.16 µs/op 0.6% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 8.21 µs/op 8.16 µs/op 0.6% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 16.79 KiB/op 16.79 KiB/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 168.77 count/op 168.77 count/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 8.98 µs/op 9.06 µs/op 0.8% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 8.98 µs/op 9.06 µs/op 0.8% more time Not flagged

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 652b9792b76c8fb177c4e99c021ef9573a1f5366 → 4f60c5fd8f8c5f8e13ce5477cbd4b6ae3474e681.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

@marandaneto
marandaneto enabled auto-merge (squash) November 16, 2026 09:22
@marandaneto
marandaneto merged commit 8415830 into PostHog:main Nov 16, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants