Skip to content

.NET: Structured Output calling does not work with lists/array if ResponseFormat is used, but works if .RunAsync<> is used #2874

Description

@rwjdk

If you make the following agent and ask for Movies you correctly get 10 movies in an array (r1) and a list (r2) :

AzureOpenAIClient azureOpenAiClient = ...;
ChatClientAgent chatClientAgent = azureOpenAiClient.GetChatClient("gpt-4.1-mini").CreateAIAgent();

ChatClientAgentRunResponse<Movie[]> r1 = await chatClientAgent.RunAsync<Movie[]>("Give me top 10 IMDB Movies");
Movie[] r1Movies = r1.Result;

ChatClientAgentRunResponse<List<Movie>> r2 = await chatClientAgent.RunAsync<List<Movie>>("Give me top 10 IMDB Movies");
List<Movie> r2Movies = r2.Result;

public class Movie
{
    public required string Title { get; set; }
    public required int Year { get; set; }
}

So far so good 👍

Now try the same using the ResponseFormat way (if you don't have a ChatClientAgent due to example middleware and can't use .RunAsync<> anymore 😢)

JsonSerializerOptions jsonSerializerOptions = new()
{
    PropertyNameCaseInsensitive = true,
    TypeInfoResolver = new DefaultJsonTypeInfoResolver(),
    Converters = { new JsonStringEnumConverter() }
};

AzureOpenAIClient azureOpenAiClient = ...;
ChatClientAgent chatClientAgent = azureOpenAiClient.GetChatClient("gpt-4.1-mini").CreateAIAgent(
    new ChatClientAgentOptions
    {
        ChatOptions = new ChatOptions
        {
            ResponseFormat = ChatResponseFormat.ForJsonSchema<Movie[]>(jsonSerializerOptions)
        }
    });
AgentRunResponse agentRunResponse = await chatClientAgent.RunAsync("Give me top 10 IMDB Movies");

Error:

System.ClientModel.ClientResultException: 'HTTP 400 (invalid_request_error: )
Parameter: response_format

Invalid schema for response_format 'Movie__': schema must be a JSON Schema of 'type: "object"', got 'type: "array"'.'

So the ResponseFormat does not work with List and Arrays, while the .RunAsync<>() does (only plain objects).

Metadata

Metadata

Labels

.NETUsage: [Issues, PRs], Target: .NetagentsUsage: [Issues, PRs], Target: Single agent

Type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions