JSON output

Get valid JSON back from the model with the response_format option, plus the prompt and token settings that make it reliable.

With response_format set to a JSON object the model answers with valid JSON. A short instruction and an example in the prompt make the shape reliable.

Many apps need the model's answer as data: extracted fields, a classification, a list of items. With response_format: {"type": "json_object"} the model replies with a single valid JSON object instead of free text. Models that support JSON output are marked on Models & pricing.

Make a request#

  1. Set response_format to {"type": "json_object"}.
  2. Say in the system or user message that the answer must be JSON, and show the shape you expect with a short example.
  3. Leave room in max_tokens so the object is never cut off.

curl

curl https://usemodellane.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $MODELLANE_API_KEY" \
  -d '{
    "model": "lane-1",
    "messages": [
      {"role": "system", "content": "Extract the question and answer. Reply in JSON like {\"question\": \"...\", \"answer\": \"...\"}."},
      {"role": "user", "content": "Which is the longest river in the world? The Nile."}
    ],
    "response_format": {"type": "json_object"}
  }'

Python

import json
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["MODELLANE_API_KEY"],
    base_url="https://usemodellane.com/v1",
)

system_prompt = """Extract the question and answer from the user's text.
Reply in JSON with this shape:
{"question": "Which is the highest mountain in the world?", "answer": "Mount Everest"}"""

response = client.chat.completions.create(
    model="lane-1",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": "Which is the longest river in the world? The Nile."},
    ],
    response_format={"type": "json_object"},
)

data = json.loads(response.choices[0].message.content)
print(data["question"], "->", data["answer"])

Node.js

import OpenAI from "openai"

const client = new OpenAI({
  apiKey: process.env.MODELLANE_API_KEY,
  baseURL: "https://usemodellane.com/v1",
})

const systemPrompt = `Extract the question and answer from the user's text.
Reply in JSON with this shape:
{"question": "Which is the highest mountain in the world?", "answer": "Mount Everest"}`

const response = await client.chat.completions.create({
  model: "lane-1",
  messages: [
    { role: "system", content: systemPrompt },
    { role: "user", content: "Which is the longest river in the world? The Nile." },
  ],
  response_format: { type: "json_object" },
})

const data = JSON.parse(response.choices[0].message.content)
console.log(data.question, "->", data.answer)

The reply's content is a JSON string:

JSON

{"question": "Which is the longest river in the world?", "answer": "The Nile"}

Tips#

  • Always ask for JSON in the prompt. The option guarantees valid JSON; the prompt decides which keys and values you get.
  • Give an example object. One example with the exact keys and value types is the most reliable way to fix the shape.
  • Validate the result. JSON mode does not check your schema. Parse the content and validate it before you use it, and retry or fall back when a key is missing.
  • Check finish_reason. If it is length, the output hit max_tokens and the JSON is cut off. Raise the limit and try again.
  • Prefer tools for function arguments. When the JSON is meant to trigger an action, tool calls give each function its own schema.