Enabling JSON Mode in Chat Completions
When using LLMs to build backend pipelines, you often need the output in structured formats like JSON instead of plain conversational text. Let us review how to configure JSON Mode.
1. How JSON Mode Works
JSON Mode guarantees that the model will return a valid JSON string that can be parsed by JSON.parse().
To enable JSON Mode:
- Set the
response_formatparameter object to typejson_object. - Crucial Rule: You must explicitly instruct the model to output JSON inside the system or user prompt. If you omit the word "JSON" or "json" in your prompts, the API will throw a validation error.
2. Implementing JSON Mode in Node.js
Configure your API payload settings:
import { openai } from "../lib/openai";
async function generateUserMockProfile() {
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
// 1. Force the model to output a valid JSON string
response_format: { type: "json_object" },
messages: [
{
role: "system",
// 2. Explicitly include "json" in the prompt instructions
content: "You are a database helper. Generate mock profiles for 3 users. Return a JSON object with a users list array."
}
],
});
const rawJsonText = completion.choices[0].message.content || "";
// Safely parse because JSON structure validity is guaranteed
const parsedData = JSON.parse(rawJsonText);
console.log("Mock Users List:", parsedData.users);
}3. Difference Between JSON Mode and Structured Outputs
While JSON Mode guarantees that the string format is valid JSON, it does not guarantee that the JSON matches any specific key-value schema. The model can still omit keys or change property names.
To enforce a strict structural schema, use Structured Outputs instead.
Published on Last updated: