Zubnet AI学习Wiki › Structured Output
Using AI

Structured Output

JSON Mode, Constrained Generation
让 AI 模型以特定的、机器可解析的格式响应 — 通常是 JSON、XML,或定义好的 schema。不是自由文本,模型产出你的代码能可靠解析的数据。大多数供应商现在原生支持结构化输出:你定义一个 schema,模型保证它的响应符合这个 schema。

为什么重要

当你从聊天机器人转向应用的那一刻,你就需要结构化输出。你的代码没法解析“当然!这是结果:”后面跟一个项目列表。你需要 {"results": [...]}。结构化输出就是让 AI 可以作为软件组件使用的东西,而不只是文本生成器。

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.

相关概念

← 所有术语
← Streaming Suno →