How it works

By default, an LLM responds in free text. That's great for conversation, but bad for code integration: you need to parse the response, handle format variations and deal with errors.

Structured output solves this. You define a schema, usually JSON Schema, describing the expected fields, and the model is forced to respond within that format. The response arrives as valid JSON, ready to use in code.

Implementation varies: some providers, OpenAI and Anthropic, offer native parameters like response_format or tool use. Others use libraries like Instructor or Outlines that enforce the format on the client side.

Practical example

Imagine extracting information from a customer email. Without structured output, the model responds: "Customer João Silva wants to cancel order 12345, reason: delivery delay." You need to parse this with regex or another LLM.

With structured output, you define a schema with fields {nome, acao, pedido_id, motivo} and the model responds directly: {"nome": "João Silva", "acao": "cancelar", "pedido_id": "12345", "motivo": "atraso na entrega"}. No parsing, no format errors.

What it's for

It is used to integrate LLMs with systems: APIs, databases, data pipelines and agents. It removes the fragile step of parsing free text and reduces format errors.

Difference between structured output and JSON mode

JSON mode ensures the response is valid JSON, but does not guarantee it follows a specific schema. Structured output goes further: it guarantees that fields and types match the defined schema. JSON mode is the minimum, structured output is the ideal.