Skip to main content
Menu
Build / Structured output
Build

Structured output

Ask for JSON, or for JSON that matches a schema you give.

5 minute readUpdated 13 November 2026curl · Python · TypeScript

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.

Any valid JSON object
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

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.

Was this page helpful?Suggest an edit