PM-301c · Module 2
JSON Mode
4 min read
JSON mode (available in most major model APIs) constrains the model's output to valid JSON syntax. It is a necessary tool but not a sufficient one. JSON mode guarantees syntactic validity — it does not guarantee schema compliance. A response that is valid JSON but contains the wrong keys, wrong types, or missing required fields is a schema violation even if JSON mode is enabled.
- What JSON Mode Does Constrains token generation to ensure the output is parseable as JSON. No prose before or after. No markdown code fences. No trailing commas. Valid JSON syntax is guaranteed at the API level.
- What JSON Mode Does Not Do JSON mode does not enforce your schema. It does not guarantee specific keys are present. It does not validate types, ranges, or enum values. It does not prevent the model from inventing keys you did not specify. Schema compliance requires prompt-level specification in addition to JSON mode.
- Enabling JSON Mode Pass response_format: { type: "json_object" } in the API call (OpenAI-compatible APIs). Anthropic's API uses a different mechanism — structured output via tool definitions. Confirm which mechanism your API version supports — field names and semantics differ across providers.
- JSON Mode + Schema Specification Combine both: enable JSON mode at the API level AND provide schema specification in the prompt. JSON mode catches syntax errors. Schema specification in the prompt drives schema compliance. Neither alone is sufficient for production use.
{
"model": "your-model",
"response_format": { "type": "json_object" },
"messages": [
{
"role": "system",
"content": "You are a contract analysis specialist. Return all responses as JSON conforming to the ContractReview schema provided in each request."
},
{
"role": "user",
"content": "Analyze this contract clause and return JSON with keys: risk_level (low|medium|high|critical), issue (string), recommendation (string).\n\nClause: [clause text here]"
}
]
}