Skip to content

Add image generation code to Gemini Model Handler - #36177

Merged
jrmccluskey merged 20 commits into
apache:masterfrom
jrmccluskey:nanoBanana
Oct 21, 2025
Merged

jrmccluskey merged 20 commits into
apache:masterfrom
jrmccluskey:nanoBanana

Conversation

@jrmccluskey

@jrmccluskey jrmccluskey commented Sep 16, 2025 •

Copy link
Copy Markdown
Contributor

Adds extra code for generating images based on text prompts and contextual images to the GeminiModelHandler, focused on using the gemini-2.5-flash-image-preview model (AKA Nano Banana.) Based on the example code from https://ai.google.dev/gemini-api/docs/image-generation

Also fixes parsing code in the text example and the core model handler, which had been broken by changes to the Gemini API's return format.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@jrmccluskey jrmccluskey changed the title [WIP] Add image generation code to Gemini Model Handler Add image generation code to Gemini Model Handler Oct 14, 2025
@jrmccluskey
jrmccluskey marked this pull request as ready for review October 14, 2025 20:49
@github-actions

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@jrmccluskey

Copy link
Copy Markdown
Contributor Author

assign set of reviewers

@jrmccluskey

Copy link
Copy Markdown
Contributor Author

precommit failures are likely flakes unrelated to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @damccorm for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

return (
pcoll
| "WriteImageToFile" >> beam.ParDo(
_WriteImageFn(self._output_dir, self._filename_prefix)))

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.

inference_args: any additional arguments passed to the generate_content
call.
"""
return model.models.generate_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 call is the exact same as the one to generate text - is the only difference in these apis that one accepts a list and one accepts a single text string? Or are there other things I'm missing?

This is surprisingly subtle IMO

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.

The type hinting is the major distinction here, the actual generation call to gemini is largely distinguished by the model you're calling (the docstring covers this somewhat.) The API is pretty simple from a user standpoint, the distinguishing request functions are really just to make sure we get reasonable typing in here.

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

Thanks, this LGTM

@damccorm

Copy link
Copy Markdown
Contributor

Oh, please fix linting issues though

@jrmccluskey

Copy link
Copy Markdown
Contributor Author

Failures are unrelated, merging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants