Module 2 • Topic 5

Output Formatting: JSON, Tables, Markdown & More

Control how the AI formats its response — structured data, tables, lists, and machine-readable formats.

Output Formatting: JSON, Tables, Markdown & More

Controlling HOW the AI formats its response is just as important as controlling WHAT it says. For production systems, structured output is non-negotiable — downstream code needs to parse AI responses reliably.

Key Concepts

  • Structured Output: Machine-readable formats like JSON, XML, CSV

Controlling HOW the AI formats its response is just as important as controlling WHAT it says. For production systems, structured output is non-negotiable — downstream code needs to parse AI responses reliably.

Key Concepts

  • Structured Output: Machine-readable formats like JSON, XML, CSV
  • Semi-Structured Output: Human-readable but organized formats like Markdown, tables, lists
  • Format Enforcement: Techniques to guarantee the model outputs valid structured data

Common Output Formats

FormatBest ForParsing Difficulty
JSONAPI responses, data pipelinesEasy (native parsing)
MarkdownDocumentation, reportsMedium
CSVSpreadsheet data, bulk analysisEasy
XMLLegacy systems, SOAP APIsMedium
YAMLConfiguration filesEasy
Plain textHuman consumptionN/A
HTMLWeb contentMedium

Techniques for Reliable JSON Output

  1. Schema Definition: Provide the exact JSON schema you expect
  2. Assistant Prefix: Start the response with { to force JSON mode
  3. Few-Shot Examples: Show 1-2 examples of the desired JSON structure
  4. Validation Instructions: "Ensure the output is valid JSON. Do not include any text before or after the JSON object."

Example: Forcing JSON Output

"Analyze the following customer review and output a JSON object with this exact schema:

`json

{

"sentiment": "positive" | "negative" | "neutral",

"confidence": 0.0 to 1.0,

"key_topics": ["string"],

"summary": "string (max 50 words)"

}

`

Output ONLY the JSON object. No explanations, no markdown code fences."

Table Generation

"Create a comparison table with these exact columns:

Use checkmarks (✅) and crosses (❌) for boolean features. Include at least 8 rows."

Markdown Reports

"Generate a technical report in this exact Markdown structure:

Executive Summary (2-3 sentences)

Key Findings (numbered list, 5 items)

Data Analysis (include a table)

Recommendations (bullet points)

Next Steps (action items with owners)"

Format Enforcement Strategies

  • API-Level: OpenAI's response_format: { type: "json_object" } forces JSON
  • Grammar-Based: Tools like llama.cpp support BNF grammars for exact format control
  • Validation Loop: Generate → parse → if invalid, regenerate with error feedback
  • Few-Shot + Schema: Most reliable prompt-level technique

Common Mistakes

  • Not specifying the EXACT format (leading to inconsistent outputs across runs)
  • Asking for JSON but not specifying the schema (model invents its own keys)
  • Mixing format instructions with content instructions (separate them clearly)
  • Not validating output format programmatically before using it downstream
  • Forgetting that complex formats (nested JSON) need more explicit instructions

Read to the end of the lesson to mark it as read.