Write prompts that return reliable structured output

8 steps · about 2 h 5 min · Beginner

  1. Learn the parts of a prompt (guide, Prompt Engineering Guide, 10 min)

    Instruction, context, input and output format are the pieces you will adjust in every later step.

  2. Make instructions specific and testable (guide, Prompt Engineering Guide, 15 min)

    Vague requests produce vague answers; state the task, audience, length and format explicitly.

  3. Separate instructions from data with tags (doc, Anthropic, 10 min)

    Clear delimiters stop the model from confusing your instructions with the text it is asked to process.

  4. Show the format with a few examples (guide, Prompt Engineering Guide, 15 min)

    Two or three worked examples fix the output shape far more reliably than a description alone.

  5. Ask for reasoning before the answer (guide, Prompt Engineering Guide, 15 min)

    Letting the model work through steps improves multi-step tasks; keep the reasoning separate from the final field.

  6. Describe the output with a JSON schema (tutorial, JSON Schema, 15 min)

    A schema turns 'return JSON' into a precise contract for field names, types and required values.

  7. Validate every response in code (doc, Pydantic, 25 min)

    Parse each reply into a typed model so malformed or missing fields fail loudly instead of corrupting your data.

  8. Guard the prompt against injected instructions (guide, Prompt Engineering Guide, 20 min)

    User-supplied text can try to override your instructions; learn the common attacks and practical defences.