# 12.2 结构化输出

> 别让 AI 写作文，让它“填表”。请使用 Markdown 表格/JSON 格式输出。

## 12.2.1 AI 天生爱唠叨

如果你问 AI：“这是哪三款手机的对比？”

它通常会给你写一篇像博客一样的长文：

> “好的，这三款手机分别是 A、B 和 C。A 的特点是像素高，屏幕大... 而 B 的续航很好，但是有点重... C 则是...”

看着很累，重点难找，复制粘贴也很麻烦。

这是因为 AI 默认是在 **“聊天”（Chat）**，而不是在\*\*“处理数据”（Data Processing）\*\*。

## 12.2.2 驾驭 AI 的缰绳：格式限制

对付“话痨”最好的办法，就是直接扔给它一个 **“框”**。

我们不仅要告诉它 **“内容是什么”**，还要规定它\*\*“长什么样”\*\*。

### 场景 1：做竞品分析（使用表格）

> “请对比 iPhone 15 和 Pixel 8。 **请使用 Markdown 表格形式输出**，包含以下列：型号、屏幕尺寸、处理器、价格、核心卖点。”

结果瞬间清爽：

| 型号        | 屏幕尺寸 | 处理器       | 价格   | 核心卖点         |
| --------- | ---- | --------- | ---- | ------------ |
| iPhone 15 | 6.1寸 | A16       | $799 | 生态系统、视频拍摄    |
| Pixel 8   | 6.2寸 | Tensor G3 | $699 | AI 拍照功能、原生安卓 |

### 场景 2：程序员写代码（使用 JSON）

> “帮我提取这段新闻里的人物、地点和时间。 **不要废话，直接输出 JSON 格式**，字段为：`person`, `location`, `time`。”

结果直接可用：

```json
{
  "person": "Sam Altman",
  "location": "San Francisco",
  "time": "2023-11-17"
}
```

## 12.2.3 为什么要结构化？

结构化输出不仅仅是为了好看，它有两个巨大的价值：

1. **可读性（Readability）**：信息密度极大提升，一目了然。
2. **可连接性（Connectivity）**：结构化的数据（如 JSON/CSV）可以直接被 Excel、代码或其他软件读取。这时，AI 就不再只是一个聊天机器人，而变成了一个**数据预处理器**。

> \[!NOTE] **API 级别的强校验 (JSON Schema)**
>
> 如果仅依靠“请输出 JSON”的自然语言提示，模型在复杂场景下依然可能输出错误格式（例如少了个逗号、或多废话几句代码注释）。在工业级生产环境中，我们不仅要求输出格式，还需要进行 **机器强约束与校验**：
>
> 1. **Structured Outputs / Function Calling**：现代模型 API（如 OpenAI 的 Structured Outputs）支持直接在发送 API 请求时附带 `JSON Schema`（数据结构定义），通过引擎底层甚至是生成采样过程（Sampling）强迫其100%符合结构。
> 2. **本地校验阻断**：即使收到了 JSON 内容，也要利用代码库（如 Pydantic 或 jsonschema）直接验证拦截。一旦失败立刻抛弃或交回模型自动重试。
>
> **代码实验**： 请参阅本书配套的 [`labs/02_structured_output.py`](https://github.com/yeasy/ai_beginner_guide/blob/main/labs/02_structured_output.py)。其中展现了如何利用 Python `jsonschema` 对 AI 生成的 JSON 数据格式执行本地化严谨校验并捕捉错误。

## 12.2.4 思考题

回想一下你最近一次让 AI 帮你做总结。

如果当时你加上一句 **“请用列表形式展示核心观点”或者“请生成一个思维导图格式（Markdown）”**，效果会不会好很多？

下次试试看。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yeasy.gitbook.io/ai_beginner_guide/di-san-bu-fen-shi-zhan-ying-yong-ji-qiao/12_prompt_advanced/12.2_structured_output.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
