Structured output
What it is
Structured output means the model answers in a fixed, machine-readable format, usually JSON that matches a schema you supply. The schema states:
- which fields must be present;
- the type of each (text, number, true or false, one of a fixed list);
- which are optional.
Most providers can now guarantee that the output matches the schema.
Why it matters
Free text is for people; code needs fields.
- To route a ticket, your program needs
category: "refund", not a sentence it has to interpret. - A schema also makes the model's job narrower, which makes it more reliable, and gives you something exact to validate.
In the real world: A support system classifies every incoming message into one of eight categories with a fixed schema, and routes it to the right queue without a person reading it first.
from pydantic import BaseModel
from typing import Literal
class Ticket(BaseModel):
category: Literal["refund", "delivery", "payment", "other"]
order_id: str | None
urgent: bool
summary: str
ticket = client.chat(messages=messages, response_format=Ticket)
ticket.category # 'refund': a value your code can branch onAsking for JSON in the prompt and parsing whatever comes back. Without a schema enforced by the provider, the model will sometimes add a sentence before the JSON, leave out a field or invent a category. Use the provider's schema feature where it exists.