Output Formatting
Production AI features almost always need structured output — JSON, markdown tables, XML, code blocks — that downstream code can parse.
Introduction
Production AI features almost always need structured output — JSON, markdown tables, XML, code blocks — that downstream code can parse. Formatting failures cause more incidents than any other AI bug.
Beginner analogy: If you ask the AI for 'a list', you might get bullets, numbers or prose. If you ask for 'JSON with keys name and age', you get parseable data every time.
Understanding the topic
Core concepts to understand:
- Specify the format explicitly — show a JSON schema or example.
- Use JSON mode / structured outputs APIs (OpenAI, Anthropic).
- Use Zod or Pydantic to validate and retry on failure.
- Always handle malformed output gracefully — never
JSON.parseblindly.
Syntax reference
Visual workflow / architecture:
Prompt:"Extract name, email, role.Return JSON only:{ name: string, email: string, role: string }"│▼{"name": "Ada Lovelace","email": "ada@example.com","role": "Engineer"}│▼Zod / Pydantic validation│✅ ok ❌ retry with error feedback
Real-world use
OpenAI's response_format=json_schema and Anthropic's tool use guarantee valid JSON. Vercel AI SDK ships generateObject() with built-in schema validation.
Best practices
- Use the provider's structured-output mode whenever possible.
- Validate with a schema (Zod, Pydantic, JSON Schema).
- Retry once with the validation error fed back to the model.
Common mistakes
- Parsing markdown code fences manually — fragile.
- Trusting the model to follow format on long outputs without validation.
Hands-on exercise
Interview preparation — practice these questions:
- Q1. How do you guarantee JSON output from an LLM?
- Q2. What is OpenAI's structured outputs feature?
- Q3. How do you handle a malformed JSON response?