نستخدم ملفات تعريف الارتباط. تُبقيك ملفات تعريف الارتباط وتخزين المتصفح مسجّلًا للدخول وتتذكر تفضيلاتك. وبإذنك نقيس أيضًا كيفية استخدام الموقع. يمكنك في أي وقت. راجع سياسة ملفات تعريف الارتباط.

المخرجات المنظمة

نقاط النهاية

المخرجات المنظمة

افرض على النموذج إعادة JSON صالح يطابق مخطط JSON Schema عبر response_format، للاستخراج والتصنيف وأي رد يحلله الكود بثقة.

جرّبه في ساحة التجربة

تجعل المخرجات المنظمة النموذج يُعيد JSON يطابق مخطط JSON Schema الذي تقدمه. استخدمها للاستخراج والتصنيف وأي رد يحلله الكود. اضبط response_format في Chat completions (أو text.format في Responses).

معاملات الطلب

المعاملالنوعالوصف
response_format.typestring"json_schema" لمخرجات مقيدة بمخطط. و"json_object" لوضع JSON الحر.
response_format.json_schema.namestringاسم للمخطط.
response_format.json_schema.strictbooleanالقيمة true تفرض المخطط بدقة حيث يدعم المزوّد ذلك.
response_format.json_schema.schemaobjectمخطط 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. راجع الأخطاء.