Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
26f0784
[AI] Add config for image aspect ratio and size with Nano Banana
andrewheard Mar 10, 2026
9d6081e
Merge remote-tracking branch 'origin/ah/ai-image-config' into pb-imag…
paulb777 Mar 10, 2026
07e45e5
build issues
paulb777 Mar 10, 2026
2005fea
Add `available` annotations
andrewheard Mar 10, 2026
918fcf3
docs, agents, and more tests
paulb777 Mar 10, 2026
879fa39
integration tests
paulb777 Mar 10, 2026
7ac3ae3
Add missing default value `imageSize: ImagenImageSize? = nil`
andrewheard Mar 10, 2026
a1c1006
Fix tests
andrewheard Mar 10, 2026
30405c2
Address TODOs
andrewheard Mar 10, 2026
3e2736a
More build fixes
andrewheard Mar 10, 2026
e88c0de
review
paulb777 Mar 10, 2026
bd09d10
Merge remote-tracking branch 'origin/ah/ai-image-config' into pb-imag…
paulb777 Mar 10, 2026
36068b9
Apply suggestions from code review
paulb777 Mar 10, 2026
eccd2c8
review, style
paulb777 Mar 11, 2026
a663da0
style
paulb777 Mar 11, 2026
5f37627
Replace custom `Encodable` conformance with `EncodableProtoEnum`
andrewheard Mar 11, 2026
36a9828
Check sizes in image generation tests
andrewheard Mar 11, 2026
5ca0094
Add image generation size integration test
andrewheard Mar 11, 2026
bc1d717
Add TODO for Imagen integration test
andrewheard Mar 11, 2026
b8107c3
Add imagen image_size integration test
paulb777 Mar 11, 2026
a433717
Merge remote-tracking branch 'origin/main' into pb-image-config
paulb777 Mar 13, 2026
268800c
post-merge availability cleanup
paulb777 Mar 13, 2026
a01f1c9
remove done todo
paulb777 Mar 13, 2026
5656eca
missed available
paulb777 Mar 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions FirebaseAI/Sources/GenerationConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public struct GenerationConfig: Sendable {
/// Configuration for controlling the "thinking" behavior of compatible Gemini models.
var thinkingConfig: ThinkingConfig?

/// Configure the aspect ratio and size of generated images.
var imageConfig: ImageConfig?

/// Creates a new `GenerationConfig` value.
///
/// See the
Expand Down Expand Up @@ -162,12 +165,13 @@ public struct GenerationConfig: Sendable {
/// > backwards-incompatible ways.
/// - thinkingConfig: Configuration for controlling the "thinking" behavior of compatible Gemini
/// models; see ``ThinkingConfig`` for more details.
/// - imageConfig: Configure the aspect ratio and size of generated images.
public init(temperature: Float? = nil, topP: Float? = nil, topK: Int? = nil,
candidateCount: Int? = nil, maxOutputTokens: Int? = nil,
presencePenalty: Float? = nil, frequencyPenalty: Float? = nil,
stopSequences: [String]? = nil, responseMIMEType: String? = nil,
responseSchema: Schema? = nil, responseModalities: [ResponseModality]? = nil,
thinkingConfig: ThinkingConfig? = nil) {
thinkingConfig: ThinkingConfig? = nil, imageConfig: ImageConfig? = nil) {
// Explicit init because otherwise if we re-arrange the above variables it changes the API
// surface.
self.temperature = temperature
Expand All @@ -183,12 +187,14 @@ public struct GenerationConfig: Sendable {
responseJSONSchema = nil
self.responseModalities = responseModalities
self.thinkingConfig = thinkingConfig
self.imageConfig = imageConfig
}

init(temperature: Float? = nil, topP: Float? = nil, topK: Int? = nil, candidateCount: Int? = nil,
maxOutputTokens: Int? = nil, presencePenalty: Float? = nil, frequencyPenalty: Float? = nil,
stopSequences: [String]? = nil, responseMIMEType: String, responseJSONSchema: JSONObject,
responseModalities: [ResponseModality]? = nil, thinkingConfig: ThinkingConfig? = nil) {
responseModalities: [ResponseModality]? = nil, thinkingConfig: ThinkingConfig? = nil,
imageConfig: ImageConfig? = nil) {
self.temperature = temperature
self.topP = topP
self.topK = topK
Expand All @@ -202,6 +208,7 @@ public struct GenerationConfig: Sendable {
self.responseJSONSchema = responseJSONSchema
self.responseModalities = responseModalities
self.thinkingConfig = thinkingConfig
self.imageConfig = imageConfig
}

/// Merges two configurations, giving precedence to values found in the `overrides` parameter.
Expand Down Expand Up @@ -239,6 +246,7 @@ public struct GenerationConfig: Sendable {
config.responseMIMEType = overrideConfig.responseMIMEType ?? config.responseMIMEType
config.responseModalities = overrideConfig.responseModalities ?? config.responseModalities
config.thinkingConfig = overrideConfig.thinkingConfig ?? config.thinkingConfig
config.imageConfig = overrideConfig.imageConfig ?? config.imageConfig

// 5. Handle Schema mutual exclusivity with precedence for `responseJSONSchema`.
if let responseJSONSchema = overrideConfig.responseJSONSchema {
Expand Down Expand Up @@ -271,5 +279,6 @@ extension GenerationConfig: Encodable {
case responseJSONSchema = "responseJsonSchema"
case responseModalities
case thinkingConfig
case imageConfig
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct ImageGenerationParameters {
let storageURI: String?
let negativePrompt: String?
let aspectRatio: String?
let sampleImageSize: String?
let safetyFilterLevel: String?
let personGeneration: String?
let outputOptions: ImageGenerationOutputOptions?
Expand All @@ -38,6 +39,7 @@ extension ImageGenerationParameters: Encodable {
case storageURI = "storageUri"
case negativePrompt
case aspectRatio
case sampleImageSize
case safetyFilterLevel = "safetySetting"
case personGeneration
case outputOptions
Expand All @@ -52,6 +54,7 @@ extension ImageGenerationParameters: Encodable {
try container.encodeIfPresent(storageURI, forKey: .storageURI)
try container.encodeIfPresent(negativePrompt, forKey: .negativePrompt)
try container.encodeIfPresent(aspectRatio, forKey: .aspectRatio)
try container.encodeIfPresent(sampleImageSize, forKey: .sampleImageSize)
try container.encodeIfPresent(safetyFilterLevel, forKey: .safetyFilterLevel)
try container.encodeIfPresent(personGeneration, forKey: .personGeneration)
try container.encodeIfPresent(outputOptions, forKey: .outputOptions)
Expand Down
1 change: 1 addition & 0 deletions FirebaseAI/Sources/Types/Public/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Any changes to these types must be done carefully to avoid breaking changes for
- **`Backend.swift`**: Defines the `Backend` struct, which is used to configure the backend API for the Firebase AI SDK. It provides static methods `vertexAI(location:)` and `googleAI()` to create instances for the respective backends.
- **`Part.swift`**: Defines the `Part` protocol and several conforming structs: `TextPart`, `InlineDataPart`, `FileDataPart`, `FunctionCallPart`, `FunctionResponsePart`, `ExecutableCodePart`, and `CodeExecutionResultPart`. A `Part` represents a discrete piece of data in a media format that can be interpreted by the model.
- **`ResponseModality.swift`**: Defines the `ResponseModality` struct, which represents the different types of data that a model can produce as output (e.g., `text`, `image`, `audio`).
- **`ImageConfig.swift`**: Defines the `ImageConfig` struct, used for configuring generated image properties like aspect ratio and size.
- **`Schema.swift`**: Defines the `Schema` class, which allows the definition of input and output data types for function calling. It supports various data types like string, number, integer, boolean, array, and object.
- **`ThinkingConfig.swift`**: Defines the `ThinkingConfig` struct, for controlling the "thinking" behavior of compatible Gemini models. It includes parameters like `thinkingBudget` and `includeThoughts`.
- **`URLContextMetadata.swift`**: Defines the `URLContextMetadata` struct, which contains metadata related to the `Tool.urlContext()` tool.
Expand Down
150 changes: 150 additions & 0 deletions FirebaseAI/Sources/Types/Public/ImageConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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 for controlling generated image properties such as aspect ratio and image size.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct ImageConfig: Sendable {
let aspectRatio: AspectRatio?
let imageSize: ImageSize?

/// Initializes an `ImageConfig` with the given aspect ratio and image size.
///
/// - Parameters:
/// - aspectRatio: The aspect ratio for generated images.
/// - imageSize: The size for 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 {
/// 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 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 lets 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)

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"
}
}

@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)
}
}
2 changes: 2 additions & 0 deletions FirebaseAI/Sources/Types/Public/Imagen/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ These types are part of the public API and are used by developers to interact wi

- **`ImagenGenerationConfig.swift`**: Defines the `ImagenGenerationConfig` struct, which contains configuration options for generating images with Imagen, such as `negativePrompt`, `numberOfImages`, `aspectRatio`, `imageFormat`, and `addWatermark`.

- **`ImagenImageSize.swift`**: Defines the `ImagenImageSize` struct, which represents the size of images generated by Imagen. It provides static properties for common image sizes like `size1K`, `size2K`, etc.

- **`ImagenGenerationResponse.swift`**: Defines the `ImagenGenerationResponse` struct, which is the response from a request to generate images. It contains the generated `images` and a `filteredReason` if any images were filtered.

- **`ImagenImageFormat.swift`**: Defines the `ImagenImageFormat` struct, which represents the image format for generated images. It provides static methods for `png()` and `jpeg(compressionQuality:)`.
Expand Down
53 changes: 2 additions & 51 deletions FirebaseAI/Sources/Types/Public/Imagen/ImagenAspectRatio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

/// An aspect ratio for images generated by Imagen.
/// The aspect ratio of images generated by Imagen.
///
/// To specify an aspect ratio for generated images, set ``ImagenGenerationConfig/aspectRatio`` in
/// your ``ImagenGenerationConfig``. See the [Cloud
/// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#aspect-ratio)
/// for more details and examples of the supported aspect ratios.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct ImagenAspectRatio: Sendable {
/// Square (1:1) aspect ratio.
///
/// Common uses for this aspect ratio include social media posts.
public static let square1x1 = ImagenAspectRatio(kind: .square1x1)

/// Portrait widescreen (9:16) aspect ratio.
///
/// This is the ``landscape16x9`` aspect ratio rotated 90 degrees. This 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 = ImagenAspectRatio(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 = ImagenAspectRatio(kind: .landscape16x9)

/// Portrait full screen (3:4) aspect ratio.
///
/// This is the ``landscape4x3`` aspect ratio rotated 90 degrees. This lets to capture more of
/// the scene vertically compared to the ``square1x1`` aspect ratio.
public static let portrait3x4 = ImagenAspectRatio(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 = ImagenAspectRatio(kind: .landscape4x3)

let rawValue: String
}

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension ImagenAspectRatio: ProtoEnum {
enum Kind: String {
case square1x1 = "1:1"
case portrait9x16 = "9:16"
case landscape16x9 = "16:9"
case portrait3x4 = "3:4"
case landscape4x3 = "4:3"
}
}
public typealias ImagenAspectRatio = ImageConfig.AspectRatio
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public struct ImagenGenerationConfig {
/// ``ImagenAspectRatio`` for more details.
public var aspectRatio: ImagenAspectRatio?

/// The size of generated images.
///
/// Defaults to 1K. Supported image sizes depend on the model; see ``ImagenImageSize`` for more
/// details.
public var imageSize: ImagenImageSize?

/// The image format of generated images.
///
/// Defaults to PNG. See ``ImagenImageFormat`` for more details.
Expand All @@ -67,16 +73,18 @@ public struct ImagenGenerationConfig {
/// See ``numberOfImages``.
/// - aspectRatio: The aspect ratio of generated images; defaults to to square, 1:1. See
/// ``aspectRatio``.
/// - imageSize: The size of generated images; defaults to 1K. See ``imageSize``.
/// - imageFormat: The image format of generated images; defaults to PNG. See ``imageFormat``.
/// - addWatermark: Whether to add an invisible watermark to generated images; the default value
/// depends on the model. See ``addWatermark``.
public init(negativePrompt: String? = nil, numberOfImages: Int? = nil,
aspectRatio: ImagenAspectRatio? = nil, imageFormat: ImagenImageFormat? = nil,
addWatermark: Bool? = nil) {
self.numberOfImages = numberOfImages
aspectRatio: ImagenAspectRatio? = nil, imageSize: ImagenImageSize? = nil,
imageFormat: ImagenImageFormat? = nil, addWatermark: Bool? = nil) {
self.negativePrompt = negativePrompt
self.imageFormat = imageFormat
self.numberOfImages = numberOfImages
self.aspectRatio = aspectRatio
self.imageSize = imageSize
self.imageFormat = imageFormat
self.addWatermark = addWatermark
}
}
21 changes: 21 additions & 0 deletions FirebaseAI/Sources/Types/Public/Imagen/ImagenImageSize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

/// The size of images generated by Imagen.
///
/// To specify the size of generated images, set ``ImagenGenerationConfig/imageSize`` in
/// your ``ImagenGenerationConfig``. See the [Cloud
/// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images)
/// for more details and examples of the supported aspect ratios.
public typealias ImagenImageSize = ImageConfig.ImageSize
1 change: 1 addition & 0 deletions FirebaseAI/Sources/Types/Public/Imagen/ImagenModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public final class ImagenModel {
storageURI: storageURI,
negativePrompt: generationConfig?.negativePrompt,
aspectRatio: generationConfig?.aspectRatio?.rawValue,
sampleImageSize: generationConfig?.imageSize?.rawValue,
safetyFilterLevel: safetySettings?.safetyFilterLevel?.rawValue,
personGeneration: safetySettings?.personFilterLevel?.rawValue,
outputOptions: generationConfig?.imageFormat.map {
Expand Down
1 change: 1 addition & 0 deletions FirebaseAI/Tests/TestApp/Sources/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ public enum ModelNames {
public static let gemini2_5_FlashLivePreview = "gemini-2.5-flash-native-audio-preview-12-2025"
public static let gemini2_5_Pro = "gemini-2.5-pro"
public static let gemini3_1_FlashLitePreview = "gemini-3.1-flash-lite-preview"
public static let gemini3_1_FlashImagePreview = "gemini-3.1-flash-image-preview"
public static let gemma3_4B = "gemma-3-4b-it"
}
Loading
Loading