Skip to navigation
DocsStructured output

Build

Structured output

In short: Structured output means the model replies with JSON in a shape you define, via response_format JSON Schema, or a forced tool or output_config for Claude.

Updated View .md
On this page

Structured output means the model answers with JSON in a shape you define, so your code can parse it without guessing. There are three ways to ask for it; which one works depends on the model.

Which method works where

Model idjson_schemajson_objectForced tool
stepaudio-3-chat-preview:freeNoNoNo
step-5:freeNoNoNo
gpt-6-luna:freeNoNoNo
nvidia-deepseek-v4-1-flash:freeNoNoNo
minimaxai-minimax-m3-1-flash:freeNoNoNo
deepseek-v4-flash-0731-thinking:freeNoNoNo
deepseek-v4-flash-0731:freeNoNoNo
deepseek-v4-flash-0731-1:freeNoNoNo
beam-1-0:freeNoNoNo
qwen-qwen3.8-27b:freeNoNoNo
qwen3.8-27b-uncensored:freeNoNoNo
dealignai-glm-5.3-flash-uncensored-fp8:freeNoNoNo
nvidia-nemotron-3-ultra-550b-a55b:freeNoNoNo
z-ai-glm-5.3:freeNoNoNo
deepseek-deepseek-v4.1-flash:freeNoNoNo
z-ai-glm-5.3-flash:freeNoNoNo
ernie-5.5-flash-preview:freeNoNoNo
glm-4.6v-flash:freeNoNoNo
glm-4.7-flash:freeNoNoNo
hy4-flash:freeNoNoNo
dealignai-deepseek-v4.1-flash-uncensored-fp8:freeNoNoNo
dealignai-glm-5.3-uncensored-fp8:freeNoNoNo
moonshotai-kimi-k3.1-pro:freeNoNoNo
deepseek-v4-flash:freeNoNoNo
ernie-5.5-flash-preview-1:freeNoNoNo
ernie-5.5-flash-preview-2:freeNoNoNo
gemma-4-31b-it:freeNoNoNo
gemma4-31b:freeNoNoNo
laguna-xs-2.1:freeNoNoNo
lfm-3-1-7b:freeNoNoNo
longcat-2.5:freeNoNoNo
laguna-xs-2.1-1:freeNoNoNo
laguna-xs-2.1-uncensored:freeNoNoNo
llama-3.3-70b-instruct:freeNoNoNo
nemotron-3-nano-omni-30b-a3b-reasoning:freeNoNoNo
nemotron-3.5-super-vl:freeNoNoNo
nemotron-3-5-lightning:freeNoNoNo
nvidia-nemotron-3-ultra-550b-a55b-1:freeNoNoNo
solar-mini4:freeNoNoNo
nemotron-3-ultra-550b-a55b-uncensored:freeNoNoNo
jais-3-mova-36b-a4b:freeNoNoNo
nvidia-mistral-large-4-1t-a52b:freeNoNoNo
k2-horizon-375b-a23b-uncensored:freeNoNoNo
k2-horizon-375b-a23b:freeNoNoNo
glm-4.6v-flash-1:freeNoNoNo
glm-4.7-flash-1:freeNoNoNo
grok-4.7-fast:freeNoNoNo
roleplay-chat:freeNoNoNo

json_schema and json_object are values of response_format on chat completions. “Yes” means the provider documents support for it; “No (dropped)” is explained below.

How do I get JSON that matches my schema?

Set response_format: {"type": "json_schema", ...} with strict: true, the recommended method, which makes supporting models follow the schema exactly. With strict mode, list every property in required (use a null type for optional values) and set additionalProperties: false.

import json, os
from openai import OpenAI

client = OpenAI(base_url="https://api.oneaiapikey.com/v1", api_key=os.environ["ONEAIAPIKEY_API_KEY"])

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "email": {"type": ["string", "null"]},
        "topics": {"type": "array", "items": {"type": "string"}},
    },
    "required": ["name", "email", "topics"],
    "additionalProperties": False,
}

reply = client.chat.completions.create(
    model="gpt-6-luna:free",
    messages=[{"role": "user", "content": "Extract the contact: 'Hi, I'm Dana Lee (dana@example.com), asking about invoices and refunds.'"}],
    response_format={"type": "json_schema", "json_schema": {"name": "contact", "strict": True, "schema": schema}},
)
contact = json.loads(reply.choices[0].message.content)
print(contact)
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.oneaiapikey.com/v1", apiKey: process.env.ONEAIAPIKEY_API_KEY });

const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    email: { type: ["string", "null"] },
    topics: { type: "array", items: { type: "string" } },
  },
  required: ["name", "email", "topics"],
  additionalProperties: false,
};

const reply = await client.chat.completions.create({
  model: "gpt-6-luna:free",
  messages: [{ role: "user", content: "Extract the contact: 'Hi, I'm Dana Lee (dana@example.com), asking about invoices and refunds.'" }],
  response_format: { type: "json_schema", json_schema: { name: "contact", strict: true, schema } },
});
console.log(JSON.parse(reply.choices[0].message.content));

In the Responses API the same schema goes in text.format instead of response_format.

What is JSON mode?

JSON mode, response_format: {"type": "json_object"}, only promises valid JSON, not a particular shape. Describe the shape in the prompt, and include the word “JSON” in it (some providers reject the request otherwise). Prefer JSON Schema where the model supports it.

Response healing. Ask the gateway to repair JSON that does not parse: add "plugins": [{"id": "response-healing"}] to the request body (it is removed before the request reaches the model) or send the header X-Response-Healing: on. It works on non-streaming /v1/chat/completions requests with response_format, and /v1/responses requests with text.format, of type json_object or json_schema. When the reply text is not valid JSON, the gateway removes a Markdown code fence or prose around the value and trailing commas, and closes a reply that was cut off after its last complete member (the member that was cut is dropped, never guessed). The result is used only if it has the requested top-level shape. Valid JSON and text that cannot be repaired are returned unchanged, and billing does not change. It does not check your schema, so still validate.

Claude models

Forced tool call (OpenAI format). Define one tool whose parameters are your schema and force it with tool_choice. The arguments of the tool call are your JSON. This works for every model with tool support, so it is also the most portable method.

Forced tool, works for Claude and others
reply = client.chat.completions.create(
    model="qwen-qwen3.8-27b:free",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Extract the contact: 'Hi, I'm Dana Lee (dana@example.com), asking about invoices and refunds.'"}],
    tools=[{"type": "function", "function": {
        "name": "save_contact",
        "description": "Save the extracted contact.",
        "parameters": schema,           # the same JSON Schema as above
    }}],
    tool_choice={"type": "function", "function": {"name": "save_contact"}},
)
contact = json.loads(reply.choices[0].message.tool_calls[0].function.arguments)

Native structured output (Anthropic format). On /v1/messages, Anthropic’s output_config.format with a JSON Schema is passed through to Claude, and the reply text is JSON that matches the schema.

Anthropic output_config
import anthropic, json, os

client = anthropic.Anthropic(base_url="https://api.oneaiapikey.com", api_key=os.environ["ONEAIAPIKEY_API_KEY"])

message = client.messages.create(
    model="qwen-qwen3.8-27b:free",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Extract the contact: 'Hi, I'm Dana Lee (dana@example.com), asking about invoices and refunds.'"}],
    extra_body={"output_config": {"format": {"type": "json_schema", "schema": schema}}},
)
contact = json.loads(message.content[0].text)

Should I validate the output?

Yes. Even with strict schemas, check the result before you use it: a reply cut off by max_tokens (finish_reason: "length") is incomplete JSON, and a model can refuse instead of answering. Parse inside a try, validate with your schema library (Pydantic, Zod, JSON Schema), and retry or fall back when it fails.

↑ ↓ moveEnter openEsc close
New accounts$10 credit on sign-up
Claim $10 credit →