System Prompt for JSON and Structured Output
Table of Contents
If your application reads AI output programmatically — populates a UI, writes to a database, triggers another API call — you need predictable structure. Free-form prose breaks every parser. JSON output is the standard answer, and the system prompt is where you make it happen reliably. This guide covers the patterns that actually work.
The free system prompt generator can produce a structured-output prompt with one toggle.
The Two Paths to Structured Output
You have two ways to get reliable JSON from an LLM:
- API-level enforcement — OpenAI's "JSON mode" (response_format: { type: "json_object" }) and "structured outputs" (response_format: { type: "json_schema", ... }) force the model to produce valid JSON. Anthropic and Google have similar features now too.
- Prompt-level instruction — telling the model in the system prompt to output JSON. Less reliable than API enforcement but works on any model and any provider.
For production, use both. API enforcement catches syntactic errors (invalid JSON), and the system prompt handles semantic correctness (the right fields with the right meanings).
The Base JSON System Prompt
"You are a [role]. Respond ONLY with valid JSON in this exact format. Do not include any text outside the JSON object. Do not include markdown code fences (```json or ```). Do not include explanations or apologies. The response must parse with JSON.parse() in JavaScript without modification.
Required format:
{ "field1": string, "field2": number, "field3": boolean, "field4": string[] }
If you cannot determine a value for a field, use null. Never omit a field. Never add fields not listed above."
Common JSON Failures and Fixes
- Markdown code fences wrap the JSON — "Do not include markdown code fences" in the prompt fixes this in 95% of cases. The other 5% needs API-level JSON mode.
- Extra explanation before/after — "Respond ONLY with the JSON object. No prefix, no suffix, no explanation."
- Missing fields — "Always include all fields. Use null if you cannot determine a value."
- Extra fields — "Never add fields not in the schema. Stick to the required fields exactly."
- Wrong field types — show the example with explicit types:
{ "count": 5, "name": "Alice", "active": true }instead of just{ "count": 5, "name": "Alice", "active": true } - Single quotes instead of double — JSON requires double quotes. The model will sometimes use single quotes in long sessions. "All strings must use double quotes, not single quotes."
Always Show a Concrete Example
Models follow examples better than abstract rules. Always include a concrete example in the system prompt:
"Example output:
{ "summary": "User asked about pricing", "intent": "pricing", "urgency": "low", "needs_human": false }"
One example is worth ten paragraphs of schema description.
Validation in Your Application Code
Even with API enforcement and a tight system prompt, validate the output in your application code before using it. Use a schema validator (Zod, Joi, Ajv, Pydantic) to confirm the structure matches what you expect. If validation fails, you have two options: retry with a corrective prompt ("the previous response was invalid, please provide valid JSON") or fail gracefully and log the error for debugging.
Production AI apps that read JSON output without validation eventually crash. Validate every time.
Token Cost of Strict JSON Output
JSON output is more verbose than free-form text. Field names, brackets, commas, and quotes all count as tokens. A 100-word free-form summary becomes a 130-150 token JSON response. This adds up at scale.
Trade-off: pay slightly more per request for structured output that does not break your parser. Almost always worth it. Use the AI cost calculator to model the difference.
When to Use XML Instead of JSON
Anthropic's Claude is trained to respect XML tags more than JSON. For Claude-only applications, XML tags can be more reliable: <summary>User asked about pricing</summary><intent>pricing</intent>
For multi-provider or OpenAI-primary applications, JSON is the standard. Pick one and stick with it across your prompt.
Generate a JSON Output Prompt
Toggle "use structured output" in our generator. Copy-ready output.
Open System Prompt Generator
