Zubnet AIAprenderWiki › Structured Output
Using AI

Structured Output

JSON Mode, Constrained Generation
Fazer um modelo de IA responder em um formato específico e parseável por máquina — tipicamente JSON, XML ou um schema definido. Em vez de texto livre, o modelo produz dados que seu código pode parsear de forma confiável. A maioria dos provedores agora suporta saída estruturada nativamente: você define um schema, e o modelo garante que sua resposta se conforme a ele.

Por que importa

No momento em que você passa de chatbot para aplicação, precisa de saída estruturada. Seu código não pode parsear “Claro! Aqui estão os resultados:” seguido de uma lista. Você precisa de {"results": [...]}. Saída estruturada é o que torna IA usável como componente em software em vez de só um gerador de texto.

Deep Dive

There are three levels of structured output support. Prompt-based: you ask the model to "respond in JSON" and hope it complies (unreliable for complex schemas). Constrained decoding: the provider modifies the sampling process to only allow tokens that keep the output valid according to your schema (reliable, but can affect quality). Schema-validated: the provider guarantees conformance by retrying or repairing invalid outputs before returning them.

JSON Schema Is the Standard

Most providers (Anthropic, OpenAI, Google) converge on JSON Schema as the way to define structured outputs. You specify the expected fields, their types, which are required, and any enums or constraints. The model then generates JSON that matches. This works well for flat structures and moderately nested ones. Very deeply nested or recursive schemas can confuse models, and extremely large schemas eat into your context window.

When Not to Use It

Structured output isn't always the right choice. For creative writing, summarization, or explanations, free-form text is better. For data extraction, classification, or API integrations, structured output is essential. The middle ground — extracting structured data from unstructured input — is where it shines brightest. "Read this resume and extract: name, email, years of experience, top 3 skills" with a JSON schema is a solved problem.

Conceitos relacionados

← Todos os termos
← Streaming Suno →