Yapılandırılmış Çıktılar
Yanıtı JSON olarak isteyin: herhangi bir JSON nesnesi ya da gönderdiğiniz bir şemaya uyan JSON. Yanıtı bir kişi değil, bir program okuyacaksa kullanın.
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"]
}
}
}
}' JSON, yanıtın metnidir ve choices[0].message.content içindedir:
{
"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}
}
} response_format modları
response_format, /v1/chat/completions üzerinde bir alandır. Onsuz yanıt sıradan metindir.
| Mod | Değer | Ne elde edersiniz |
|---|---|---|
| JSON nesnesi | {"type": "json_object"} | Yanıt, belirlenmiş bir biçimi olmayan JSON'dır. Hangi anahtarları istediğinizi istemde belirtin. |
| JSON şeması | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | Yanıt, gönderdiğiniz şemaya uyar: anahtarlarına, türlerine ve zorunlu alanlarına. |
| Strict | "strict": true | json_schema içindeki bir alandır, üçüncü bir mod değildir. false göndermezseniz true olur. |
| Alan | Tür | Varsayılan | Açıklama | Uygulayan |
|---|---|---|---|---|
type | string | json_object veya json_schema. response_format içinde zorunludur. text kabul edilir ve sıradan bir metin yanıtı verir. | Modu kabul eden her model | |
json_schema.schema | object | Yanıtın uyduğu JSON Schema. type değeri json_schema olduğunda her zaman gönderin. | Modu kabul eden her model | |
json_schema.name | string | OpenAI biçimindeki gibi şema için bir etiket. | Host edilen açık ağırlıklı modeller | |
json_schema.strict | boolean | true | Yanıtın şemaya tam olarak uymasının gerekip gerekmediği. Shannon modelleri her şemayı strict olarak uygular. | Host edilen açık ağırlıklı modeller |
JSON nesnesi modu şema gerektirmez. Anahtarları istemde adlandırın:
{
"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"}
} Model başına destek
| Model | json_object | json_schema |
|---|---|---|
Her Shannon modeli (shannon-*) | Evet | Evet |
| Host edilen açık ağırlıklı modeller, aşağıda adı geçenler hariç | Evet | Evet |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Evet | — |
GET /v1/models aynısını kimlik başına bildirir: her modelde capabilities.json_schema ve host edilen açık ağırlıklı modellerde capabilities.response_format. Modeller ve fiyatlandırma
Desteklenen şema anahtar sözcükleri
Bir modelin şemanızdan ne aldığı modele ve uç noktaya bağlıdır:
| Modeller | /v1/chat/completions | /v1/responses |
|---|---|---|
| Shannon modelleri | Aşağıdaki anahtar sözcük alt kümesi, strict olarak uygulanır. | Şema gönderdiğiniz gibi, strict olarak uygulanır. |
| Host edilen açık ağırlıklı modeller | Şema gönderdiğiniz gibi. strict, göndermezseniz true olur. | Şema gönderdiğiniz gibi. strict, göndermezseniz true olur. |
Shannon modellerinin /v1/chat/completions üzerindeki anahtar sözcük alt kümesi:
| Anahtar sözcük | Nasıl okunur |
|---|---|
| Okunan anahtar sözcükler | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | Şemanın en üst düzeyindeki $defs veya definitions içinden çözülür. $ref yanına yazılan description gibi anahtar sözcükler korunur. Eşleşen tanımı olmayan bir başvuru {"type": "object"} olarak okunur. |
allOf | Üyeler tek bir şemada birleştirilir. İki üye aynı anahtar sözcüğü ayarlarsa sonrakisi geçerli olur. |
oneOf | anyOf olarak okunur. |
| Kendine başvuran şemalar | 24 düzeye kadar açılır. Daha derin bir düzey {"type": "object"} olarak okunur. |
| Alt kümenin dışında | Diğer her anahtar sözcük, örneğin additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not ve if / then / else. Bunlar modelin aldığı şemanın parçası değildir. |
/v1/responses üzerinde: text.format
/v1/responses aynı isteği text.format olarak yazar. Şema, bir json_schema anahtarının altında değil, doğrudan format içinde, name ve strict yanında yer alır. JSON nesnesi modu için {"format": {"type": "json_object"}} gönderin.
{
"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"]
}
}
}
} JSON, message öğesinin output_text parçasının text alanıdır:
{
"output": [
{
"id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"annotations": []
}
]
}
]
} Model başına destek ve hatalar /v1/chat/completions üzerindekiyle aynıdır. Responses API
/v1/messages üzerinde: şema olarak bir araç
Messages biçiminin yapılandırılmış çıktı alanı yoktur. Aynı modellerden JSON almak için aynı anahtarla /v1/chat/completions veya /v1/responses çağırın.
/v1/messages üzerinde kalmak için nesneyi bir araç olarak tanımlayın: şemanızı input_schema içine koyun ve aracı tool_choice içinde adlandırın. Bu durumda tool_use bloğunun input alanı, zaten ayrıştırılmış nesnenizdir. Adlandırılmış bir tool_choice, host edilen açık ağırlıklı modeller tarafından uygulanır.
{
"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"}
}
]
} Bloğu okumadan önce stop_reason değerinin tool_use olduğunu denetleyin. Fonksiyon Çağırma
JSON nasıl gelir
- JSON bir dizedir. Yanıtın yanında ayrıştırılmış bir nesne yoktur: JSON ayrıştırıcınızı
message.contentüzerinde çalıştırın. stream: trueile JSON,delta.contentparçaları halinde gelir. Onları birleştirin ve stream bittikten sonra bir kez ayrıştırın; tek başına bir parça geçerli JSON değildir.- Host edilen bir açık ağırlıklı model,
response_formatiçeren bir isteği akıl yürütme metni olmadan yanıtlar:reasoning_contentdeğerinullolur.
Aldığınızı denetleyin
Şema, modelin nasıl yazacağına yol gösterir. API bitmiş yanıtı şemanızla karşılaştırmaz; bu yüzden son denetimi kodunuz yapar:
- Baştaki örneğin yaptığı gibi bir hata yöneticisinin içinde ayrıştırın.
- Ayrıştırılmış nesneyi kendi kodunuzda aynı şemayla doğrulayın, örneğin bir JSON Schema kütüphanesi, Pydantic veya Zod ile.
- Uzun bir yanıt JSON tamamlanmadan bitebilir. Stream edilmeyen bir yanıt
finish_reasondeğerini yalnızcastopveyatool_callsolarak bildirir; bu yüzden bir ayrıştırma hatası işarettir. Yeniden isteyin ya da daha küçük bir nesne isteyin.
Hatalar
| Durum | Tür | Mesaj | Ne zaman |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | Model iki modu da kabul etmiyor. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Model json_object kabul ediyor ve siz json_schema gönderdiniz. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | response_format içinde type yok ya da bir JSON nesnesi değil. |
İki 400 yanıtı, bakiyenizden hiçbir şey düşülmeden önce gönderilir. Hata yönetimi