The following example shows how to enable JSON mode in OpenAIChatAgent.

What is JSON mode?

JSON mode is a new feature in OpenAI which allows you to instruct model to always respond with a valid JSON object. This is useful when you want to constrain the model output to JSON format only.

Note

Currently, JOSN mode is only supported by gpt-4-turbo-preview and gpt-3.5-turbo-0125. For more information (and limitations) about JSON mode, please visit OpenAI API documentation.

How to enable JSON mode in OpenAIChatAgent.

To enable JSON mode for OpenAIChatAgent, set responseFormat to ChatCompletionsResponseFormat.JsonObject when creating the agent. Note that when enabling JSON mode, you also need to instruct the agent to output JSON format in its system message.

var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
var model = "gpt-4o-mini";

var openAIClient = new OpenAIClient(apiKey);
var openAIClientAgent = new OpenAIChatAgent(
    chatClient: openAIClient.GetChatClient(model),
    name: "assistant",
    systemMessage: "You are a helpful assistant designed to output JSON.",
    seed: 0, // explicitly set a seed to enable deterministic output
    responseFormat: ChatResponseFormat.CreateJsonObjectFormat()) // set response format to JSON object to enable JSON mode
    .RegisterMessageConnector()
    .RegisterPrintMessage();

After enabling JSON mode, the openAIClientAgent will always respond in JSON format when it receives a message.

var reply = await openAIClientAgent.SendAsync("My name is John, I am 25 years old, and I live in Seattle.");

var person = JsonSerializer.Deserialize<Person>(reply.GetContent());
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");

if (!string.IsNullOrEmpty(person.Address))
{
    Console.WriteLine($"Address: {person.Address}");
}

Console.WriteLine("Done.");

When running the example, the output from openAIClientAgent will be a valid JSON object which can be parsed as Person class defined below. Note that in the output, the address field is missing because the address information is not provided in user input.

public class Person
{
    [JsonPropertyName("name")]
    public string Name { get; set; }

    [JsonPropertyName("age")]
    public int Age { get; set; }

    [JsonPropertyName("address")]
    public string Address { get; set; }
}

The output will be:

Name: John
Age: 25
Done