Repository navigation
Conversation
…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
0c9d620 to
0440ae1
Compare
| """ | ||
| if message: | ||
| return message | ||
| if not is_image_model(model_name) or any(part.media is not None for part in content): |
There was a problem hiding this comment.
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_SUPPORTSsetstools=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 givesunknownplus 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: |
There was a problem hiding this comment.
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
contentinstead ofaccumulated_content - dropping the
if candidate.finish_message:guard, so a later chunk overwrites the message - passing
Noneinstead oflast_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}).' |
There was a problem hiding this comment.
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:
| return f'Image blocked by safety filters ({fr_name}).' | |
| return f'Image blocked ({fr_name}).' |
Ports the part of genkit-ai/genkit#5859 that still applies here.
Before this,
finish_messagewas 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.finish_messagenow reachesModelResponse.finish_messagein both generate and streaming. The JS plugin already does this.finish_messagesays why:blocked:Image blocked by safety filters (IMAGE_PROHIBITED_CONTENT).IMAGE_OTHER:Image generation stopped (IMAGE_OTHER). Try revising the prompt.STOP,NO_IMAGEand the rest:No image was returned. Try a clearer image prompt (...)MAX_TOKENSgets no message, becausefinish_reason=lengthalready says it.None.Not ported
response.request: this needs a change to the core generate loop, and JS doesn't do it.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
IMAGE_SAFETYas a block. Every other blocked reason, andMAX_TOKENS, got the "try a clearer prompt" hint. Here every reason that maps toblockedsays it was blocked and names the reason, andMAX_TOKENSgets no message.