Outputs Strutturati
Itlob it-tweġiba bħala JSON: kwalunkwe oġġett JSON, jew JSON li jsegwi skema li tibgħat. Uża meta programm, mhux persuna, jaqra t-tweġiba.
POST https://api.shannon-ai.com/v1/chat/completions
import json
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.shannon-ai.com/v1")
response = client.chat.completions.create(
model="Kimi-K3-3BIT-REAP",
messages=[
{"role": "user", "content": "Extract the person: John Doe, 30 years old, engineer."}
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": True,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"occupation": {"type": "string"},
},
"required": ["name", "age", "occupation"],
},
},
},
)
# The JSON arrives as text in message.content: parse it, then check it.
try:
person = json.loads(response.choices[0].message.content)
except json.JSONDecodeError:
person = None # ask again, or report the failure
print(person) import OpenAI from "openai";
const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.shannon-ai.com/v1" });
const response = await client.chat.completions.create({
model: "Kimi-K3-3BIT-REAP",
messages: [
{ role: "user", content: "Extract the person: John Doe, 30 years old, engineer." },
],
response_format: {
type: "json_schema",
json_schema: {
name: "person",
strict: true,
schema: {
type: "object",
properties: {
name: { type: "string" },
age: { type: "integer" },
occupation: { type: "string" },
},
required: ["name", "age", "occupation"],
},
},
},
});
// The JSON arrives as text in message.content: parse it, then check it.
let person = null;
try {
person = JSON.parse(response.choices[0].message.content);
} catch {
// ask again, or report the failure
}
console.log(person); curl https://api.shannon-ai.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "Kimi-K3-3BIT-REAP",
"messages": [
{"role": "user", "content": "Extract the person: John Doe, 30 years old, engineer."}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"occupation": {"type": "string"}
},
"required": ["name", "age", "occupation"]
}
}
}
}' Il-JSON huwa t-test tat-tweġiba, f'choices[0].message.content:
{
"id": "chatcmpl-0c4e7a1b9d2f4e6a8b3c5d7e9f1a2b3c",
"object": "chat.completion",
"created": 1791590400,
"model": "Kimi-K3-3BIT-REAP",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"reasoning_content": null
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 96,
"completion_tokens": 19,
"total_tokens": 115,
"prompt_tokens_details": {"cached_tokens": 0},
"completion_tokens_details": {"reasoning_tokens": 0}
}
} Il-modalitajiet ta' response_format
response_format huwa qasam ta' /v1/chat/completions. Mingħajru t-tweġiba hija test ordinarju.
| Modalità | Valur | X'tikseb |
|---|---|---|
| JSON object | {"type": "json_object"} | It-tweġiba hija JSON mingħajr forma preskritta. Għid fil-prompt liema ċwievet trid. |
| JSON schema | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | It-tweġiba ssegwi l-iskema li tibgħat: iċ-ċwievet, it-tipi u l-kampi meħtieġa tagħha. |
| Strict | "strict": true | Qasam ta' json_schema, mhux it-tielet modalità. Huwa true sakemm ma tibgħatx false. |
| Qasam | Tip | Default | Deskrizzjoni | Applikat minn |
|---|---|---|---|---|
type | string | json_object jew json_schema. Meħtieġ ġewwa response_format. text jiġi aċċettat u jagħti tweġiba ordinarja bit-test. | Kull mudell li jaċċetta l-modalità | |
json_schema.schema | object | Il-JSON Schema li ssegwi t-tweġiba. Ibgħatha kull meta type ikun json_schema. | Kull mudell li jaċċetta l-modalità | |
json_schema.name | string | Tikketta għall-iskema, bħal fil-format OpenAI. | Mudelli open-weight ospitati | |
json_schema.strict | boolean | true | Jekk it-tweġiba trid issegwi l-iskema eżattament. Il-mudelli Shannon japplikaw kull skema bħala strict. | Mudelli open-weight ospitati |
Il-modalità JSON object ma teħtieġ l-ebda skema. Semmi ċ-ċwievet fil-prompt:
{
"model": "shannon-3",
"messages": [
{
"role": "user",
"content": "Name the capital of France. Answer as JSON with the keys city and country."
}
],
"response_format": {"type": "json_object"}
} Appoġġ għal kull mudell
| Mudell | json_object | json_schema |
|---|---|---|
Kull mudell Shannon (shannon-*) | Iva | Iva |
| Mudelli open-weight ospitati, ħlief dawk imsemmija hawn taħt | Iva | Iva |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Iva | — |
GET /v1/models jirrapporta l-istess għal kull id: capabilities.json_schema fuq kull mudell, u capabilities.response_format fuq il-mudelli open-weight ospitati. Mudelli u prezzijiet
Keywords tal-iskema appoġġjati
Dak li jirċievi mudell mill-iskema tiegħek jiddependi mill-mudell u mill-endpoint:
| Mudelli | /v1/chat/completions | /v1/responses |
|---|---|---|
| Mudelli Shannon | Is-sottoinsieme ta' keywords hawn taħt, applikat bħala strict. | L-iskema kif tibgħatha, applikata bħala strict. |
| Mudelli open-weight ospitati | L-iskema kif tibgħatha. strict huwa true sakemm ma tibgħatux. | L-iskema kif tibgħatha. strict huwa true sakemm ma tibgħatux. |
Is-sottoinsieme ta' keywords tal-mudelli Shannon fuq /v1/chat/completions:
| Keyword | Kif jinqara |
|---|---|
| Keywords moqrija | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | Jiġi riżolt minn $defs jew definitions fil-livell ta' fuq tal-iskema. Keywords miktuba maġenb $ref, bħal description, jinżammu. Referenza mingħajr definizzjoni li taqbel tinqara bħala {"type": "object"}. |
allOf | Il-membri jinsiġu f'skema waħda. Fejn żewġ membri jissettjaw l-istess keyword, tgħodd l-aħħar waħda. |
oneOf | Jinqara bħala anyOf. |
| Skemi li jirreferu għal rashom | Jitkabbru sa 24 livell. Livell iktar fond jinqara bħala {"type": "object"}. |
| Barra s-sottoinsieme | Kull keyword oħra, pereżempju additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not u if / then / else. Dawn mhumiex parti mill-iskema li jirċievi l-mudell. |
Fuq /v1/responses: text.format
/v1/responses jikteb l-istess talba bħala text.format. L-iskema tinsab direttament f'format, maġenb name u strict, mhux taħt ċavetta json_schema. Għall-modalità JSON object ibgħat {"format": {"type": "json_object"}}.
{
"model": "Kimi-K3-3BIT-REAP",
"input": "Extract the person: John Doe, 30 years old, engineer.",
"text": {
"format": {
"type": "json_schema",
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"occupation": {"type": "string"}
},
"required": ["name", "age", "occupation"]
}
}
}
} Il-JSON huwa t-text tal-parti output_text tal-element message:
{
"output": [
{
"id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"annotations": []
}
]
}
]
} L-appoġġ għal kull mudell u l-iżbalji huma l-istess bħal fuq /v1/chat/completions. Responses API
Fuq /v1/messages: tool bħala skema
Il-format Messages m'għandu l-ebda qasam ta' output strutturat. Għal JSON mill-istess mudelli, sejjaħ /v1/chat/completions jew /v1/responses bl-istess ċavetta.
Biex tibqa' fuq /v1/messages, iddeskrivi l-oġġett bħala tool: poġġi l-iskema tiegħek f'input_schema u semmi t-tool f'tool_choice. L-input tal-blokk tool_use huwa mbagħad l-oġġett tiegħek, diġà parsjat. tool_choice imsemmi jiġi applikat mill-mudelli open-weight ospitati.
{
"model": "Kimi-K3-3BIT-REAP",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Extract the person: John Doe, 30 years old, engineer."
}
],
"tools": [
{
"name": "record_person",
"description": "Record the extracted person.",
"input_schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"occupation": {"type": "string"}
},
"required": ["name", "age", "occupation"]
}
}
],
"tool_choice": {"type": "tool", "name": "record_person"}
} {
"stop_reason": "tool_use",
"content": [
{
"type": "tool_use",
"id": "toolu_4b8d2f6a1c3e4a5b9d7f0e2c4a6b8d1f",
"name": "record_person",
"input": {"name": "John Doe", "age": 30, "occupation": "engineer"}
}
]
} Ara li stop_reason huwa tool_use qabel taqra l-blokk. Sejħiet ta' Funzjonijiet
Kif jasal il-JSON
- Il-JSON huwa string. It-tweġiba m'għandha l-ebda oġġett parsjat maġenbha: ħaddem il-JSON parser tiegħek fuq
message.content. - B'
stream: trueil-JSON jasal f'biċċietdelta.content. Għaqqadhom u parsja darba biss meta jintemm l-istream; biċċa waħedha mhix JSON validu. - Mudell open-weight ospitat iwieġeb talba
response_formatmingħajr test tar-raġunament:reasoning_contenthuwanull.
Iċċekkja dak li tirċievi
L-iskema tiggwida kif jikteb il-mudell. L-API ma tqabbilx it-tweġiba lesta mal-iskema tiegħek, għalhekk il-kodiċi tiegħek jagħmel il-kontroll finali:
- Parsja ġewwa error handler, kif jagħmel l-eżempju fil-bidu.
- Ivvalida l-oġġett parsjat bl-istess skema fil-kodiċi tiegħek, pereżempju b'librerija ta' JSON Schema, Pydantic jew Zod.
- Tweġiba twila tista' tintemm qabel ma l-JSON ikun komplut. Tweġiba li ma tiġix fi stream tirrapporta
finish_reasonbħalastopjewtool_callsbiss, għalhekk żball tal-parsing huwa s-sinjal. Itlob mill-ġdid, jew itlob oġġett iżgħar.
Żbalji
| Status | Tip | Messaġġ | Meta |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | Il-mudell ma jaċċetta l-ebda waħda miż-żewġ modalitajiet. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Il-mudell jaċċetta json_object u inti bgħatt json_schema. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | response_format m'għandux type, jew mhuwiex oġġett JSON. |
Iż-żewġ tweġibiet 400 jintbagħtu qabel ma jittieħed xejn mill-bilanċ tiegħek. Ġestjoni tal-iżbalji