结构化输出

结构化输出让模型按照你指定的 JSON 格式返回数据,适用于数据提取、分类标注、表单填充等场景。

JSON Mode#

最简单的结构化输出方式,强制模型返回合法 JSON:

Python

代码
from openai import OpenAI
 
client = OpenAI(
    base_url="https://as.apinoma.com/v1",
    api_key="<你的 APINOMA_API_KEY>"
)
 
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[
        {"role": "system", "content": "你是一个数据提取助手。请以 JSON 格式返回结果。"},
        {"role": "user", "content": "提取以下文本中的人名、公司和职位:张三是阿里巴巴的高级工程师"}
    ],
    response_format={"type": "json_object"}
)
 
import json
result = json.loads(response.choices[0].message.content)
print(result)
# {"name": "张三", "company": "阿里巴巴", "title": "高级工程师"}

TypeScript

使用 JSON Mode 时,system prompt 中必须包含 “JSON” 关键词,否则部分模型可能忽略格式要求。

JSON Schema 约束#

更精确地控制输出结构,确保字段名称和类型符合预期:

代码
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[
        {"role": "user", "content": "分析这段用户评论的情感:这个产品太棒了,非常好用!"}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "sentiment_analysis",
            "schema": {
                "type": "object",
                "properties": {
                    "sentiment": {
                        "type": "string",
                        "enum": ["positive", "negative", "neutral"],
                        "description": "情感倾向"
                    },
                    "confidence": {
                        "type": "number",
                        "description": "置信度 0-1"
                    },
                    "keywords": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "关键情感词"
                    }
                },
                "required": ["sentiment", "confidence", "keywords"],
                "additionalProperties": False
            }
        }
    }
)

输出:

实际应用场景#

数据提取

代码
# 从非结构化文本提取结构化数据
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{
        "role": "user",
        "content": """提取以下订单信息:
        客户李四于2025年1月15日下单购买了3台MacBook Pro,
        单价18999元,收货地址是北京市朝阳区xxx路123号"""
    }],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "order_info",
            "schema": {
                "type": "object",
                "properties": {
                    "customer": {"type": "string"},
                    "date": {"type": "string"},
                    "items": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string"},
                                "quantity": {"type": "integer"},
                                "unit_price": {"type": "number"}
                            }
                        }
                    },
                    "address": {"type": "string"}
                },
                "required": ["customer", "date", "items", "address"]
            }
        }
    }
)

分类标注

支持的模型#

模型

JSON Mode

JSON Schema

`openai/gpt-4o`

`openai/gpt-4o-mini`

`anthropic/claude-sonnet-4.6`

`google/gemini-pro-compatible-flash-lite-preview`

不支持 JSON Schema 的模型可以通过在 system prompt 中详细描述期望的 JSON 格式来实现类似效果。

最后更新于 2026年6月23日