Repository navigation
fix(ai): capture Gemini content filter blocks as errors - #1165
Closed
zachary822 wants to merge 1 commit into
Closed
zachary822 wants to merge 1 commit into
zachary822 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 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 asPROHIBITED_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_statusstays 200, because the HTTP request succeeded.$ai_stop_reasonis unchanged.SAFETY,RECITATION,BLOCKLIST,PROHIBITED_CONTENT,SPIIand theIMAGE_*equivalents.STOP,MAX_TOKENSand the others are not errors.$ai_errorholds only the reason name, never content, so it is kept as is in privacy mode (like the existing fixed"cancelled"error).💚 How did you test it?
New tests in
posthog/test/ai/gemini/test_gemini.pyandtest_gemini_async.py:After: all pass.
ruff format --check,ruff check, mypy with the baseline filter,make public_api_checkandpython -W error -c "import posthog"are clean.pytest posthog/test/ai: 1392 passed, 15 skipped. In the full suite, 3 tests intest_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_CONTENTbut no error flag.Not covered
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.finish_reason = "content_filter"and Anthropicstop_reason = "refusal"are still not errors._extract_block_reasondispatches 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
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
$ai_spanfrom the app instead, but the generation itself should carry the error, and only the wrapper sees the response before capture.extract_stop_reason. The stream prompt-block case and the other providers were left out to keep the change small.🤖 Generated with Claude Code