Skip to content

Python: Report content_filter when Gemini blocks the prompt - #10213

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
nniiovoo:python-gemini-blocked-prompt
Nov 10, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
nniiovoo:python-gemini-blocked-prompt

Conversation

@nniiovoo

Copy link
Copy Markdown
Contributor

Motivation & Context

When Gemini blocks the prompt itself, the response has no candidates and only prompt_feedback.block_reason. The client returned an empty message with finish_reason=None, so callers can't tell a blocked prompt from an empty answer, and streaming also dropped the usage. Repro is in #10212.

Description & Review Guide

  • What are the major changes? A small _response_finish_reason helper, used by both _process_generate_response and _process_chunk. It returns the mapped candidate finish reason as before, and content_filter when the candidate has no finish reason and prompt_feedback.block_reason is set. Usually a blocked prompt has no candidates, but when a later automatic-continuation hop is blocked the SDK keeps the earlier candidate with its finish_reason cleared, so that case is covered too.
  • What is the impact of these changes? A blocked prompt now reports finish_reason="content_filter" on both paths, and the streamed response keeps its usage. Responses where the candidate has a finish reason are unchanged.
  • What do you want reviewers to focus on? I went with a finish reason rather than raising, so callers can still read prompt_feedback from raw_representation. Happy to change it if you'd rather raise.

Tests: I added tests for a blocked prompt and a blocked continuation hop, on both paths (they fail on main). I also set prompt_feedback = None in the _make_response test helper, since a MagicMock attribute is always truthy.

Related Issue

Fixes #10212

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: issue analysis, implementation, tests, and this description.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Signed-off-by: NNIIOVO <haimingni44@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the change that distinguishes Gemini-blocked prompts from empty answers.

No open issues 🎉

What changed in this PR

This PR fixes #10212 by making the Python Gemini client report content_filter when Gemini blocks a prompt, distinguishing blocked requests from empty answers.

Changes:

  • Share finish-reason handling across streaming and non-streaming responses while preserving existing candidate finish reasons.
  • Retain streaming usage when a prompt is blocked.
  • Add regression tests for blocked prompts and blocked continuation hops in both modes, and correct the mock’s default prompt feedback.
File Description
python/​packages/​gemini/​tests/​test_gemini_client.py Adds blocked-response coverage and sets mock prompt feedback to None.
python/​packages/​gemini/​agent_framework_gemini/​_chat_client.py Adds shared finish-reason resolution that recognizes prompt blocking on both response paths.

Review effort: Balanced

Merged via the queue into microsoft:main with commit 2ebdda9 Nov 10, 2026
55 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — c308861f Deployed Nov 9, 2026 by nniiovoo via team_check #9104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Gemini: a blocked prompt comes back as an empty response with no finish reason

3 participants