Ask for JSON
Provide schema example in prompt. 'Respond only with JSON matching this schema: …'. Works ~90% of the time. Retry on malformed.
Advertisement
JSON mode
Modern APIs (OpenAI, Anthropic) have flag to force valid JSON. Doesn't guarantee schema, just syntax.
Advertisement
Structured outputs
Provide JSON Schema. API enforces schema-conformance via constrained decoding. Guaranteed valid. Slower.
Function calling
Define function signature. Model produces (name, args). Cleaner than JSON in prompt. Standard for tool use.