JSON mode
Forces the model to output valid JSON by setting response_format. Always instruct the model to produce JSON in your prompt.
Syntax
openai-api
{ response_format: { type: "json_object" } }Example
openai-api
const response = await client.chat.completions.create({
model: "gpt-4o",
response_format: { type: "json_object" },
messages: [{
role: "user",
content: "Return a JSON object with name and age for Alice, age 28"
}]
});
const data = JSON.parse(response.choices[0].message.content);
// { name: "Alice", age: 28 }