Structured output
Ask for JSON, or for JSON that matches a schema you give.
Two modes#
response_format is text (the default), json_object (any valid JSON object), or json_schema (JSON that matches the schema you give). json_schema needs a model with the json_schema capability; on another model it is refused with 400 unsupported_parameter before any upstream call.
curl https://api.panward.ai/v1/chat/completions \ -H "Authorization: Bearer $PANWARD_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "z-ai/glm-5.3", "messages": [{"role": "user", "content": "List the medications in this note as a JSON object."}], "response_format": {"type": "json_object"} }'
Write a schema#
json_schema takes a name (letters, digits, _ and -, up to 64 characters), an optional description, the schema itself, and strict (default false).
The samples read the model id from PANWARD_JSON_MODEL: set it to a chat model with the json_schema capability from the model catalog. A model without it refuses the request with 400 unsupported_parameter before any upstream call, so nothing is billed.
cat > body.json <<EOF { "model": "$PANWARD_JSON_MODEL", "messages": [{"role": "user", "content": "List the medications in this discharge note."}], "response_format": { "type": "json_schema", "json_schema": { "name": "medications", "strict": true, "schema": { "type": "object", "properties": { "medications": { "type": "array", "items": { "type": "object", "properties": {"name": {"type": "string"}, "dose": {"type": "string"}}, "required": ["name", "dose"], "additionalProperties": false } } }, "required": ["medications"], "additionalProperties": false } } } } EOF curl https://api.panward.ai/v1/chat/completions \ -H "Authorization: Bearer $PANWARD_API_KEY" \ -H "Content-Type: application/json" \ --data-binary @body.json
import json, os from openai import OpenAI client = OpenAI( base_url="https://api.panward.ai/v1", api_key=os.environ["PANWARD_API_KEY"], ) reply = client.chat.completions.create( model=os.environ["PANWARD_JSON_MODEL"], messages=[{"role": "user", "content": "List the medications in this discharge note."}], response_format={ "type": "json_schema", "json_schema": { "name": "medications", "strict": True, "schema": { "type": "object", "properties": { "medications": { "type": "array", "items": { "type": "object", "properties": {"name": {"type": "string"}, "dose": {"type": "string"}}, "required": ["name", "dose"], "additionalProperties": False, }, } }, "required": ["medications"], "additionalProperties": False, }, }, }, ) choice = reply.choices[0] if choice.message.refusal: print("refused:", choice.message.refusal) elif choice.finish_reason == "length": print("cut off: the JSON may be incomplete") else: print(json.loads(choice.message.content))
import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://api.panward.ai/v1", apiKey: process.env.PANWARD_API_KEY, }); const reply = await client.chat.completions.create({ model: process.env.PANWARD_JSON_MODEL!, messages: [{ role: "user", content: "List the medications in this discharge note." }], response_format: { type: "json_schema", json_schema: { name: "medications", strict: true, schema: { type: "object", properties: { medications: { type: "array", items: { type: "object", properties: { name: { type: "string" }, dose: { type: "string" } }, required: ["name", "dose"], additionalProperties: false, }, }, }, required: ["medications"], additionalProperties: false, }, }, }, }); const choice = reply.choices[0]; if (choice.message.refusal) console.log("refused:", choice.message.refusal); else if (choice.finish_reason === "length") console.log("cut off: the JSON may be incomplete"); else console.log(JSON.parse(choice.message.content));
Refusals and truncation#
When the model declines, message.refusal holds its reason and there is no JSON to parse. finish_reason: "length" means generation stopped at the token limit, so the JSON may be incomplete: raise max_completion_tokens or ask for less.
With streaming#
A streamed answer is JSON only once it is complete. Concatenate the content deltas and parse after the stream has ended.
With tools#
Use a tool's parameters schema when the model should decide whether to call something, and response_format when every answer must have one shape (Tool calling).
What the receipt covers#
The receipt binds the exact bytes of the request and the response, not the validity of the JSON. Validate the answer against your schema in your application.