Strukturéiert Output
Freet d'Äntwert als JSON un: iergend e JSON-Objet, oder JSON no engem Schema, dat Dir schéckt. Benotzt et, wann e Programm, kee Mënsch, d'Äntwert liest.
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"]
}
}
}
}' D'JSON ass den Text vun der Äntwert, an 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}
}
} D'Moduse vun response_format
response_format ass e Feld vun /v1/chat/completions. Ouni et ass d'Äntwert normalen Text.
| Modus | Wäert | Wat Dir kritt |
|---|---|---|
| JSON-Objet | {"type": "json_object"} | D'Äntwert ass JSON ouni virgeschriwwe Form. Sot am Prompt, wéi eng Schlësselen Dir wëllt. |
| JSON Schema | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | D'Äntwert follegt dem Schema, dat Dir schéckt: sengen Schlësselen, Typen an erfuerderleche Felder. |
| Strict | "strict": true | E Feld vun json_schema, kee drëtte Modus. Et ass true, ausser Dir schéckt false. |
| Feld | Typ | Standard | Beschreiwung | Ugewannt vun |
|---|---|---|---|---|
type | string | json_object oder json_schema. Erfuerderlech an response_format. text gëtt akzeptéiert a gëtt eng normal Textäntwert. | All Modell, dee de Modus hëlt | |
json_schema.schema | object | D'JSON Schema, dem d'Äntwert follegt. Schéckt et, ëmmer wann type json_schema ass. | All Modell, dee de Modus hëlt | |
json_schema.name | string | E Label fir d'Schema, wéi am OpenAI-Format. | Hosted Open-Weight-Modeller | |
json_schema.strict | boolean | true | Ob d'Äntwert dem Schema genau muss follegen. D'Shannon-Modeller wenden all Schema als strict un. | Hosted Open-Weight-Modeller |
De JSON-Objet-Modus brauch kee Schema. Nennt d'Schlësselen am 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"}
} Ënnerstëtzung pro Modell
| Modell | json_object | json_schema |
|---|---|---|
All Shannon-Modell (shannon-*) | Jo | Jo |
| Hosted Open-Weight-Modeller, ausser deenen, déi ënnendrënner genannt sinn | Jo | Jo |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Jo | — |
GET /v1/models mellt datselwecht pro ID: capabilities.json_schema bei all Modell, an capabilities.response_format bei den Hosted Open-Weight-Modeller. Modeller a Präisser
Ënnerstëtzt Schema-Schlësselwierder
Wat e Modell vun Ärem Schema kritt, hänkt vum Modell an vum Endpoint of:
| Modeller | /v1/chat/completions | /v1/responses |
|---|---|---|
| Shannon-Modeller | D'Ënnermengt vu Schlësselwierder ënnendrënner, als strict ugewannt. | D'Schema esou, wéi Dir et schéckt, als strict ugewannt. |
| Hosted Open-Weight-Modeller | D'Schema esou, wéi Dir et schéckt. strict ass true, ausser Dir schéckt et. | D'Schema esou, wéi Dir et schéckt. strict ass true, ausser Dir schéckt et. |
D'Ënnermengt u Schlësselwierder vun de Shannon-Modeller op /v1/chat/completions:
| Schlësselwuert | Wéi et gelies gëtt |
|---|---|
| Gelies Schlësselwierder | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | Aus $defs oder definitions um héchsten Niveau vum Schema opgeléist. Schlësselwierder, déi niewent dem $ref stinn, wéi description, bleiwen erhalen. Eng Referenz ouni passend Definitioun gëtt als {"type": "object"} gelies. |
allOf | D'Memberen ginn an ee Schema zesummegefouert. Wou zwee Memberen datselwecht Schlësselwuert setzen, gëllt dat lescht. |
oneOf | Gëtt als anyOf gelies. |
| Schemaen, déi sech op sech selwer bezéien | Op 24 Niveauen opgeklappt. Eng méi déif Niveau gëtt als {"type": "object"} gelies. |
| Baussent der Ënnermengt | All aner Schlësselwuert, zum Beispill additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not an if / then / else. Dës sinn net Deel vum Schema, dat de Modell kritt. |
Op /v1/responses: text.format
/v1/responses schreift déiselwecht Ufro als text.format. D'Schema steet direkt an format, niewent name an strict, net ënner engem json_schema-Schlëssel. Fir de JSON-Objet-Modus schéckt {"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"]
}
}
}
} D'JSON ass den text vum output_text-Deel vum message-Item:
{
"output": [
{
"id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"annotations": []
}
]
}
]
} D'Ënnerstëtzung pro Modell an d'Feeler sinn déiselwecht wéi op /v1/chat/completions. Responses-API
Op /v1/messages: e Tool als Schema
D'Messages-Format huet kee Feld fir strukturéierten Output. Fir JSON vun deene selwechte Modeller rufft /v1/chat/completions oder /v1/responses mam selwechte Schlëssel.
Fir op /v1/messages ze bleiwen, beschreift d'Objet als Tool: setzt Äert Schema an input_schema an nennt d'Tool an tool_choice. Den input vum tool_use-Block ass dann Äert Objet, schonn geparst. E genannten tool_choice gëtt vun den Hosted Open-Weight-Modeller ugewannt.
{
"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"}
}
]
} Kontrolléiert, datt stop_reason tool_use ass, ier Dir de Block liest. Funktiounsofruff
Wéi d'JSON ukënnt
- D'JSON ass e String. D'Äntwert huet kee geparst Objet derniewent: loosst Äre JSON-Parser op
message.contentlafen. - Mat
stream: truekënnt d'JSON a Stécker vundelta.content. Setzt se zesummen a parst eemol, wann de Stream eriwwer ass; e Stéck eleng ass kee gültegt JSON. - En Hosted Open-Weight-Modell beäntwert eng
response_format-Ufro ouni Reasoning-Text:reasoning_contentassnull.
Kontrolléiert, wat Dir kritt
D'Schema leet fest, wéi de Modell schreift. D'API vergläicht déi fäerdeg Äntwert net mat Ärem Schema, Äre Code mécht dofir déi lescht Kontroll:
- Parst an engem Error-Handler, wéi d'Beispill uewen.
- Validéiert dat geparst Objet mam selwechte Schema an Ärem eegene Code, zum Beispill mat enger JSON-Schema-Bibliothéik, Pydantic oder Zod.
- Eng laang Äntwert ka ophalen, ier d'JSON komplett ass. Eng Äntwert, déi net gestreamt gëtt, mellt
finish_reasonnëmmen alsstopodertool_calls, dofir ass e Parse-Feeler d'Signal. Freet nach eng Kéier, oder freet e méi klengt Objet.
Feeler
| Status | Typ | Message | Wéini |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | De Modell hëlt kee vun den zwee Moduse. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | De Modell hëlt json_object an Dir hutt json_schema geschéckt. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | response_format huet kee type, oder ass kee JSON-Objet. |
Déi zwou 400-Äntwerte ginn geschéckt, ier eppes aus Ärem Solde ofgezunn gëtt. Feelerbehandlung