Skip to content

.NET feedback: Structured output feels more cumbersome in AF + Have issues with Enums in result object #1057

@rwjdk

Description

@rwjdk

(Based on how it is done in the sample: Agent_Step05_StructuredOutput)

After playing around with structured Output in AF, it feels like a step down from MEAI and SK

  1. MEAI feels like it has the best solution, as you can do something like this:
string question = "What is the capital of Denmark and what is its population?";
ChatResponse<City> structuredOutputResponse = await chatClient.GetResponseAsync<City>(question);
City result = structuredOutputResponse.Result;

//(AF do not have a similar agent.RunAsync<City>(question)) ☹️
  1. SK was a bit more cumbersome than MEAI with the need to set the typeof() and manual deserialization
    ChatCompletionAgent agent = new()
    {
        Kernel = kernel,
        Instructions = "You are a City Expert",
        Arguments = new KernelArguments(new AzureOpenAIPromptExecutionSettings
        {
            ResponseFormat = typeof(City)
        })
    };
    
    ...
    
    await foreach (AgentResponseItem<ChatMessageContent> response in agent.InvokeAsync(message))
    {
        string json = response.Message.Content!;
        City? city = JsonSerializer.Deserialize<City>(json, new JsonSerializerOptions
        {
            Converters = { new JsonStringEnumConverter() }
        });
    }
    1. AF, however, feels even more cumbersome than SK
    ChatOptions = new()
    {
        ResponseFormat = ChatResponseFormat.ForJsonSchema<City>()
    }

    ...And if the Structured Output object has Enums, you need the following JSON Serializer options for it to work in AF

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

    (It even seems to generate the incorrect schema if Enums are inside nested objects in the Structured output object, giving back non-existing enum values as the schema does not generate the enum valid values)


    It would be cool if AF could have the MEAI approach...


    For context, here is the City and Underlying Enum

    public class City
    {
        public required string NameOfCapital { get; set; }
        public required string Population { get; set; }
        public required Size SizeOfCity { get; set; }
    }
    
    public enum Size
    {
        RandomValue1, //values AI can't guess randomly
        RandomValue2 //values AI can't guess randomly
    }

Metadata

Metadata

Labels

Type

Projects

Status

Planned

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions