Prompt
I'm building a feature that returns JSON from AI, what API should I use
Latest observation
Oct 8, 2026
OpenAI APIWeb search: off
If you want the model to reliably return JSON, use an API that supports structured output / JSON mode rather than asking the model to “please output JSON” in plain text.
Best choice
Use an API with one of these features:
- Structured Outputs: you provide a JSON Schema, and the model must match it
- JSON mode: the model is constrained to valid JSON, but less strict than schema validation
Recommended approach
If your use case has a defined shape, prefer:
- Structured Outputs with a schema
- Fallback to JSON mode only if you just need valid JSON text
Example use cases
{"name": "...", "age": 42}- arrays of objects
- classification results
- extraction tasks
- tool/API responses
Why this matters
Plain prompting can still produce:
- invalid JSON
- extra commentary
- missing fields
- wrong types
Structured output reduces those failures a lot.
If you’re using OpenAI
Use the API’s Responses API with structured outputs / JSON schema support.
Example pattern:
- define your schema
- send it in the request
- parse the response as JSON
If you’re using another provider
Look for terms like:
- “JSON mode”
- “schema mode”
- “structured output”
- “function calling” / “tool calling”
Those are usually the right features.
My recommendation
- Need strict machine-readable JSON? Use Structured Outputs
- Need just valid JSON, flexible shape? Use JSON mode
- Need the model to decide among actions or fill fields? Use tool/function calling
If you want, I can show you:
- the best OpenAI API call for JSON output, or
- examples in Python / JavaScript / TypeScript.
Brands