# GPT 结构化输出与 PDF 输入:JSON Schema、response_format Source: https://ofox.ai/zh/docs/develop/models/gpt/structured-output 本页说明 GPT 系列的结构化输出与 PDF 输入写法;模型 ID 与接口配置见 [GPT 系列概览](https://ofox.ai/zh/docs/develop/models/gpt)。 ## 结构化输出 通过 `response_format` 传入 JSON Schema,`strict: true` 时输出严格符合 Schema: ```python filename="gpt_json_schema.py" import json import os from openai import OpenAI client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"]) resp = client.chat.completions.create( model="openai/gpt-5.4-mini", response_format={ "type": "json_schema", "json_schema": { "name": "price", "strict": True, "schema": { "type": "object", "properties": {"price": {"type": "number"}, "currency": {"type": "string"}}, "required": ["price", "currency"], "additionalProperties": False, }, }, }, messages=[{"role": "user", "content": "球拍和球共 1.10 美元,球拍比球贵 1 美元,球多少钱?"}], ) print(json.loads(resp.choices[0].message.content)) ``` 更多写法见[结构化输出](https://ofox.ai/zh/docs/develop/guides/structured-output)。 ## PDF 输入 **Chat Completions** ```python filename="gpt_pdf.py" import base64 import os from openai import OpenAI client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"]) pdf_b64 = base64.b64encode(open("invoice.pdf", "rb").read()).decode() resp = client.chat.completions.create( model="openai/gpt-5.4-mini", messages=[{"role": "user", "content": [ {"type": "file", "file": {"filename": "invoice.pdf", "file_data": f"data:application/pdf;base64,{pdf_b64}"}}, {"type": "text", "text": "这张发票的总金额是多少?"}, ]}], ) print(resp.choices[0].message.content) ``` **Responses** ```python filename="gpt_pdf_responses.py" import base64 import os from openai import OpenAI client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"]) pdf_b64 = base64.b64encode(open("invoice.pdf", "rb").read()).decode() resp = client.responses.create( model="openai/gpt-5.4-mini", input=[{"role": "user", "content": [ {"type": "input_file", "filename": "invoice.pdf", "file_data": f"data:application/pdf;base64,{pdf_b64}"}, {"type": "input_text", "text": "这张发票的总金额是多少?"}, ]}], ) print(resp.output_text) ``` 其他报错见[常见报错](https://ofox.ai/zh/docs/develop/models/gpt#errors)。