Skip to content

fix(py/google-genai): pass Gemini finish_message through and explain empty image replies - #127

Open
hilariie wants to merge 1 commit into
mainfrom
fix/gemini-image-finish-message
Open

hilariie wants to merge 1 commit into
mainfrom
fix/gemini-image-finish-message

Conversation

@hilariie

Copy link
Copy Markdown
Contributor

Ports the part of genkit-ai/genkit#5859 that still applies here.

Before this, finish_message was always empty on Gemini responses. If an image model came back with no image (for example, a prompt that was too vague, or a policy block without a service message), the result looked like a normal success.

  • Pass-through: the candidate's finish_message now reaches ModelResponse.finish_message in both generate and streaming. The JS plugin already does this.
  • Empty image replies: when a Gemini image model returns no image, finish_message says why:
    • Any reason that maps to blocked: Image blocked by safety filters (IMAGE_PROHIBITED_CONTENT).
    • IMAGE_OTHER: Image generation stopped (IMAGE_OTHER). Try revising the prompt.
    • STOP, NO_IMAGE and the rest: No image was returned. Try a clearer image prompt (...)
    • MAX_TOKENS gets no message, because finish_reason=length already says it.
    • Gemini's own message always takes priority. Text models and replies that contain an image get None.

Not ported

  • Core background-model / typed model ref changes: these came from feat(py): core support for background model operations and typed model refs genkit#5854, which was closed as superseded, and this repo already has them.
  • Echoing the normalized config on response.request: this needs a change to the core generate loop, and JS doesn't do it.
  • Dropping gemini-2.5-flash-image-preview: this repo keeps retired models registered but hidden from autocomplete.
  • NO_IMAGE / IMAGE_OTHER -> other: already here.

Differences from upstream

  • Upstream only named IMAGE_SAFETY as a block. Every other blocked reason, and MAX_TOKENS, got the "try a clearer prompt" hint. Here every reason that maps to blocked says it was blocked and names the reason, and MAX_TOKENS gets no message.

…empty image replies

Ports the still-applicable part of genkit-ai/genkit#5859 (9d401da1d).

- The candidate's finish_message now reaches ModelResponse.finish_message
  in both generate and streaming, matching the JS plugin.
- Gemini native image models that return no media get a finish_message
  saying why, so an empty reply is not a silent success. Blocked reasons
  name the reason; MAX_TOKENS gets none, since finish_reason=length
  already says it.

Not ported: the core background-model changes (superseded upstream and
already here), the effective-config echo on response.request, and
dropping gemini-2.5-flash-image-preview (this repo keeps retired models
registered but hidden from autocomplete).

Co-authored-by: Jacob Cable <jacobcable94@gmail.com>
Upstream-Commit: genkit-ai/genkit@9d401da1d
Upstream-PR: genkit-ai/genkit#5859
@cabljac
cabljac force-pushed the fix/gemini-image-finish-message branch from 0c9d620 to 0440ae1 Compare November 13, 2026 13:53

@cabljac cabljac 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.

A few comments/questions. We should raise this with @ifielker and let her decide on it, as google genai has a bunch of in-flight work

"""
if message:
return message
if not is_image_model(model_name) or any(part.media is not None for part in content):

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.

This only checks the model and whether a media part came back. It never looks at the request. On an image model, the hint "No image was returned. Try a clearer image prompt" also fires when:

  • the reply is only a toolRequest (GEMINI_IMAGE_SUPPORTS sets tools=True)
  • the reply is a plain text chat turn (multiturn=True)
  • response_modalities=['TEXT'] is set
  • the finish reason is MALFORMED_FUNCTION_CALL, UNEXPECTED_TOOL_CALL, TOO_MANY_TOOL_CALLS, OTHER, FINISH_REASON_UNSPECIFIED, or a reason the SDK doesn't know yet, with no service message
  • the stream ended with no finish reason (fr is None), which gives unknown plus the hint

In those cases the hint reads as prompt advice and doesn't name the reason. I reproduced the tool-request, text-reply and TEXT-only cases. Maybe only add the prompt hint for STOP and NO_IMAGE when the reply has no tool requests and no text, and otherwise name the raw reason? It's also fine to return None when fr is None.



@pytest.mark.asyncio
async def test_generate_stream_image_model_finish_message(mocker: MockerFixture) -> None:

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.

Could we add tests for a reply that is only a tool request, a text-only reply on an image model, and a stream that ends with no finish reason? Those are the cases where the hint should not show.

The stream tests also don't pin a few things. Each of these changes to _gemini.py still passes all the new tests:

  • passing the last chunk's content instead of accumulated_content
  • dropping the if candidate.finish_message: guard, so a later chunk overwrites the message
  • passing None instead of last_fr

Maybe add: a stream with an image in chunk 1 and text with STOP after (expect None), a stream with finish_message on a middle chunk, and a stream that ends with IMAGE_SAFETY.

fr_name = getattr(fr, 'name', fr) if fr is not None else None
finish_reason = _to_finish_reason(fr)
if finish_reason == FinishReason.BLOCKED:
return f'Image blocked by safety filters ({fr_name}).'

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.

nit: RECITATION, LANGUAGE and SPII also map to blocked, so we'd get "Image blocked by safety filters (RECITATION)." RECITATION and LANGUAGE aren't safety filters. Maybe use neutral wording:

Suggested change
return f'Image blocked by safety filters ({fr_name}).'
return f'Image blocked ({fr_name}).'

This branch has not been deployed

No deployments
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.

2 participants