> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.beatapi.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.beatapi.io/_mcp/server.

# 要求模型返回 JSON，为什么还是带代码块或解析失败？

> 要稳定解析 JSON 输出，需在 POST /v1/chat/completions 中启用原生 response_format，并核对 finish_reason 与 choices[0].message.content 的 schema 校验结果。

仅靠提示词要求 JSON，模型仍可能输出 Markdown 代码块、附加说明，或在触达 token 上限时截断对象。稳定解析需要同时满足两个条件：请求启用原生 `response_format`，且回复完整结束。

## 使用原生结构化输出

先确认目标模型支持原生 `POST /v1/chat/completions` 的 `response_format`，再在请求体中声明结构化 JSON Schema，避免仅靠自然语言约束。同时给 `max_tokens` 留出足够空间（不超过模型文档列出的 `max_output_tokens`），防止 JSON 中途截断。

```json
{
  "model": "gpt-6-luna",
  "messages": [
    {
      "role": "user",
      "content": "对工单分类并用一句话总结问题。"
    }
  ],
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "ticket_triage",
      "schema": {
        "type": "object",
        "properties": {
          "category": { "type": "string" },
          "summary": { "type": "string" }
        },
        "required": ["category", "summary"],
        "additionalProperties": false
      }
    }
  }
}
```

通过 Capabilities 调用模型时，先查看该模型在 Capabilities 公开的输入字段，确认支持后再传格式参数。

## 校验结束状态与业务 Schema

1. 解析前先检查 `choices[0].finish_reason`。只有 `stop` 代表输出完整；截断和拒答需分别记录为独立状态，不直接做 JSON 解析。
2. 解析 `choices[0].message.content`，并按业务 Schema 校验 `category`、`summary` 等必填字段。HTTP `200` 只代表传输成功，不保证字段完全符合业务约束。
3. 解析失败或 Schema 校验失败的记录转入人工复核，防止异常结构进入后续自动化流程。

## 合同与相关问题

发送收费请求前，先免费查看模型合同：`model:gpt-6-luna`, `model:gpt-image-2.5-flare-std`。

[返回AI 模型问题索引](/zh/faq/models)。