Vokatra voarafitra
Angataho ho JSON ny valiny: object JSON na inona na inona, na JSON manaraka schema alefanao. Ampiasao rehefa programa, fa tsy olona, no mamaky ny valiny.
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"]
}
}
}
}' Ny JSON dia ny soratry ny valiny, ao amin'ny 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}
}
} Ny mode an'ny response_format
Ny response_format dia saha an'ny /v1/chat/completions. Raha tsy misy dia soratra mahazatra ny valiny.
| Mode | Sanda | Izay azonao |
|---|---|---|
| JSON object | {"type": "json_object"} | Ny valiny dia JSON tsy misy endrika voafaritra. Lazao ao amin'ny prompt izay key tianao. |
| JSON schema | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | Ny valiny dia manaraka ny schema alefanao: ny key, karazana ary saha ilaina. |
| Strict | "strict": true | Saha ao amin'ny json_schema, fa tsy mode fahatelo. true izy raha tsy alefanao ny false. |
| Saha | Karazana | Default | Famaritana | Ampiharin'ny |
|---|---|---|---|---|
type | string | json_object na json_schema. Ilaina ao anatin'ny response_format. Ekena ny text ary manome valiny soratra mahazatra. | Model rehetra mandray ny mode | |
json_schema.schema | object | Ny JSON Schema arahin'ny valiny. Alefaso isaky ny json_schema ny type. | Model rehetra mandray ny mode | |
json_schema.name | string | Label ho an'ny schema, toy ny amin'ny endrika OpenAI. | Model open-weight hosted | |
json_schema.strict | boolean | true | Raha tsy maintsy manaraka tsara ny schema ny valiny. Ny model Shannon dia mampihatra schema rehetra ho strict. | Model open-weight hosted |
Ny mode JSON object dia tsy mila schema. Tononinao ao amin'ny prompt ny key:
{
"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"}
} Fanohanana isaky ny model
| Model | json_object | json_schema |
|---|---|---|
Model Shannon rehetra (shannon-*) | Eny | Eny |
| Model open-weight hosted, afa-tsy ireo voatonona etsy ambany | Eny | Eny |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Eny | — |
Ny GET /v1/models dia milaza izany isaky ny id: capabilities.json_schema amin'ny model rehetra, ary capabilities.response_format amin'ny model open-weight hosted. Model & vidiny
Keyword schema tohanana
Izay raisin'ny model amin'ny schema-nao dia miankina amin'ny model sy ny endpoint:
| Model | /v1/chat/completions | /v1/responses |
|---|---|---|
| Model Shannon | Ny ampahany keyword etsy ambany, ampiharina ho strict. | Ny schema araka ny alefanao, ampiharina ho strict. |
| Model open-weight hosted | Ny schema araka ny alefanao. true ny strict raha tsy alefanao. | Ny schema araka ny alefanao. true ny strict raha tsy alefanao. |
Ny ampahany keyword an'ny model Shannon amin'ny /v1/chat/completions:
| Keyword | Ny fomba famakiana |
|---|---|
| Keyword vakiana | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | Voavahana avy amin'ny $defs na definitions eo ambonin'ny schema. Ny keyword sorana eo anilan'ny $ref, toy ny description, dia tazonina. Ny reference tsy misy famaritana mifanaraka dia vakiana ho {"type": "object"}. |
allOf | Ny mpikambana dia atambatra ho schema iray. Rehefa mametraka keyword mitovy ny mpikambana roa, ny aorian'izay no raisina. |
oneOf | Vakiana ho anyOf. |
| Schema mitondra ny tenany | Voalaza hatramin'ny ambaratonga 24. Ny ambaratonga lalina kokoa dia vakiana ho {"type": "object"}. |
| Ivelan'ny ampahany | Ny keyword hafa rehetra, ohatra additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not ary if / then / else. Ireo dia tsy anisan'ny schema raisin'ny model. |
Amin'ny /v1/responses: text.format
Ny /v1/responses dia manoratra ny fangatahana mitovy ho text.format. Ny schema dia mipetraka mivantana ao amin'ny format, eo anilan'ny name sy strict, fa tsy ao ambanin'ny key json_schema. Ho an'ny mode JSON object alefaso ny {"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"]
}
}
}
} Ny JSON dia ny text an'ny ampahany output_text ao amin'ny item message:
{
"output": [
{
"id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"annotations": []
}
]
}
]
} Ny fanohanana isaky ny model sy ny hadisoana dia mitovy amin'ny an'ny /v1/chat/completions. Responses API
Amin'ny /v1/messages: tool ho schema
Ny endrika Messages dia tsy manana saha structured-output. Ho an'ny JSON avy amin'ny model mitovy, antsoy ny /v1/chat/completions na /v1/responses miaraka amin'ny key mitovy.
Raha te-hijanona amin'ny /v1/messages, faritano ho tool ny object: apetraho ao amin'ny input_schema ny schema-nao ary tononinao ao amin'ny tool_choice ny tool. Ny input an'ny block tool_use dia ny object-nao, efa voaparse. Ny tool_choice voatondro dia ampiharin'ny model open-weight hosted.
{
"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"}
}
]
} Hamarino fa tool_use ny stop_reason alohan'ny hamakianao ny block. Antso fiasa
Ny fomba fiavian'ny JSON
- Ny JSON dia string. Ny valiny dia tsy manana object voaparse eo anilany: ampandehano ny JSON parser-nao amin'ny
message.content. - Miaraka amin'ny
stream: trueny JSON dia tonga ho sombin'nydelta.content. Ampiarahy ary parse-o rehefa vita ny stream; ny sombiny tokana dia tsy JSON mety. - Ny model open-weight hosted dia mamaly ny fangatahana
response_formattsy misy soratra reasoning:nullnyreasoning_content.
Hamarino izay raisinao
Ny schema dia mitarika ny fomba fanoratan'ny model. Ny API dia tsy mampitaha ny valiny vita amin'ny schema-nao, ka ny kaody-nao no manao ny fanamarinana farany:
- Parse-o ao anaty error handler, araka ny ohatra eo ambony.
- Hamarino ny object voaparse miaraka amin'ny schema mitovy ao amin'ny kaody-nao, ohatra amin'ny library JSON Schema, Pydantic na Zod.
- Ny valiny lava dia mety mifarana alohan'ny hahavitan'ny JSON. Ny valiny tsy streamed dia milaza ny
finish_reasonhostopnatool_callsihany, ka ny hadisoana parse no famantarana. Angataho indray, na angataho object kely kokoa.
Hadisoana
| Status | Karazana | Hafatra | Rahoviana |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | Ny model dia tsy mandray mode roa. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Ny model dia mandray json_object ary nandefa json_schema ianao. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Tsy manana type ny response_format, na tsy object JSON izy. |
Ny valiny 400 roa dia alefa alohan'ny hanalana na inona na inona amin'ny balance-nao. Fitantanana hadisoana