Prompt Engineering Tutorial 0/120 lessons ~6 min read Lesson 22

    Output Formatting

    Controlling output format is the difference between an LLM that 'sometimes works' and one that ships.

    Course progress0%
    Focus
    7 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Controlling output format is the difference between an LLM that 'sometimes works' and one that ships. JSON, markdown, tables, fixed schemas — all are unlocked through the format section of your prompt.

    Beginner analogy: Restaurants give chefs plating diagrams, not free reign. Format prompts plate the model's output the same way every time.

    Understanding the topic

    Core concepts to understand:

    • Specify format explicitly (JSON, markdown, CSV, custom).
    • Provide a schema or template if possible.
    • Show ONE example of the exact format.
    • Use 'response_format' parameter or JSON mode on supported models.
    • Validate output programmatically (Zod, Pydantic) and retry on failure.

    Syntax reference

    Visual workflow / architecture:

    bash
    Prompt:
    "Return JSON exactly matching:
    {
    \"summary\": string,
    \"tags\": string[],
    \"sentiment\": \"positive\" | \"negative\" | \"neutral\"
    }"

    Real-world use

    Every LLM that integrates with a backend uses strict output formatting — invoice parsers, lead scorers, ticket triagers. Without it, downstream code breaks.

    Best practices

    • Validate output and retry once with the error appended to the prompt.
    • Use providers' built-in JSON modes when available.
    • Avoid asking for markdown when you'll parse it as JSON later.

    Common mistakes

    • Allowing 'or anything else you think is relevant' — model goes wild.
    • Mixing markdown with JSON — parser breaks.

    Hands-on exercise

    Interview preparation — practice these questions:

    • Q1. How do you guarantee JSON output from an LLM?
    • Q2. What's a safe retry strategy on format failure?
    • Q3. JSON mode vs prompt-based formatting — pros & cons?
    • Q4. How do you handle markdown vs raw text?
    Ready to mark this lesson complete?Track your journey across the entire course.