Skip to content

Agent Structured Output

Overview

Tornado agents can enforce structured JSON output schemas, ensuring responses conform to predefined C# types. This enables type-safe processing of AI responses.

Quick Start

csharp
using LlmTornado.Agents;

TornadoAgent agent = new TornadoAgent(
    client: api,
    model: ChatModel.OpenAi.Gpt41.V41Mini,
    instructions: "Extract contact information from text.",
    outputSchema: typeof(ContactInfo)
);

Conversation result = await agent.RunAsync(
    "John Doe, john@example.com, +1234567890"
);

ContactInfo? contact = JsonConvert.DeserializeObject<ContactInfo>(
    result.Messages.Last().Content
);

public class ContactInfo
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}

Defining Schemas

csharp

TornadoAgent agent = new TornadoAgent(
    api,
    ChatModel.OpenAi.Gpt41.V41Mini,
    outputSchema: typeof(TaskBreakdown)
);

[JsonConverter(typeof(StringEnumConverter))]
public enum PriorityStatus
{
    Low, 
    High
}

[Description("Describe how to break down the task")]
public class TaskBreakdown
{
    public string Title { get; set; }
    public int EstimatedHours { get; set; }
    [Description("Sequential steps to take in order to complete")]
    public List<string> Steps { get; set; }
    public PriorityStatus Priority { get; set; }
}

Dynamic Schema Updates

csharp
TornadoAgent agent = new TornadoAgent(api, model, outputSchema: typeof(TypeA));

// Change schema
agent.UpdateOutputSchema(typeof(TypeB));

// Disable structured output
agent.UpdateOutputSchema(null);

Best Practices

  • Use explicit property names and types
  • Add XML documentation to schema classes
  • Validate deserialized objects
  • Handle null/missing fields gracefully