> ## Documentation Index
> Fetch the complete documentation index at: https://doc.tokendog.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 结构化输出

> 用 response_format 约束模型返回 JSON

通过 `response_format` 指定 JSON Schema，模型会严格按结构返回，便于程序直接解析。

<CodeGroup>
  ```bash curl theme={null}
  curl https://tokendog.io/v1/chat/completions \
    -H "Authorization: Bearer $TOKENDOG_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model":"gpt-5",
      "messages":[{"role":"user","content":"提取：张三，28岁"}],
      "response_format":{"type":"json_schema","json_schema":{
        "name":"person","schema":{"type":"object",
          "properties":{"name":{"type":"string"},"age":{"type":"integer"}},
          "required":["name","age"],"additionalProperties":false}}}
    }'
  ```

  ```python Python theme={null}
  from openai import OpenAI
  client = OpenAI(base_url="https://tokendog.io/v1", api_key="YOUR_TOKENDOG_API_KEY")
  resp = client.chat.completions.create(
      model="gpt-5",
      messages=[{"role":"user","content":"提取：张三，28岁"}],
      response_format={"type":"json_schema","json_schema":{
          "name":"person","schema":{"type":"object",
              "properties":{"name":{"type":"string"},"age":{"type":"integer"}},
              "required":["name","age"],"additionalProperties":False}}},
  )
  print(resp.choices[0].message.content)
  ```

  ```javascript Node theme={null}
  import OpenAI from "openai";
  const client = new OpenAI({ baseURL:"https://tokendog.io/v1", apiKey:"YOUR_TOKENDOG_API_KEY" });
  const resp = await client.chat.completions.create({
    model:"gpt-5",
    messages:[{role:"user",content:"提取：张三，28岁"}],
    response_format:{type:"json_schema",json_schema:{
      name:"person",schema:{type:"object",
        properties:{name:{type:"string"},age:{type:"integer"}},
        required:["name","age"],additionalProperties:false}}},
  });
  console.log(resp.choices[0].message.content);
  ```
</CodeGroup>

<Tip>返回内容为符合 schema 的 JSON 字符串，可直接用 `JSON.parse` / `json.loads` 解析。</Tip>
