Notes

Structured Output (Agent)

A language model's natural output is prose, and prose is fine for a reader and useless to a program. Structured output is the constraint that makes an answer machine-readable: the response must conform to a declared shape — typically a JSON object with named, typed fields — so the next line of code can consume it without guessing.

Asking versus enforcing
Asking politely in the prompt for valid JSON works until it doesn't, and the failure arrives as a parse error on a response wrapped in a markdown fence. Enforcement is the real mechanism:

  • Schema-constrained decoding — the serving layer restricts which tokens it will sample, so the text cannot leave the grammar. Invalid JSON becomes impossible rather than unlikely.
  • Typed response models — libraries such as Pydantic AI, and the structured-output modes of the major APIs, take a schema, hand it to the model, and return a validated object.
  • Validate and repair — the fallback: parse, catch the error, feed it back for one correction attempt.

Why an agent depends on it
In an agent loop every decision is read by code, not by a person. A tool call is structured output: a function name plus arguments matching the tool schema. So is a plan's list of subgoals, a router's choice of worker, and the verdict from an LLM-as-judge step. When the shape breaks, the loop does not degrade gracefully — the orchestrator has nothing it can act on, the iteration is spent on repair, and an agent that keeps re-emitting the same malformed field is one of the commonest ways a step budget is exhausted with nothing to show.

Structured output constrains a model's response to a declared schema — typically JSON with named, typed fields — so program code can consume it directly instead of parsing prose. It is enforced by schema-constrained decoding or typed validation, not by asking in the prompt. Every tool call, plan and routing decision in an agent loop is structured output: when the shape breaks, the orchestrator has nothing to act on and the iteration is lost to repair.

Imagine a courier who has to bring back a delivery note. If they return with a chatty paragraph about their afternoon, the warehouse computer cannot file it. Give them a form with boxes — name, address, time — and it can.

Structured output is that form, for AI. Instead of letting the model answer in free-flowing sentences, the software insists the answer comes back in a fixed layout with the right boxes filled in.

This matters most when the AI is working on its own, because the next thing to read its answer is not a person — it is more software. A tidy answer keeps the job moving. A rambling one stops it dead, and the agent burns a turn trying again.