-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[AI] Add ImageConfig support for generateContent #15923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
paulb777
wants to merge
20
commits into
main
Choose a base branch
from
pb-image-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
26f0784
[AI] Add config for image aspect ratio and size with Nano Banana
andrewheard 9d6081e
Merge remote-tracking branch 'origin/ah/ai-image-config' into pb-imag…
paulb777 07e45e5
build issues
paulb777 2005fea
Add `available` annotations
andrewheard 918fcf3
docs, agents, and more tests
paulb777 879fa39
integration tests
paulb777 7ac3ae3
Add missing default value `imageSize: ImagenImageSize? = nil`
andrewheard a1c1006
Fix tests
andrewheard 30405c2
Address TODOs
andrewheard 3e2736a
More build fixes
andrewheard e88c0de
review
paulb777 bd09d10
Merge remote-tracking branch 'origin/ah/ai-image-config' into pb-imag…
paulb777 36068b9
Apply suggestions from code review
paulb777 eccd2c8
review, style
paulb777 a663da0
style
paulb777 5f37627
Replace custom `Encodable` conformance with `EncodableProtoEnum`
andrewheard 36a9828
Check sizes in image generation tests
andrewheard 5ca0094
Add image generation size integration test
andrewheard bc1d717
Add TODO for Imagen integration test
andrewheard b8107c3
Add imagen image_size integration test
paulb777 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| /// Configuration options for generating images with Gemini models. | ||
| /// | ||
| /// See the [documentation](https://ai.google.dev/gemini-api/docs/image-generation#aspect_ratios_and_image_size) | ||
| /// to learn about parameters available for use with Gemini image models. | ||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| public struct ImageConfig: Sendable { | ||
| /// The aspect ratio of generated images. | ||
| public let aspectRatio: AspectRatio? | ||
|
|
||
| /// The size of the generated images. | ||
| public let imageSize: ImageSize? | ||
|
|
||
| /// Initializes configuration options for generating images with Gemini. | ||
| /// | ||
| /// - Parameters: | ||
| /// - aspectRatio: The aspect ratio of generated images. | ||
| /// - imageSize: The size of the generated images. | ||
| public init(aspectRatio: AspectRatio? = nil, imageSize: ImageSize? = nil) { | ||
| self.aspectRatio = aspectRatio | ||
| self.imageSize = imageSize | ||
| } | ||
| } | ||
|
|
||
| /// An aspect ratio for generated images. | ||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| public extension ImageConfig { | ||
| struct AspectRatio: Sendable { | ||
paulb777 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// Square (1:1) aspect ratio. | ||
| /// | ||
| /// Common uses for this aspect ratio include social media posts. | ||
| public static let square1x1 = AspectRatio(kind: .square1x1) | ||
|
|
||
| /// Portrait widescreen (9:16) aspect ratio. | ||
| /// | ||
| /// This is the ``landscape16x9`` aspect ratio rotated 90 degrees. This is a relatively new aspect | ||
| /// ratio that has been popularized by short form video apps (for example, YouTube shorts). Use | ||
| /// this for tall objects with strong vertical orientations such as buildings, trees, | ||
| /// waterfalls, or other similar objects. | ||
| public static let portrait9x16 = AspectRatio(kind: .portrait9x16) | ||
|
|
||
| /// Widescreen (16:9) aspect ratio. | ||
| /// | ||
| /// This ratio has replaced ``landscape4x3`` as the most common aspect ratio for TVs, monitors, | ||
| /// and mobile phone screens (landscape). Use this aspect ratio when you want to capture more of | ||
| /// the background (for example, scenic landscapes). | ||
| public static let landscape16x9 = AspectRatio(kind: .landscape16x9) | ||
|
|
||
| /// Portrait full screen (3:4) aspect ratio. | ||
| /// | ||
| /// This is the ``landscape4x3`` aspect ratio rotated 90 degrees. This allows you to capture more of | ||
| /// the scene vertically compared to the ``square1x1`` aspect ratio. | ||
| public static let portrait3x4 = AspectRatio(kind: .portrait3x4) | ||
|
|
||
| /// Fullscreen (4:3) aspect ratio. | ||
| /// | ||
| /// This aspect ratio is commonly used in media or film. It is also the dimensions of most old | ||
| /// (non-widescreen) TVs and medium format cameras. It captures more of the scene horizontally | ||
| /// (compared to ``square1x1``), making it a preferred aspect ratio for photography. | ||
| public static let landscape4x3 = AspectRatio(kind: .landscape4x3) | ||
|
|
||
| /// Portrait (2:3) aspect ratio. | ||
| public static let portrait2x3 = AspectRatio(kind: .portrait2x3) | ||
|
|
||
| /// Landscape (3:2) aspect ratio. | ||
| public static let landscape3x2 = AspectRatio(kind: .landscape3x2) | ||
|
|
||
| /// Portrait (4:5) aspect ratio. | ||
| public static let portrait4x5 = AspectRatio(kind: .portrait4x5) | ||
|
|
||
| /// Landscape (5:4) aspect ratio. | ||
| public static let landscape5x4 = AspectRatio(kind: .landscape5x4) | ||
|
|
||
| /// Portrait (1:4) aspect ratio. | ||
| public static let portrait1x4 = AspectRatio(kind: .portrait1x4) | ||
|
|
||
| /// Landscape (4:1) aspect ratio. | ||
| public static let landscape4x1 = AspectRatio(kind: .landscape4x1) | ||
|
|
||
| /// Portrait (1:8) aspect ratio. | ||
| public static let portrait1x8 = AspectRatio(kind: .portrait1x8) | ||
|
|
||
| /// Landscape (8:1) aspect ratio. | ||
| public static let landscape8x1 = AspectRatio(kind: .landscape8x1) | ||
|
|
||
| /// Ultrawide (21:9) aspect ratio. | ||
| public static let ultrawide21x9 = AspectRatio(kind: .ultrawide21x9) | ||
|
|
||
| let rawValue: String | ||
| } | ||
|
|
||
| /// The size of images to generate. | ||
| struct ImageSize: Sendable { | ||
| /// 512px (0.5K) image size. | ||
| /// | ||
| /// This corresponds to 512x512 pixel images in a ``ImageConfig/AspectRatio/square1x1`` aspect | ||
| /// ratio. See the [documentation](https://ai.google.dev/gemini-api/docs/image-generation#aspect_ratios_and_image_size) | ||
| /// for specific sizes in other aspect ratios. | ||
| public static let size512 = ImageSize(kind: .size512) | ||
|
|
||
| /// 1K image size. | ||
| /// | ||
| /// This corresponds to 1024x1024 pixel images in a ``ImageConfig/AspectRatio/square1x1`` aspect | ||
| /// ratio. See the [documentation](https://ai.google.dev/gemini-api/docs/image-generation#aspect_ratios_and_image_size) | ||
| /// for specific sizes in other aspect ratios. | ||
| public static let size1K = ImageSize(kind: .size1K) | ||
|
|
||
| /// 2K image size. | ||
| /// | ||
| /// This corresponds to 2048x2048 pixel images in a ``ImageConfig/AspectRatio/square1x1`` aspect | ||
| /// ratio. See the [documentation](https://ai.google.dev/gemini-api/docs/image-generation#aspect_ratios_and_image_size) | ||
| /// for specific sizes in other aspect ratios. | ||
| public static let size2K = ImageSize(kind: .size2K) | ||
|
|
||
| /// 4K image size. | ||
| /// | ||
| /// This corresponds to 4096x4096 pixel images in a ``ImageConfig/AspectRatio/square1x1`` aspect | ||
| /// ratio. See the [documentation](https://ai.google.dev/gemini-api/docs/image-generation#aspect_ratios_and_image_size) | ||
| /// for specific sizes in other aspect ratios. | ||
| public static let size4K = ImageSize(kind: .size4K) | ||
|
|
||
| let rawValue: String | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| extension ImageConfig.AspectRatio: ProtoEnum { | ||
| enum Kind: String { | ||
| case square1x1 = "1:1" | ||
| case portrait9x16 = "9:16" | ||
| case landscape16x9 = "16:9" | ||
| case portrait3x4 = "3:4" | ||
| case landscape4x3 = "4:3" | ||
| case portrait2x3 = "2:3" | ||
| case landscape3x2 = "3:2" | ||
| case portrait4x5 = "4:5" | ||
| case landscape5x4 = "5:4" | ||
| case portrait1x4 = "1:4" | ||
| case landscape4x1 = "4:1" | ||
| case portrait1x8 = "1:8" | ||
| case landscape8x1 = "8:1" | ||
| case ultrawide21x9 = "21:9" | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| extension ImageConfig.ImageSize: ProtoEnum { | ||
| enum Kind: String { | ||
| case size512 = "512" | ||
| case size1K = "1K" | ||
| case size2K = "2K" | ||
| case size4K = "4K" | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Codable Conformances | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| extension ImageConfig: Encodable {} | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| extension ImageConfig.AspectRatio: Encodable { | ||
| public func encode(to encoder: any Encoder) throws { | ||
| var container = encoder.singleValueContainer() | ||
| try container.encode(rawValue) | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| extension ImageConfig.ImageSize: Encodable { | ||
| public func encode(to encoder: any Encoder) throws { | ||
| var container = encoder.singleValueContainer() | ||
| try container.encode(rawValue) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.