Skip to content

Add Gemini native image generation - #147

Merged
jph00 merged 2 commits into
fix/gemini-tool-resultfrom
feat/gemini-imagegen
Dec 3, 2026
Merged

jph00 merged 2 commits into
fix/gemini-tool-resultfrom
feat/gemini-imagegen

Conversation

@kafkasl

@kafkasl kafkasl commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Added native Gemini image generation (using models/gemini-nano-banana-2.1) by returning generated images as OutputImage parts and pricing image output tokens at the model's image token rate.

The OutputImage is a new part added in this small aidialog PR AnswerDotAI/aidialog#76.

Generated images come back as OutputImage parts containing a base64 data URL. Callers can display them in notebooks or save them to disk. To edit an image, send the saved file back as a user InputImage in a new turn, or continue the same AsyncChat. AsyncChat resends Gemini's saved Msg.raw verbatim, keeping its thoughtSignature intact.

Depends on AnswerDotAI/aidialog#76. Stacked on #146.

Comment thread fastllm/types.py
def payload_kwargs(msgs, model, stream=False, system=None, max_tokens=None, temperature=None, tools=None, tool_choice=None,
parallel_tool_calls=None, previous_response_id=None, prompt_cache_key=None, reasoning_effort=None, web_search_options=None,
cache_idxs=None, ttl=None, stop_callables=None): pass
cache_idxs=None, ttl=None, stop_callables=None, response_modalities=None, image_config=None): pass

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

response_modalities and image_config (Gemini's options for requesting image output and aspect ratio/size) sit directly on payload_kwargs rather than inside a Gemini-only wrapper, matching Anthropic-only cache_idxs and ttl, which other providers ignore.

Comment thread fastllm/gemini.py
return FinishReason.tool_calls if r==FinishReason.stop and any(~L(tcs).attrgot('server')) else r

# %% ../nbs/07_gemini.ipynb #11b4fd46
def _is_inline_img(p): return 'inlineData' in p and p['inlineData'].get('mimeType', 'image/png').startswith('image/')

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Checks mimeType.startswith('image/') so non-image inlineData stays out of OutputImage. Below in norm_parts, thought: True parts without 'text' (thought images) are skipped so non-streaming matches norm_sse_event.

Comment thread nbs/07_gemini.ipynb
"metadata": {},
"outputs": [],
"source": [
"png_b64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/iZk9HQAAAABJRU5ErkJggg=='\n",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Uses a tiny synthetic PNG for the offline lessons instead of adding a ~1 MB generated image to cachy.jsonl.

@jph00
jph00 merged commit 00ac553 into fix/gemini-tool-result Dec 3, 2026
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