Đầu ra có cấu trúc
Đầu ra có cấu trúc
response_format buộc mô hình trả lời bằng JSON thay vì văn bản thông thường. Sử dụng json_schemakhi bạn có một schema cần ép buộc, và json_object khi bạn chỉ cần JSON hợp lệ.
| Tham số | Loại | Bắt buộc | Mô tả |
|---|---|---|---|
| response_format | object | Không bắt buộc | Hợp đồng đầu ra. Biểu mẫu:{type:"text"}{type:"json_object"}{type:"json_schema", json_schema:{name, schema, strict}} |
import json, os from openai import OpenAI client = OpenAI( api_key=os.environ["OXYY_API_KEY"], base_url="https://api.oxyy.ai/v1" ) response = client.chat.completions.create( model="gpt-5.5", messages=[{"role": "user", "content": "Extract the invoice fields."}], response_format={ "type": "json_schema", "json_schema": { "name": "invoice", "strict": True, "schema": { "type": "object", "properties": { "total": {"type": "number"}, "currency": {"type": "string"}, }, "required": ["total", "currency"], "additionalProperties": False, }, }, }, ) data = json.loads(response.choices[0].message.content)
# json_object is the looser mode: valid JSON, no schema enforced. curl https://api.oxyy.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OXYY_API_KEY" \ -d '{ "model": "gpt-5.5", "messages": [{"role": "user", "content": "List 3 colours as JSON."}], "response_format": {"type": "json_object"} }'
Tính di động theo thiết kế. Các nhà cung cấp không có chế độ schema gốc sẽ được chuyển sang dùng tool output bắt buộc, và câu trả lời sẽ được trích xuất sẵn cho bạn — bao gồm cả việc lược bỏ
```json hàng rào mà một số mô hình thêm vào. Dù sao đi nữa choices[0].message.content là một tài liệu JSON mà bạn có thể phân tích cú pháp. strict: true cần một schema khép kín hoàn toàn (additionalProperties: false và mọi thuộc tính trong required); để trống nếu bạn không có.