> ## Documentation Index
> Fetch the complete documentation index at: https://docs-vip.apigo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI 结构化输出示例

> OpenAI 结构化 JSON 输出示例。

## 推荐 endpoint

* [OpenAI /v1/responses](/api-reference/endpoints/openai/responses)

## 最小请求

```json theme={null}
{
  "model": "gpt-4.1",
  "input": "从这段文本中提取订单号和金额：订单 A-1024，金额 99.5 美元。",
  "text": {
    "format": {
      "type": "json_schema",
      "name": "order",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "order_id": { "type": "string" },
          "amount": { "type": "number" }
        },
        "required": ["order_id", "amount"],
        "additionalProperties": false
      }
    }
  }
}
```

## cURL 示例

```bash theme={null}
curl https://api-vip.apigo.ai/v1/responses \
  -H "Authorization: Bearer $YOUR API KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4.1",
    "input": "从这段文本中提取订单号和金额：订单 A-1024，金额 99.5 美元。",
    "text": {
      "format": {
        "type": "json_schema",
        "name": "order",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "order_id": { "type": "string" },
            "amount": { "type": "number" }
          },
          "required": ["order_id", "amount"],
          "additionalProperties": false
        }
      }
    }
  }'
```

## Python 示例

```python theme={null}
import json
from openai import OpenAI

client = OpenAI(
    base_url="https://api-vip.apigo.ai/v1",
    api_key="<YOUR API KEY>",
)

response = client.responses.create(
    model="gpt-4.1",
    input="从这段文本中提取订单号和金额：订单 A-1024，金额 99.5 美元。",
    text={
        "format": {
            "type": "json_schema",
            "name": "order",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "order_id": {"type": "string"},
                    "amount": {"type": "number"},
                },
                "required": ["order_id", "amount"],
                "additionalProperties": False,
            },
        }
    },
)

print(json.loads(response.output_text))
```

## Node.js 示例

```js theme={null}
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api-vip.apigo.ai/v1",
  apiKey: process.env.YOUR API KEY,
});

const response = await client.responses.create({
  model: "gpt-4.1",
  input: "从这段文本中提取订单号和金额：订单 A-1024，金额 99.5 美元。",
  text: {
    format: {
      type: "json_schema",
      name: "order",
      strict: true,
      schema: {
        type: "object",
        properties: {
          order_id: { type: "string" },
          amount: { type: "number" }
        },
        required: ["order_id", "amount"],
        additionalProperties: false
      }
    }
  }
});

console.log(JSON.parse(response.output_text));
```

## 最佳实践

* JSON Schema 尽量小而硬，不要一上来就做深层嵌套
* 用 `strict: true` 和 `additionalProperties: false` 降低漂移
* 服务端仍要做二次校验，不要直接信任模型输出
