Skip to content

fix(ai): record the HTTP status of google-genai errors - #1110

Merged
marandaneto merged 2 commits into
PostHog:mainfrom
pcbeingused333:fix/gemini-error-http-status
Oct 23, 2026
Merged

marandaneto merged 2 commits into
PostHog:mainfrom
pcbeingused333:fix/gemini-error-http-status

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

The generation wrappers (call_llm_and_track_usage and its async twin) and the Gemini embedding outcome read the HTTP status of a failed call from the exception's status_code, which OpenAI and Anthropic errors have. google-genai's APIError (ClientError and ServerError) keeps the status in code, so every failed Gemini call was captured with $ai_http_status: 0, including 429 and 503 responses.

A small helper, _get_http_status, now reads status_code and, when it is missing, code; a code outside the HTTP range (a gRPC code, for example) still counts as no status, and errors without either give 0 as before. The two wrappers and the Gemini embedding outcome use it.

💚 How did you test it?

  • New posthog/test/ai/test_http_status.py: the helper on status_code, code, an out-of-range or non-integer code and a plain exception; real google.genai.errors.ClientError (429) and ServerError (503) through the sync wrapper, and a ClientError through the async one; and an error with status_code through the sync wrapper to show it is unchanged. The three google-genai cases fail on main (status 0) and pass with the change.
  • posthog/test/ai/gemini and posthog/test/ai/test_capture_contract.py with the new test: 135 passed, 5 skipped.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed. (n/a)
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file (added .sampo/changesets/gemini-error-http-status.md in the same format)

🤖 Agent context

Autonomy: Fully autonomous

  • The bug was found and the fix, tests and description were written by Claude Code; the diff and the explanation were reviewed before submitting.

@pcbeingused333
pcbeingused333 requested a review from a team as a code owner October 22, 2026 22:09
The generation and embedding wrappers read the HTTP status of a failed call from
the exception's status_code, which OpenAI and Anthropic errors have. google-genai's
APIError (ClientError, ServerError) carries it as code, so every failed Gemini
call was captured with $ai_http_status 0. Read either attribute through a small
helper; a code outside the HTTP range still counts as no status.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@pcbeingused333
pcbeingused333 force-pushed the fix/gemini-error-http-status branch from 0b5fe1e to de6b4ba Compare October 23, 2026 06:52
@marandaneto
marandaneto enabled auto-merge (squash) October 23, 2026 12:35
@marandaneto
marandaneto merged commit 25d2767 into PostHog:main Oct 23, 2026
42 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