Skip to content

Conversation

@MathiasZander
Copy link
Contributor

@MathiasZander MathiasZander commented Nov 30, 2025

Enables configuration of image generation settings (resolution, output format) when using Gemini models with image output capabilities through the Microsoft.Extensions.AI integration.

Includes predefined constants in ImageConfigValues for common values while keeping the API flexible for custom inputs.

Summary by CodeRabbit

Release Notes

  • New Features
    • Extended image generation configuration with support for output size, format (PNG/JPEG), and compression quality settings
    • Added predefined constants for common image configurations including aspect ratios (square, 16:9, 9:16, 4:3, 3:4), output resolutions (1K, 2K, 4K), and MIME types

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Nov 30, 2025

Walkthrough

This pull request extends image generation configuration capabilities by introducing new properties and constants. It adds three new constants for image sizing and output formatting to AdditionalPropertiesKeys, creates a utility class with grouped image configuration constants, expands the GenerationConfig.ImageConfig model with ImageSize and ImageOutputOptions properties, introduces the ImageOutputOptions class, and updates the JSON serialization context to include these new types.

Changes

Cohort / File(s) Change Summary
Image Configuration Constants
src/GenerativeAI.Microsoft/AdditionalPropertyKeys.cs, src/GenerativeAI/Constants/ImageConfigValues.cs
Added three public constants for image configuration keys (ImageSize, MimeType, CompressionQuality) to AdditionalPropertiesKeys. Introduced new ImageConfigValues utility class with nested static classes defining aspect ratios (1:1, 16:9, 9:16, 4:3, 3:4), image sizes (1K, 2K, 4K), and output MIME types (PNG, JPEG).
Configuration Model Expansion
src/GenerativeAI/Types/ContentGeneration/Config/GenerationConfig.cs
Expanded ImageConfig with two new properties: ImageSize (string?) and ImageOutputOptions (ImageOutputOptions?). Introduced new ImageOutputOptions class with MimeType (string?) and CompressionQuality (int?) properties.
Integration & Serialization
src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs, src/GenerativeAI/Types/TypesSerializerContext.cs
Extended ToGenerationConfig method to conditionally extract and map image-related properties (ImageSize, MimeType, CompressionQuality) from additional properties into ImageConfig and ImageOutputOptions. Added ImageConfig and ImageOutputOptions to the JSON serialization context with JsonSerializable attributes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify that all new constants in AdditionalPropertiesKeys and ImageConfigValues are correctly named, documented, and align with expected API requirements
  • Confirm property mapping logic in MicrosoftExtensions.ToGenerationConfig correctly instantiates nested objects and handles missing properties gracefully
  • Validate that ImageOutputOptions class integrates properly with ImageConfig and that the serialization context updates are complete and correctly positioned

Possibly related PRs

  • gunpal5/Google_GenerativeAI#83: Extends image-generation configuration by modifying the same core files (AdditionalPropertyKeys, ToGenerationConfig, GenerationConfig.ImageConfig), adding complementary image-related fields.

Poem

🐰 Hop hop, the configs grow!
ImageSize, MimeType flow,
Constants neat, in classes stacked,
New options packed, no turning back!
Serialization set just right,
Image magic, pure delight! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main change: adding image configuration support for Gemini 3 Pro with Microsoft.Extensions.AI integration.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs (1)

345-369: Image config mapping looks good; consider nullable/range handling for CompressionQuality

The new mappings for ImageSize and MimeType are straightforward and correctly initialize ImageConfig / ImageOutputOptions on demand. For CompressionQuality, you currently read into a non-nullable int even though the model property is int?:

if (options.AdditionalProperties.TryGetValue(AdditionalPropertiesKeys.ImageOutputOptionsCompressionQuality, out int compressionQuality))
{
    config.ImageConfig ??= new ImageConfig();
    config.ImageConfig.ImageOutputOptions ??= new ImageOutputOptions();
    config.ImageConfig.ImageOutputOptions.CompressionQuality = compressionQuality;
}

To align with the optional nature of the field and avoid issues if the value is ever stored as nullable, you could switch to int? and guard before assignment:

-        if (options.AdditionalProperties.TryGetValue(AdditionalPropertiesKeys.ImageOutputOptionsCompressionQuality, out int compressionQuality))
-        {
-            config.ImageConfig ??= new ImageConfig();
-            config.ImageConfig.ImageOutputOptions ??= new ImageOutputOptions();
-            config.ImageConfig.ImageOutputOptions.CompressionQuality = compressionQuality;
-        }
+        if (options.AdditionalProperties.TryGetValue(AdditionalPropertiesKeys.ImageOutputOptionsCompressionQuality, out int? compressionQuality)
+            && compressionQuality is not null)
+        {
+            config.ImageConfig ??= new ImageConfig();
+            config.ImageConfig.ImageOutputOptions ??= new ImageOutputOptions();
+            config.ImageConfig.ImageOutputOptions.CompressionQuality = compressionQuality;
+        }

If you want to be stricter, you could also validate that the value is within 0–100 before assigning it, to match the documented range.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d804d26 and c5997b1.

📒 Files selected for processing (5)
  • src/GenerativeAI.Microsoft/AdditionalPropertyKeys.cs (1 hunks)
  • src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs (1 hunks)
  • src/GenerativeAI/Constants/ImageConfigValues.cs (1 hunks)
  • src/GenerativeAI/Types/ContentGeneration/Config/GenerationConfig.cs (5 hunks)
  • src/GenerativeAI/Types/TypesSerializerContext.cs (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs (2)
src/GenerativeAI.Microsoft/AdditionalPropertyKeys.cs (1)
  • AdditionalPropertiesKeys (6-42)
src/GenerativeAI/Types/ContentGeneration/Config/GenerationConfig.cs (2)
  • ImageConfig (339-369)
  • ImageOutputOptions (377-397)
src/GenerativeAI/Types/TypesSerializerContext.cs (1)
src/GenerativeAI/Types/ContentGeneration/Config/GenerationConfig.cs (2)
  • ImageConfig (339-369)
  • ImageOutputOptions (377-397)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build (9.0.x)
🔇 Additional comments (5)
src/GenerativeAI/Types/TypesSerializerContext.cs (1)

22-24: Adding ImageConfig/ImageOutputOptions to the serializer context is correct

Including these DTOs in the source-generated context aligns with the new GenerationConfig.ImageConfig surface and ensures they serialize without reflection.

src/GenerativeAI.Microsoft/AdditionalPropertyKeys.cs (1)

28-41: New image-related AdditionalProperties keys are consistent and well-documented

The added keys for image size, MIME type, and compression quality align with the new ImageConfig / ImageOutputOptions properties and the MicrosoftExtensions mapping; naming and docs are clear and consistent with existing keys.

src/GenerativeAI/Types/ContentGeneration/Config/GenerationConfig.cs (2)

169-175: ThinkingConfig docs and usage remain coherent with MicrosoftExtensions

The ThinkingConfig property on GenerationConfig and the ThinkingConfig class (IncludeThoughts, ThinkingBudget) are documented consistently with how the Microsoft extensions populate them from AdditionalProperties. No functional or shape issues here.

Also applies to: 221-237


213-217: ImageConfig and ImageOutputOptions model the new image-generation options cleanly

The new ImageConfig property on GenerationConfig and the ImageConfig/ImageOutputOptions DTOs:

  • Use the expected JSON property names (imageConfig, aspectRatio, imageSize, imageOutputOptions, mimeType, compressionQuality).
  • Clearly document supported values and reference ImageConfigValues for discoverable constants.
  • Keep all fields optional so existing callers are unaffected when not using image output.

This is a straightforward, backwards-compatible extension of the configuration surface.

Also applies to: 332-397

src/GenerativeAI/Constants/ImageConfigValues.cs (1)

1-59: ImageConfigValues constants are well-structured and aligned with the config model

Centralizing common aspect ratios, image sizes, and output MIME types here matches the new ImageConfig/ImageOutputOptions docs and avoids scattering magic strings. The nested static classes make the API surface easy to discover while keeping it non-restrictive.

@gunpal5 gunpal5 merged commit a0ed5bd into gunpal5:main Dec 5, 2025
1 of 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