نقاط النهاية
المخرجات المنظمة
افرض على النموذج إعادة JSON صالح يطابق مخطط JSON Schema عبر response_format، للاستخراج والتصنيف وأي رد يحلله الكود بثقة.
جرّبه في ساحة التجربةتجعل المخرجات المنظمة النموذج يُعيد JSON يطابق مخطط JSON Schema الذي تقدمه. استخدمها للاستخراج والتصنيف وأي رد يحلله الكود. اضبط response_format في Chat completions (أو text.format في Responses).
معاملات الطلب
| المعامل | النوع | الوصف |
|---|---|---|
response_format.type | string | "json_schema" لمخرجات مقيدة بمخطط. و"json_object" لوضع JSON الحر. |
response_format.json_schema.name | string | اسم للمخطط. |
response_format.json_schema.strict | boolean | القيمة true تفرض المخطط بدقة حيث يدعم المزوّد ذلك. |
response_format.json_schema.schema | object | مخطط JSON Schema. مع strict استخدم additionalProperties: false وأدرج كل خاصية في required. |
{
"model": "openai/gpt-5-mini",
"messages": [{ "role": "user", "content": "Extract: Ada Lovelace, born 1815 in London." }],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": { "type": "string" },
"born": { "type": "integer" },
"city": { "type": "string" }
},
"required": ["name", "born", "city"],
"additionalProperties": false
}
}
}
}
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api14.com/api/v1",
apiKey: process.env.API14_API_KEY,
});
const result = await client.chat.completions.create({
"model": "openai/gpt-5-mini",
"messages": [
{
"role": "user",
"content": "Extract the city and country from: \"I live in Lisbon, Portugal.\""
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "place",
"strict": true,
"schema": {
"type": "object",
"properties": {
"city": {
"type": "string"
},
"country": {
"type": "string"
}
},
"required": [
"city",
"country"
],
"additionalProperties": false
}
}
}
});
console.log(JSON.parse(result.choices[0].message.content));
الاستجابة
يصل JSON على هيئة سلسلة نصية في message.content:
{
"id": "gen-1760000000-json1",
"model": "openai/gpt-5-mini",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": { "role": "assistant", "content": "{\"name\":\"Ada Lovelace\",\"born\":1815,\"city\":\"London\"}" }
}
],
"usage": { "prompt_tokens": 71, "completion_tokens": 18, "total_tokens": 89, "cost": 0.0000538 }
}
تعمل دوال SDK المساعدة كالمعتاد، مثل client.chat.completions.parse(...) مع Zod في Node أو Pydantic في Python.
دعم النماذج
- للعثور على النماذج التي تدعمه، ابحث في
supported_parametersضمن النماذج عنstructured_outputsأوresponse_format. - لضمان ذهاب الطلب إلى المزوّدين الذين يحترمون المخطط فقط، أضف
provider: { "require_parameters": true }. - النماذج التي لا تدعمه أصلًا قد تنتج JSON إذا طلبته في المطالبة، لكن المخرجات غير مضمونة. تحقق منها دائمًا.
- إذا كان
finish_reasonيساوي"length"فـ JSON مقطوع. ارفعmax_tokens.
الأخطاء
المخطط غير الصالح يُعيد عادةً 400 من المزوّد مع رسالته. بقية الأخطاء هي نفسها في Chat completions. راجع الأخطاء.