📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

When should you use structured output (a JSON schema response) instead of tool calling, and how do you validate the result in .NET?

Mid-Level Coding Intermediate ai fluentvalidation function-calling json-schema microsoft-extensions-ai structured-output

🎯 Key points

  • Structured output = the final answer constrained to a JSON schema
  • Microsoft.Extensions.AI GetResponseAsync<T>() builds the schema from T and deserialises
  • Valid JSON is not correct data: validate business rules with FluentValidation
  • Tools = C# methods (AIFunctionFactory.Create) the model calls mid-conversation
  • Tools are for fetching live data or acting; structured output for shaping the answer
  • One call has fewer failure modes, less latency and cost than a tool loop
  • Retry or reject when validation fails

⏱ 30-second answer

Structured output constrains the model's final answer to a JSON schema you send. Use it when you want data in a fixed shape: extract invoice fields, or classify a ticket with a reason. In the Microsoft.Extensions.AI package (the standard .NET AI abstractions), IChatClient.GetResponseAsync<T>() generates the schema from T and deserialises the reply into ChatResponse<T>. A valid shape can still hold wrong data, so validate the values (ranges, enums, totals that add up) with FluentValidation or data annotations, and retry or reject on failure.

Tools (function calling) are C# methods you describe to the model with AIFunctionFactory.Create. The model asks to call them mid-conversation to fetch live data ("look up order 123") or take an action, and your code runs them and returns the result. Use tools only when the model needs something it doesn't have. Structured output is one call with fewer failure modes than a tool loop.

🔍 What the interviewer is checking

Checks they pick the simplest mechanism for the job and never trust model output without validation.

Green flags

  • Separates shaping the answer (schema) from fetching data or acting (tools)
  • Names GetResponseAsync
  • Plans what happens when validation fails

Red flags

  • Uses a tool loop just to get JSON back
  • Treats schema-valid output as correct

↩ Usually asked after

More in .NET and AI

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me