Models → Pricing → Docs → Blog → Changelog → Contact → Status →
Get started freeLog in

Structured output (JSON)

Ask a model for JSON only, or for JSON that follows a schema.

⏱ 4 min read

Overview

Models with the json_mode capability on Models (or in capabilities from GET /v1/models) accept a JSON output setting. Where you put it depends on the API format:

FormatFieldValues
Chat completions /v1/chat/completionsresponse_format{"type":"json_object"} or {"type":"json_schema","json_schema":{…}}
Responses /v1/responsestext.format{"type":"json_object"} or {"type":"json_schema","name":…,"schema":{…},"strict":…}
Gemini :generateContentgenerationConfig.responseMimeType"application/json", optionally with responseSchema or responseJsonSchema
Anthropic /v1/messages—No JSON-output field is read. Use a tool with an input_schema, or ask for JSON in the prompt.

The setting is passed to the model you chose. FreeModel does not check or repair the JSON the model writes, so always parse it and validate it in your code.

JSON mode (json_object)

The model answers with a JSON object. Say in the prompt what keys you want — JSON mode alone does not tell the model the shape.

curl https://freemodel.in/v1/chat/completions \
  -H "Authorization: Bearer fm_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-6-luna",
    "response_format": {"type": "json_object"},
    "messages": [
      {"role": "system", "content": "Reply with JSON: {\"city\": string, \"country\": string}."},
      {"role": "user", "content": "Where is the Gateway of India?"}
    ]
  }'

JSON Schema (json_schema)

import json
from openai import OpenAI

client = OpenAI(api_key="fm_your_key_here", base_url="https://freemodel.in/v1")

schema = {
    "type": "object",
    "properties": {
        "title": {"type": "string"},
        "priority": {"type": "string", "enum": ["low", "medium", "high"]},
        "tags": {"type": "array", "items": {"type": "string"}},
    },
    "required": ["title", "priority", "tags"],
    "additionalProperties": False,
}

r = client.chat.completions.create(
    model="gpt-6-luna",
    messages=[{"role": "user", "content": "Make a ticket: the login page is down for all users."}],
    response_format={
        "type": "json_schema",
        "json_schema": {"name": "ticket", "schema": schema, "strict": True},
    },
)

ticket = json.loads(r.choices[0].message.content)
print(ticket["priority"])

Same request on /v1/responses

resp = client.responses.create(
    model="gpt-6-luna",
    input="Make a ticket: the login page is down for all users.",
    text={"format": {"type": "json_schema", "name": "ticket", "schema": schema, "strict": True}},
)
ticket = json.loads(resp.output_text)

On /v1/responses a json_schema format is only used when it has a schema.

Gemini format

{
  "contents": [{"role": "user", "parts": [{"text": "Make a ticket: the login page is down."}]}],
  "generationConfig": {
    "responseMimeType": "application/json",
    "responseSchema": {
      "type": "OBJECT",
      "properties": {"title": {"type": "STRING"}, "priority": {"type": "STRING"}},
      "required": ["title", "priority"]
    }
  }
}

Gemini-style upper-case types are converted to JSON Schema before the request goes to the model. Without a schema, application/json becomes JSON mode.

When the output is not valid JSON

Plan for these cases:

  • The answer was cut off. If finish_reason is length, the JSON is incomplete. Raise max_tokens or ask for less.
  • The model's provider refuses the setting. If the provider behind a model rejects response_format, the gateway retries the request without it rather than failing. The answer is then plain text that may or may not be JSON.
  • The model ignores part of the schema. Validate with a schema library (for example jsonschema or Pydantic in Python, Zod or Ajv in JavaScript) and retry or fall back when it fails.
from jsonschema import validate, ValidationError

try:
    data = json.loads(r.choices[0].message.content)
    validate(data, schema)
except (json.JSONDecodeError, ValidationError):
    data = None  # retry, or ask the model to fix its output

Tips

  • Keep schemas small and flat; deep nesting raises the chance of mistakes.
  • Put the field meanings in description keys of the schema — the model reads them.
  • Reasoning models spend tokens thinking before they write the JSON. Leave room in max_tokens; see Reasoning models.
  • For calls that act on the world, tool calling is often a better fit than free-form JSON.

Billing

JSON output costs the same as any other output: input plus output tokens, times the model's multiplier. See Plan tokens and multipliers.

Was this page helpful?
Still stuck?
Open a support ticket from your dashboard or message our support bot on Telegram.
Telegram supportGet help