Prompt
How do I get structured JSON output from an AI API?
Latest observation
To get structured JSON output from an AI API, the usual goal is to make the model return data that matches a schema you define, instead of free-form text.
Best options
1) Use the API’s native JSON / structured output mode
If your AI provider supports it, this is the most reliable approach.
Typical patterns:
- JSON mode: forces valid JSON only
- Structured outputs / schema mode: forces JSON that matches a schema
- Function/tool calling: model returns arguments in a structured format
Example idea:
{
"name": "Alice",
"age": 32,
"skills": ["python", "api", "ml"]
}
If the API supports a schema, define something like:
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"skills": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["name", "age", "skills"],
"additionalProperties": false
}
2) Prompt the model very explicitly
If structured mode is not available, use a strong prompt:
Example:
Return only valid JSON. Do not include markdown, explanations, or extra text.
Use this schema:
{ "name": string, "age": number, "skills": string[] }
This helps, but it is less reliable than native schema enforcement.
3) Validate and retry
Always:
- parse the output as JSON
- validate against your schema
- if invalid, retry with a correction prompt
This makes the system much more robust.
Practical example
Say you want extracted info from text:
Input:
“John is 29 and knows JavaScript and Go.”
Desired output:
{
"name": "John",
"age": 29,
"skills": ["JavaScript", "Go"]
}
Prompt:
Extract the person's name, age, and skills.
Return only valid JSON matching:
{
"name": "string",
"age": "number",
"skills": ["string"]
}
Tips to improve reliability
- Keep schemas simple
- Set
temperature = 0for consistency - Specify
additionalProperties: falseif supported - Ask for arrays/objects explicitly
- Don’t ask for prose and JSON in the same response
- Post-process with a JSON parser and schema validator
If you tell me your API/provider
I can give you a copy-paste example for:
- OpenAI
- Anthropic
- Gemini
- Azure OpenAI
- local models / Ollama
- generic REST API