0PricingLogin
AI Agents · Lesson

Output Formatting (JSON, XML, Markdown)

Force the model to return parseable structures using JSON mode, XML tags, or strict Markdown to make outputs machine-readable.

Why Format Matters

Agents pipe model output into code. Free-form prose breaks parsers. Structured output (JSON, XML, etc.) is mandatory for any production agent.

JSON Mode

OpenAI and most providers offer a JSON mode that guarantees parseable output:

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=messages,
    response_format={'type': 'json_object'},
)
import json
data = json.loads(response.choices[0].message.content)

All lessons in this course

  1. Zero-shot, Few-shot and Chain-of-Thought
  2. System vs User vs Assistant Roles
  3. Output Formatting (JSON, XML, Markdown)
  4. Avoiding Prompt Injection in Inputs
← Back to AI Agents