Prompt
LLM API with JSON output
Latest observation
Oct 8, 2026
OpenAI APIWeb search: off
If you want an LLM API to return JSON-only output, there are a few common approaches depending on the provider.
General options
1. Prompt for JSON
Ask the model explicitly:
- “Return valid JSON only.”
- “Do not include markdown, explanations, or code fences.”
Example prompt:
Extract the following fields and return valid JSON only:
- name
- email
- phone
2. Use structured output / JSON mode
Some APIs support a built-in JSON response mode or schema enforcement.
Examples:
- OpenAI: structured outputs / JSON schema
- Anthropic: tool use / structured formatting
- Google Gemini: JSON output mode
- Azure OpenAI: similar capabilities depending on model/deployment
3. Use function calling / tool calling
Instead of free-form text, define a function schema and have the model fill it. This is often the most reliable way to get structured data.
Example schema:
{
"type": "object",
"properties": {
"name": { "type": "string" },
"email": { "type": "string" }
},
"required": ["name", "email"],
"additionalProperties": false
}
Practical recommendation
If your API supports it, use:
- JSON schema / structured outputs for strict validation
- Function calling if you need the output to map directly into an application
- A fallback prompt-only JSON instruction if the model doesn’t support structured mode
Important
Even with JSON mode, you should still:
- validate the response
- handle malformed or incomplete JSON
- retry if needed
If you want, I can give you:
- an OpenAI API example
- a Python example
- a Node.js example
- or a provider comparison for JSON output