litellm - 💡(How to fix) Fix [Bug]: Gemini image generation silently drops finishReason on safety blocks

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

When Gemini's image-generation endpoint returns finishReason: IMAGE_SAFETY / IMAGE_PROHIBITED_CONTENT, LiteLLM discards the finishReason and returns an empty ImageResponse with no exception. Downstream code can't tell "safety refusal" from "unrelated failure".

The chat-completion path was fixed by #20524 (which closed #20357), but only patched vertex_and_google_ai_studio_gemini.py. The image-gen transformation files were never updated.

Error Message

response = await litellm.aimage_generation( model="gemini/gemini-2.5-flash-image", prompt="turn this person into Yoda", )

response.data == []

no exception

Root Cause

When Gemini's image-generation endpoint returns finishReason: IMAGE_SAFETY / IMAGE_PROHIBITED_CONTENT, LiteLLM discards the finishReason and returns an empty ImageResponse with no exception. Downstream code can't tell "safety refusal" from "unrelated failure".

The chat-completion path was fixed by #20524 (which closed #20357), but only patched vertex_and_google_ai_studio_gemini.py. The image-gen transformation files were never updated.

Fix Action

Fix / Workaround

The chat-completion path was fixed by #20524 (which closed #20357), but only patched vertex_and_google_ai_studio_gemini.py. The image-gen transformation files were never updated.

Code Example

response = await litellm.aimage_generation(
    model="gemini/gemini-2.5-flash-image",
    prompt="turn this person into Yoda",
)
# response.data == []
# no exception
RAW_BUFFERClick to expand / collapse

Summary

When Gemini's image-generation endpoint returns finishReason: IMAGE_SAFETY / IMAGE_PROHIBITED_CONTENT, LiteLLM discards the finishReason and returns an empty ImageResponse with no exception. Downstream code can't tell "safety refusal" from "unrelated failure".

The chat-completion path was fixed by #20524 (which closed #20357), but only patched vertex_and_google_ai_studio_gemini.py. The image-gen transformation files were never updated.

Affected (verified 2026-05-27)

  • main and v1.86.1
  • Files: litellm/llms/gemini/image_generation/transformation.py, litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py

Both iterate candidates[].content.parts[] for inlineData, ignoring finishReason. Grep for finish|safety|prohibit|content_filter|_hidden_params in either file returns zero matches.

Reproduction

response = await litellm.aimage_generation(
    model="gemini/gemini-2.5-flash-image",
    prompt="turn this person into Yoda",
)
# response.data == []
# no exception

Underlying Gemini response contains candidates[0].finishReason: "IMAGE_PROHIBITED_CONTENT" — lost.

Suggested fix

Per #21348 Criterion 2 — preserve raw finishReason in _hidden_params or provider_specific_fields, and/or raise ContentPolicyViolationError when finishReason in {IMAGE_SAFETY, IMAGE_PROHIBITED_CONTENT, SAFETY, IMAGE_RECITATION}, matching the chat-completion behavior.

Related

  • #20357, #20524 (chat-only fix), #21348

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

litellm - 💡(How to fix) Fix [Bug]: Gemini image generation silently drops finishReason on safety blocks