Endpoints
स्ट्रक्चर्ड आउटपुट
response_format से मॉडल को आपके दिए JSON Schema से मेल खाता वैध JSON लौटाने के लिए बाध्य करें। पैरामीटर, उदाहरण और मॉडल समर्थन यहाँ हैं।
playground में आज़माएंस्ट्रक्चर्ड आउटपुट मॉडल से ऐसा JSON लौटवाते हैं जो आपके दिए JSON Schema से मेल खाता हो। इनका उपयोग डेटा निकासी, वर्गीकरण और ऐसे हर उत्तर के लिए करें जिसे आपका कोड पार्स करता है। चैट कंप्लीशन्स पर response_format सेट करें (या रिस्पॉन्स पर text.format)।
अनुरोध पैरामीटर
| पैरामीटर | प्रकार | विवरण |
|---|---|---|
response_format.type | string | schema-बाध्य आउटपुट के लिए "json_schema"। फ़्री-फ़ॉर्म JSON मोड के लिए "json_object"। |
response_format.json_schema.name | string | schema का नाम। |
response_format.json_schema.strict | boolean | true होने पर, जहाँ प्रोवाइडर सपोर्ट करता है, schema का ठीक पालन कराता है। |
response_format.json_schema.schema | object | एक JSON Schema। strict के साथ additionalProperties: false इस्तेमाल करें और हर property को 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 हेल्पर सामान्य रूप से काम करते हैं, उदाहरण के लिए Node में Zod या Python में Pydantic के साथ client.chat.completions.parse(...)।
मॉडल समर्थन
- इसे सपोर्ट करने वाले मॉडल खोजने के लिए मॉडल में
supported_parametersमेंstructured_outputsयाresponse_formatदेखें। - यह सुनिश्चित करने के लिए कि अनुरोध केवल उन प्रोवाइडर तक जाए जो schema का पालन करते हैं,
provider: { "require_parameters": true }जोड़ें। - जिन मॉडल में मूल समर्थन नहीं है वे प्रॉम्प्ट में माँगने पर भी JSON दे सकते हैं, लेकिन आउटपुट की गारंटी नहीं है। उसे हमेशा validate करें।
- यदि
finish_reason"length"है, तो JSON कट गया है।max_tokensबढ़ाएँ।
त्रुटियाँ
अमान्य schema आमतौर पर प्रोवाइडर के संदेश के साथ अपस्ट्रीम 400 लौटाता है। अन्य त्रुटियाँ चैट कंप्लीशन्स जैसी ही हैं। देखें त्रुटियाँ।