Prompt
llm api structured json
Latest observation
Oct 8, 2026
OpenAI APIWeb search: off
If you want an LLM API to return structured JSON, the common approaches are:
1) Use a JSON-only instruction
Ask the model to output only valid JSON.
Prompt example
Return only valid JSON with these fields:
- name: string
- age: integer
- skills: array of strings
Expected output
{
"name": "Alice",
"age": 29,
"skills": ["Python", "SQL"]
}
2) Use schema-based output
If your API supports it, provide a schema so the model must conform to it.
Example JSON Schema
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"skills": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["name", "age", "skills"],
"additionalProperties": false
}
3) Validate after generation
Even with structured output, you should still:
- parse JSON
- validate against schema
- retry if invalid
4) Use function/tool calling
Many LLM APIs support function calling or tools, which is often better than raw JSON prompting.
Example
{
"name": "create_profile",
"arguments": {
"name": "Alice",
"age": 29,
"skills": ["Python", "SQL"]
}
}
If you want, I can give you:
- a Python example
- an OpenAI API example
- a JSON schema template
- or a best-practice prompt for structured output.
Brands