Skip to content

Map Gemini image-safety finish reasons to content_filter - #1006

Merged
Quim Muntal (qmuntal) merged 1 commit into
microsoft:mainfrom
PratikDhanaveFork:gemini-image-safety-finish-reason
Sep 7, 2026
Merged

Map Gemini image-safety finish reasons to content_filter#1006
Quim Muntal (qmuntal) merged 1 commit into
microsoft:mainfrom
PratikDhanaveFork:gemini-image-safety-finish-reason

Conversation

@PratikDhanave

Copy link
Copy Markdown
Contributor

Problem

toFinishReason (provider/geminiprovider/agent.go) maps the text content-policy finish reasons — SAFETY, RECITATION, BLOCKLIST, PROHIBITED_CONTENT, SPII — to the canonical content_filter. But the genai SDK also defines image-generation equivalents in the same content-policy category: FinishReasonImageSafety (IMAGE_SAFETY), FinishReasonImageProhibitedContent (IMAGE_PROHIBITED_CONTENT), and FinishReasonImageRecitation (IMAGE_RECITATION). These fall through to the default and return an empty string.

So a response blocked by image-safety policy reports no finish reason, whereas the equivalent text-safety block reports content_filter — an inconsistency within the same semantic category (and a gap vs. the cross-provider content_filter contract).

Fix

Map the three image-safety reasons to content_filter alongside their text counterparts.

Test

Extends the existing TestFinishReason_NonStreaming table with IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, and IMAGE_RECITATION rows, each expecting content_filter. They fail before the fix (empty string), pass after.

toFinishReason mapped the text content-policy reasons (SAFETY, RECITATION,
BLOCKLIST, PROHIBITED_CONTENT, SPII) to content_filter but let the
image-generation equivalents (IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT,
IMAGE_RECITATION) fall through to the default empty string. A response
blocked by image-safety policy therefore reported no finish reason instead of
content_filter, an inconsistency within the same content-policy category.
Map the image-safety reasons alongside their text counterparts.
Copilot AI lite review requested due to automatic review settings September 6, 2026 04:44
@github-actions github-actions Bot added area:provider Changes files in the provider area area:provider/gemini Changes files in the provider / gemini area size:small At most 30 changed lines across at most 2 files labels Sep 6, 2026

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

The mapping change is straightforward, consistent with existing semantics, and is covered by an expanded unit test.

Pull request overview

This PR aligns the Gemini provider’s finish-reason mapping with the framework’s cross-provider content_filter contract by including the genai SDK’s image-safety finish reasons, preventing blocked image-generation responses from reporting an empty finish reason.

Changes:

  • Map IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, and IMAGE_RECITATION to the canonical content_filter finish reason.
  • Extend the existing non-streaming finish-reason table test to cover the new image-safety reasons.
File summaries
File Description
provider/geminiprovider/agent.go Extends toFinishReason to treat image-safety finish reasons as content_filter.
provider/geminiprovider/agent_test.go Adds table-driven test cases asserting content_filter for image-safety finish reasons.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions github-actions Bot added pending-auto-risk Automatic risk classification is in progress risk:low Limited blast radius and straightforward rollback and removed pending-auto-risk Automatic risk classification is in progress labels Sep 6, 2026
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Scope: user-visible behavior (unexported toFinishReason helper, but the returned content_filter value is observed by callers via the response's finish-reason field)

Changed Go contract: toFinishReason in provider/geminiprovider/agent.go now maps genai.FinishReasonImageSafety, genai.FinishReasonImageProhibitedContent, and genai.FinishReasonImageRecitation to "content_filter", alongside the existing text-safety reasons (SAFETY, RECITATION, BLOCKLIST, PROHIBITED_CONTENT, SPII).

Upstream evidence reviewed: python/packages/gemini/agent_framework_gemini/_chat_client.py, _FINISH_REASON_MAP (lines ~315–326). Python maps SAFETY, RECITATION, LANGUAGE, BLOCKLIST, PROHIBITED_CONTENT, SPII, IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, and IMAGE_RECITATION all to "content_filter".

Result: aligned. The three image-safety reasons added in this PR match upstream Python's mapping to content_filter exactly, closing the parity gap described in the PR. No exported Go API surface changed (only internal mapping logic + tests), so this is not a public API change.

Note (non-blocking, out of scope for this PR): upstream Python also maps LANGUAGEcontent_filter, which Go's toFinishReason does not currently handle. This is a pre-existing gap unrelated to this PR's fix and may be worth a follow-up.

Warning

Firewall blocked 2 domains

The following domains were blocked by the firewall during workflow execution:

  • github.com
  • raw.githubusercontent.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "github.com"
    - "raw.githubusercontent.com"

See Network Configuration for more information.

Generated by Go API Consistency Review Agent · copilot · auto · 36.4 AIC · ⌖ 5.37 AIC · ⊞ 9.5K ·

@qmuntal
Quim Muntal (qmuntal) added this pull request to the merge queue Sep 7, 2026
Merged via the queue into microsoft:main with commit 9df9b8b Sep 7, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider/gemini Changes files in the provider / gemini area area:provider Changes files in the provider area risk:low Limited blast radius and straightforward rollback size:small At most 30 changed lines across at most 2 files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants