Skip to content

Add OutputImage message part for generated images - #76

Merged
jph00 merged 1 commit into
mainfrom
feat/output-image
Dec 1, 2026
Merged

jph00 merged 1 commit into
mainfrom
feat/output-image

Conversation

@kafkasl

@kafkasl kafkasl commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

This PR is used by AnswerDotAI/fastllm#147 to add image generation capabilities to fastllm.

Message parts currently cover media sent into a model (images, audio, video, files), with no part type for images generated in an assistant reply.

I added an OutputImage media part so providers such as Gemini can return generated images alongside text. Plain-text formatting emits the compact <media> tag instead of dumping base64 data. Markdown rendering embeds the image when image data is present and falls back when empty.

Comment thread aidialog/msg_parts.py

# %% ../nbs/00_msg_parts.ipynb #e75741cc
@patch(as_prop=True)
def formatted(self:OutputImage): return self.ctext

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.

avoid returning the huge data:image/png;base64,... string and returns ctext, which in this case will be <media output_image image/png>

Comment thread aidialog/msg_parts.py
@patch(as_prop=True)
def formatted(self:OutputImage): return self.ctext
@patch
def doc(self:OutputImage, showthink=False, mx=2000): return f'![]({self.text})' if self.text else ''

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.

return the image as ![](data:...) so it renders properly in solveit after hist2fmt

Comment thread nbs/00_msg_parts.ipynb
Comment on lines +1998 to +2007
"source": [
"out_img = OutputImage('data:image/png;base64,xxx', mime='image/png')\n",
"test_eq(out_img.formatted, '<media output_image image/png>')\n",
"test_eq(out_img.doc(), '![](data:image/png;base64,xxx)')\n",
"test_eq(out_img._repr_markdown_(), '![](data:image/png;base64,xxx)')\n",
"test_eq(OutputImage(mime='image/png').doc(), '')\n",
"test_eq(OutputImage(mime='image/png')._repr_markdown_(), '<media output_image image/png>')\n",
"test_eq(mk_part(**_part2dict(out_img)), out_img)"
]
},

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.

This test checks plain-text formatting, Markdown rendering, empty-data fallbacks, and round-trip serialization.

@jph00
jph00 merged commit 867de40 into main Dec 1, 2026
2 checks passed
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