Skip to content

.Net: Bug: Inconsistent Handling of ResponseFormat Type in AzureOpenAIPromptExecutionSettings Between IChatCompletionService and IChatClient #12311

Open
@runceel

Description

@runceel

Describe the bug
When using AzureOpenAIPromptExecutionSettings.ResponseFormat with a System.Type (e.g., typeof(Person)), there is inconsistent behavior between IChatCompletionService and IChatClient.

  • IChatCompletionService returns structured output as expected.
  • IChatClient throws a serialization exception:
Serialization and deserialization of 'System.RuntimeType' instances is not supported. The unsupported member type is located on type 'System.Object'. Path: $.ResponseFormat.
at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, Exception innerException)
at System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)   at System.Text.Json.Serialization.Metadata.JsonTypeInfo1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.SerializeAsObject(Utf8JsonWriter writer, Object rootValue)
at System.Text.Json.JsonSerializer.WriteStringAsObject(Object value, JsonTypeInfo jsonTypeInfo)
at Microsoft.SemanticKernel.PromptExecutionSettingsExtensions.ToChatOptions(PromptExecutionSettings settings, Kernel kernel)
at Program.<<Main>$>d__0.MoveNext() (Program.cs:line 36)

To Reproduce
Steps to reproduce the behavior:

  1. Set up a Semantic Kernel project with both IChatCompletionService and IChatClient using Azure OpenAI.
  2. Set ResponseFormat = typeof(Person) in AzureOpenAIPromptExecutionSettings.
  3. Call both GetChatMessageContentAsync (via IChatCompletionService) and GetResponseAsync (via IChatClient).
  4. Observe that IChatClient throws a serialization exception, while IChatCompletionService works as expected.

Expected behavior
Both IChatCompletionService and IChatClient should handle the ResponseFormat type consistently, enabling structured output and returning the result in JSON format.

Screenshots
N/A (see exception message above)

Platform

  • Language: C#
  • Source: NuGet package Microsoft.SemanticKernel 1.54.0,
  • AI model: Azure OpenAI Service, gpt-4.1
  • IDE: Visual Studio 2022
  • OS: Windows 11

Additional context

This difference in behavior was noticed when switching from AddAzureOpenAIChatCompletion to AddAzureOpenAIChatClient while using ChatCompletionAgent, which resulted in the exception.

Minimal repro:

using Microsoft.Extensions.AI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;

const string Endpoint = "<<your aoai endpoint>>";
const string DeploymentName = "<<your deployment model name>>";
const string ApiKey = "<<your api key>>";

var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(
    DeploymentName,
    Endpoint,
    ApiKey,
    serviceId: "ChatCompletion");
builder.AddAzureOpenAIChatClient(
    DeploymentName,
    Endpoint,
    ApiKey,
    serviceId: "ChatClient");

var kernel = builder.Build();

IChatCompletionService chatCompletionService = kernel.GetRequiredService<IChatCompletionService>("ChatCompletion");
IChatClient chatClient = kernel.GetRequiredService<IChatClient>("ChatClient");

const string input = "My name is Jone Doe, I am 30 years old.";

var chatCompletionResponse = await chatCompletionService.GetChatMessageContentAsync(input,
    new AzureOpenAIPromptExecutionSettings
    {
        ResponseFormat = typeof(Person),
    });
Console.WriteLine(chatCompletionResponse.Content);

var chatClientResponse = await chatClient.GetResponseAsync([new ChatMessage(ChatRole.User, input)],
    options: new AzureOpenAIPromptExecutionSettings
    {
        ResponseFormat = typeof(Person),
    }.ToChatOptions(kernel));
Console.WriteLine(chatClientResponse.Text);

record Person(string Name, int Age);

Metadata

Metadata

Assignees

Labels

.NETIssue or Pull requests regarding .NET codebugSomething isn't working

Type

Projects

Status

Bug

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions