Skip to content

fix(ai): capture Gemini content filter blocks as errors - #1165

Closed
zachary822 wants to merge 1 commit into
PostHog:mainfrom
zachary822:gemini-error-reporting
Closed

zachary822 wants to merge 1 commit into
PostHog:mainfrom
zachary822:gemini-error-reporting

Conversation

@zachary822

Copy link
Copy Markdown

💡 Motivation and Context

Gemini's content filters do not raise. The request succeeds and returns either a blocked prompt (prompt_feedback.block_reason, no candidates) or a response stopped with a filter finish reason such as PROHIBITED_CONTENT. The wrapper captured these as successful generations: $ai_http_status = 200, no $ai_is_error. In AI observability a blocked call looked the same as a good one, and an app that falls back to another provider showed no error at the point of the fallback.

 call_llm_and_track_usage / call_llm_and_track_usage_async
   response = call_method(**kwargs)
   tag("$ai_stop_reason", ...)
+  if no exception:
+    block_reason = _extract_block_reason(response, provider)   # gemini only
+    if block_reason:
+      $ai_is_error = True
+      $ai_error = "Gemini blocked the prompt: SAFETY"
+                | "Gemini blocked the response: PROHIBITED_CONTENT"

 capture_streaming_event
   $ai_stop_reason = stop_reason
+  if no stream error and stop_reason is a Gemini filter reason:
+    $ai_is_error = True, $ai_error = "Gemini blocked the response: <reason>"
  • $ai_http_status stays 200, because the HTTP request succeeded. $ai_stop_reason is unchanged.
  • The filter finish reasons are SAFETY, RECITATION, BLOCKLIST, PROHIBITED_CONTENT, SPII and the IMAGE_* equivalents. STOP, MAX_TOKENS and the others are not errors.
  • $ai_error holds only the reason name, never content, so it is kept as is in privacy mode (like the existing fixed "cancelled" error).
  • The new helpers are private, so the public API snapshot does not change.

💚 How did you test it?

New tests in posthog/test/ai/gemini/test_gemini.py and test_gemini_async.py:

blocked response (PROHIBITED_CONTENT, SAFETY)  -> $ai_is_error, $ai_error, http 200
blocked prompt (block_reason SAFETY)           -> $ai_is_error, $ai_error, http 200
STOP / MAX_TOKENS, BLOCKED_REASON_UNSPECIFIED  -> no $ai_is_error
privacy mode                                   -> $ai_error kept
stream ending in PROHIBITED_CONTENT            -> $ai_is_error
async blocked response                         -> $ai_is_error
  • Before: the 6 block tests fail; the 5 unblocked tests pass.
    After: all pass.
  • ruff format --check, ruff check, mypy with the baseline filter, make public_api_check and python -W error -c "import posthog" are clean.
  • pytest posthog/test/ai: 1392 passed, 15 skipped. In the full suite, 3 tests in test_metrics_autocapture.py (forked-child cases) fail on macOS with and without this change.

Also checked against a real app that falls back to another provider when Gemini blocks a chapter translation: before, the blocked generation had $ai_stop_reason = PROHIBITED_CONTENT but no error flag.

Not covered

  • A blocked prompt in a stream: the stream path only receives stop_reason, so a prompt block (no candidates) is not flagged. Covering it needs the Gemini stream loops to pass the chunk's block reason through.
  • Other providers: OpenAI finish_reason = "content_filter" and Anthropic stop_reason = "refusal" are still not errors. _extract_block_reason dispatches by provider, so they can be added if wanted.

No sdk-spec covers how wrappers report provider-side blocks.

Merge Danger

Door: two-way. Only captured event properties change; reverting restores the old properties.

Blast Radius: analytics. Blocked Gemini generations start counting as errors in AI observability error rates, dashboards and alerts that filter on $ai_is_error.

📝 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: Human-driven (agent-assisted)

  • Written with Claude Code (Claude Opus 5.5) and reviewed by the author.
  • Considered sending a separate error $ai_span from the app instead, but the generation itself should carry the error, and only the wrapper sees the response before capture.
  • Kept the helpers private and followed the existing dispatch, import and docstring style next to extract_stop_reason. The stream prompt-block case and the other providers were left out to keep the change small.

🤖 Generated with Claude Code

@zachary822 zachary822 closed this Oct 31, 2026
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.

1 participant