Simira kuzvirimo
Zvinobuda zvakarongeka

Zvinobuda zvakarongeka

Kumbira mhinduro se-JSON: JSON object ipi neipi, kana JSON inotevera schema yaunotumira. Shandisa kana program, kwete munhu, ichiverenga mhinduro.

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)

JSON i-text yemhinduro, mu-choices[0].message.content:

200 JSON
{
  "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}
  }
}

Ma-mode e-response_format

response_format i-field ye-/v1/chat/completions. Pasina iyo mhinduro i-text yakajairika.

Mode Value Zvaunowana
JSON object {"type": "json_object"} Mhinduro i-JSON isina chimiro chakatarwa. Taura mu-prompt kuti ndeapi ma-key aunoda.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Mhinduro inotevera schema yaunotumira: ma-key ayo, types ne-fields dzinodiwa.
Strict "strict": true Field ye-json_schema, kwete mode yechitatu. I-true kunze kwekunge utumira false.
Field Mhando Default Tsananguro Inoshandiswa ne
type string json_object kana json_schema. Inodiwa mukati me-response_format. text inogamuchirwa uye inopa mhinduro ye-text yakajairika. Model imwe neimwe inogamuchira mode
json_schema.schema object JSON Schema inotevedzwa nemhinduro. Itumire pese apo type i-json_schema. Model imwe neimwe inogamuchira mode
json_schema.name string Zita re-schema, sepa-OpenAI format. Hosted open-weight models
json_schema.strict boolean true Kuti mhinduro inofanira kutevedza schema chaizvo here. Shannon models dzinoshandisa schema imwe neimwe se-strict. Hosted open-weight models

JSON object mode haidi schema. Taura ma-key mu-prompt:

Request body
{
  "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"}
}

Tsigiro pa-model

Model json_object json_schema
Shannon model imwe neimwe (shannon-*) Hongu Hongu
Hosted open-weight models, kunze kwedzakatumidzwa pazasi Hongu Hongu
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Hongu —

GET /v1/models inoratidza zvakafanana pa-id: capabilities.json_schema pa-model imwe neimwe, ne-capabilities.response_format pa-hosted open-weight models. Ma-model nemitengo

Schema keywords dzinotsigirwa

Zvinogamuchirwa ne-model ze-schema yako zvinoenderana ne-model ne-endpoint:

Ma-model /v1/chat/completions /v1/responses
Shannon models Keyword subset iri pazasi, inoshandiswa se-strict. Schema sezvaunoitumira, inoshandiswa se-strict.
Hosted open-weight models Schema sezvaunoitumira. strict i-true kunze kwekunge wainotumira. Schema sezvaunoitumira. strict i-true kunze kwekunge wainotumira.

Keyword subset ye-Shannon models pa-/v1/chat/completions:

Keyword Kuti inoverengwa sei
Keywords dzinoverengwa type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref Inogadziriswa kubva ku-$defs kana definitions pa-top level ye-schema. Keywords dzakanyorwa padivi pe-$ref, sa-description, dzinochengetwa. Reference isina definition inoenderana inoverengwa se-{"type": "object"}.
allOf Members dzinosanganiswa kuita schema imwe. Pane members mbiri dzinoisa keyword imwechete, yekupedzisira ndiyo inoverengwa.
oneOf Inoverengwa se-anyOf.
Schemas dzinozvitaura dzoga Dzinowedzerwa kusvika ku-24 levels. Level yakadzika inoverengwa se-{"type": "object"}.
Kunze kwe-subset Keyword imwe neimwe yakasara, semuenzaniso additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not ne-if / then / else. Izvi hazvisi chikamu che-schema inogamuchirwa ne-model.

Pa-/v1/responses: text.format

/v1/responses inonyora request imwechete se-text.format. Schema iri zvakananga mu-format, padivi pe-name ne-strict, kwete pasi pe-json_schema key. Pa-JSON object mode tumira {"format": {"type": "json_object"}}.

Request body
{
  "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 i-text ye-output_text part ye-message item:

200 JSON
{
  "output": [
    {
      "id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
      "type": "message",
      "role": "assistant",
      "status": "completed",
      "content": [
        {
          "type": "output_text",
          "text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
          "annotations": []
        }
      ]
    }
  ]
}

Tsigiro pa-model ne-errors zvakafanana nepa-/v1/chat/completions. Responses API

Pa-/v1/messages: tool se-schema

Messages format haina structured-output field. Kuti uwane JSON kubva kuma-model akafanana, danidzira /v1/chat/completions kana /v1/responses ne-key imwechete.

Kuti ugare pa-/v1/messages, tsanangura object se-tool: isa schema yako mu-input_schema uye utumidze tool mu-tool_choice. input ye-tool_use block inobva yava object yako, yatoparswa (parsed). tool_choice yakatumidzwa inoshandiswa ne-hosted open-weight models.

Request body
{
  "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"}
}
200 JSON
{
  "stop_reason": "tool_use",
  "content": [
    {
      "type": "tool_use",
      "id": "toolu_4b8d2f6a1c3e4a5b9d7f0e2c4a6b8d1f",
      "name": "record_person",
      "input": {"name": "John Doe", "age": 30, "occupation": "engineer"}
    }
  ]
}

Tarisa kuti stop_reason i-tool_use usati waverenga block. Kudanwa kwemabasa

Kuti JSON inosvika sei

  • JSON i-string. Mhinduro haina parsed object padivi payo: mhanyisa JSON parser yako pa-message.content.
  • Ne-stream: true JSON inouya mu-delta.content pieces. Dzibatanidze uye i-parse stream yapera; chimedu chimwe chega hachisi JSON chaiyo.
  • Hosted open-weight model inopindura response_format request pasina reasoning text: reasoning_content i-null.

Tarisa zvaunogamuchira

Schema inotungamira kuti model inonyora sei. API haienzanise mhinduro yapera ne-schema yako, saka code yako ndiyo inoita check yekupedzisira:

  • I-parse mukati me-error handler, sezvinoitwa ne-sample iri kumusoro.
  • Tarisa parsed object ne-schema imwechete mu-code yako pachako, semuenzaniso ne-JSON Schema library, Pydantic kana Zod.
  • Mhinduro refu inogona kupera JSON isati yapera. Mhinduro isiri streamed inoratidza finish_reason se-stop kana tool_calls chete, saka parse error ndiyo chiratidzo. Kumbira zvakare, kana ukumbire object diki.

Errors

Status Mhando Message Rinhi
400 invalid_request_error <id> does not accept response_format Model haigamuchiri mode ipi neipi.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object Model inogamuchira json_object uye iwe wakatumira json_schema.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … response_format haina type, kana haisi JSON object.

Mhinduro mbiri dze-400 dzinotumirwa pasati pabviswa chinhu mu-balance yako. Zvikanganiso