Skip to content

.Net: Feature gpt image 1 support - #12866

Open
Ramin Ahmadi (AhmadiRamin) wants to merge 3 commits into
microsoft:mainfrom
AhmadiRamin:feature-gpt-image-1-support
Open

Ramin Ahmadi (AhmadiRamin) wants to merge 3 commits into
microsoft:mainfrom
AhmadiRamin:feature-gpt-image-1-support

Conversation

@AhmadiRamin

@AhmadiRamin Ramin Ahmadi (AhmadiRamin) commented Aug 5, 2025 •

Copy link
Copy Markdown

Summary

This PR adds support for Azure OpenAI and OpenAI image generation models using the new Microsoft.Extensions.AI abstractions, enabling developers to use gpt-image-1, DALL-E 3, and other image models through Semantic Kernel's familiar registration patterns.

Motivation and Context

  • gpt-image-1 is OpenAI's latest image generation model with improved quality and capabilities
  • Developers need a consistent way to integrate image generation into SK-based applications
  • Aligns with SK's migration to Microsoft.Extensions.AI abstractions instead of internal SK services
  • Provides the familiar SK developer experience while using modern, standardized abstractions

Changes

Added Microsoft.Extensions.AI Integration

  • AddAzureOpenAIImageGenerator() - Azure OpenAI image generation support
  • AddOpenAIImageGenerator() - OpenAI image generation support
  • Uses IImageGenerator from Microsoft.Extensions.AI instead of internal ITextToImageService
  • Follows the same patterns as AddAzureOpenAIChatClient and AddAzureOpenAIEmbeddingGenerator

Multi-Modal Support

  • Seamlessly integrates with existing chat completion and embedding services
  • Enables complex multi-modal scenarios through SK's orchestration capabilities
  • Works with SK's agent framework and plugin ecosystem

Usage Examples

Basic Setup

var builder = Kernel.CreateBuilder();

// Add image generation support for gpt-image-1
builder.AddAzureOpenAIImageGenerator(
    deploymentName: "gpt-image-1",
    endpoint: "https://your-resource.openai.azure.com/",
    apiKey: "your-api-key");

var kernel = builder.Build();
var imageGenerator = kernel.GetRequiredService<IImageGenerator>();

Multi-Modal Application

var builder = Kernel.CreateBuilder();

// Add multiple AI services
builder.AddAzureOpenAIChatCompletion("gpt-4", endpoint, apiKey);
builder.AddAzureOpenAIImageGenerator("gpt-image-1", endpoint, apiKey);
builder.AddAzureOpenAIEmbeddingGenerator("text-embedding-ada-002", endpoint, apiKey);

// SK orchestrates all services automatically
var kernel = builder.Build();

Usage Example

// Configure and build kernel with gpt-image-1 support
var builder = Kernel.CreateBuilder();

// New approach using Microsoft.Extensions.AI
builder.AddAzureOpenAIImageGenerator(
	deploymentName: "gpt-image-1",
	endpoint: "ENDPOINT",
	apiKey: "APIKEY");

var kernel = builder.Build();
var imageGenerator = kernel.GetRequiredService<IImageGenerator>();

var contents = await imageGenerator.GenerateImagesAsync(
	"A cute cat wearing a wizard hat, sitting on a pile of books, with a magical wand in its paw.",
	new Microsoft.Extensions.AI.ImageGenerationOptions
	{
		Count = 1,
		RawRepresentationFactory = generator =>
		{
			// Return OpenAI-specific options with gpt-image-1 features
			return new OpenAI.Images.ImageGenerationOptions
			{
				Quality = GeneratedImageQuality.Low,
				OutputFileFormat = GeneratedImageFileFormat.Jpeg,
				Size = GeneratedImageSize.W1024xH1024
			};
		}
	});

// Editing
string filePath = @"C:\MySemanticKernelTest\car.jpg";
byte[] imageBytes = File.ReadAllBytes(filePath);
var editedImage = await imageGenerator.EditImageAsync(new DataContent(imageBytes, "jpeg"), "Make the car's colour Red", new Microsoft.Extensions.AI.ImageGenerationOptions
{
	Count = 1,
	RawRepresentationFactory = generator =>
	{
		// Return OpenAI-specific options with gpt-image-1 features
		return new OpenAI.Images.ImageGenerationOptions
		{
			Quality = GeneratedImageQuality.Low,
			OutputFileFormat = GeneratedImageFileFormat.Jpeg,
			Size = GeneratedImageSize.W1024xH1024
		};
	}
});

Contribution Checklist

@AhmadiRamin
Ramin Ahmadi (AhmadiRamin) requested a review from a team as a code owner August 5, 2025 18:00
@moonbox3 Evan Mattson (moonbox3) added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core labels Aug 5, 2025
@github-actions github-actions Bot changed the title Feature gpt image 1 support .Net: Feature gpt image 1 support Aug 5, 2025
@AhmadiRamin

Copy link
Copy Markdown
Author

Hi Evan Mattson (@moonbox3)

What's next in the merging process?

Are we waiting for microsoft/octo-semantickernel-pr-dotnet to review the code?

/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Generated image contents</returns>
[Experimental("SKEXP0001")]
Task<IReadOnlyList<ImageContent>> GenerateEditImageAsync(

@rogerbarreto Roger Barreto (rogerbarreto) Aug 19, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the contribution.

Recently we have new Text-to-Image abstractions available in Microsoft.Extensions.AI including openai implementations available for editing images as well.

Due to that we will be moving away from our experimental abstractions for Text-to-Image in favor of the new experimental M.E.AI abstractions using the IImageGenerator interface.

Suggest updating this PR to move our existing OpenAITextToImageService internal implementations using the new OpenAIImageGenerator, without our specific logic.

package: Microsoft.Extensions.AI.OpenAI

using Microsoft.Extensions.AI;

var imageGenerator = azureOpenAiClient.GetImage().AsIImageGenerator();

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks Roger Barreto (@rogerbarreto) for your comments, I will update the PR as you suggested.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi Roger Barreto (@rogerbarreto), I've updated the PR, please let me know if you want to update anything else.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Roger Barreto (@rogerbarreto) Have you had a chance to review this?

Keep existing SK image contracts while using MEAI for generation.
Support native generation and editing registrations with telemetry.

Refs microsoft#12866

Copilot AI 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.

🔵 Needs a closer look

Runtime compatibility of the shared legacy image path with the experimental adapters across both providers remains unverified.

No open issues 🎉

What changed in this PR

Adds OpenAI and Azure OpenAI image generation through Microsoft.Extensions.AI, using Semantic Kernel’s existing registration patterns while adapting its legacy text-to-image services.

Changes:

  • Adds IImageGenerator registrations with telemetry and logging support.
  • Routes legacy image generation through the new abstraction and supports data URI results.
  • Adds tests for registration, generation, editing, settings preservation, and error handling.
File Description
dotnet/​src/​SemanticKernel.Abstractions/​AI/​TextToImage/​TextToImageServiceExtensions.cs Returns image URLs or data URIs.
dotnet/​src/​Connectors/​Connectors.OpenAI/​Services/​OpenAITextToImageService.cs Documents delegation and the new registration API.
dotnet/​src/​Connectors/​Connectors.OpenAI/​Extensions/​OpenAIServiceCollectionExtensions.DependencyInjection.cs Adds OpenAI image-generator service registrations.
dotnet/​src/​Connectors/​Connectors.OpenAI/​Extensions/​OpenAIKernelBuilderExtensions.cs Adds OpenAI kernel-builder registration overloads.
dotnet/​src/​Connectors/​Connectors.OpenAI/​Core/​ClientCore.TextToImage.cs Adapts legacy requests and responses through IImageGenerator.
dotnet/​src/​Connectors/​Connectors.OpenAI.UnitTests/​Services/​OpenAITextToImageServiceTests.cs Tests data URIs, settings, errors, and cancellation.
dotnet/​src/​Connectors/​Connectors.OpenAI.UnitTests/​Extensions/​ServiceCollectionExtensionsTests.cs Tests registration, telemetry, generation, and editing.
dotnet/​src/​Connectors/​Connectors.OpenAI.UnitTests/​Extensions/​KernelBuilderExtensionsTests.cs Tests kernel-builder image-generator registration.
dotnet/​src/​Connectors/​Connectors.AzureOpenAI/​Services/​AzureOpenAITextToImageService.cs Documents delegation and the new registration API.
dotnet/​src/​Connectors/​Connectors.AzureOpenAI/​Extensions/​AzureOpenAIServiceCollectionExtensions.DependencyInjection.cs Adds Azure image-generator service registrations.
dotnet/​src/​Connectors/​Connectors.AzureOpenAI/​Extensions/​AzureOpenAIKernelBuilderExtensions.cs Adds Azure kernel-builder registration overloads.
dotnet/​src/​Connectors/​Connectors.AzureOpenAI.UnitTests/​Services/​AzureOpenAITextToImageServiceTests.cs Tests data URIs and preservation of Azure request settings.
dotnet/​src/​Connectors/​Connectors.AzureOpenAI.UnitTests/​Extensions/​AzureOpenAIServiceCollectionExtensionsTests.cs Tests registration, authentication, telemetry, and editing.
dotnet/​src/​Connectors/​Connectors.AzureOpenAI.UnitTests/​Extensions/​AzureOpenAIKernelBuilderExtensionsTests.cs Tests Azure kernel-builder image-generator registration.

Review effort: Balanced

This branch was previously deployed

1 inactive deployment
github-app-auth — f3cd3001 Deployed Oct 21, 2026 by rogerbarreto via add_label #29407
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel.core kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

Status: Community PR

Development

Successfully merging this pull request may close these issues.

5 participants