GoofyCubes
6

Chapter 6

Prompt Engineering and Structured Outputs

Learning Objective

Learn how to write prompts that produce consistent, safe, and machine-readable responses.

What it means

Prompt engineering is the practice of writing clear instructions for an LLM. In production, prompts should define role, task, constraints, input context, output format, and failure behavior. Structured outputs such as JSON make it easier for software systems to validate and consume model responses.

Why it matters

Vague prompts produce vague outputs. A production system needs predictable responses. For example, an API cannot reliably process a paragraph if it expects a JSON object with specific fields.

Healthcare Example

Instead of asking 'What is in this clinical note?', ask the model to extract patient conditions, medications, missing documentation, and return valid JSON only. This makes downstream validation easier.

Good Prompt Pattern

SYSTEM:
You are a healthcare document analysis assistant.
Use only the provided document text.
If information is missing, return null.
Return valid JSON only.

USER:
Extract the following fields: patient_age, diagnosis_codes, medications, missing_information.
Document:
{document_text}

Code: Parse Model JSON Safely

import json

def parse_llm_json(response_text):
    try:
        data = json.loads(response_text)
    except json.JSONDecodeError:
        return {"valid": False, "error": "Invalid JSON"}

    required = ["patient_age", "diagnosis_codes", "medications", "missing_information"]
    missing = [field for field in required if field not in data]
    if missing:
        return {"valid": False, "error": f"Missing fields: {missing}"}

    return {"valid": True, "data": data}

Common Mistakes

  • Prompt does not specify output format.
  • Prompt asks multiple unrelated tasks at once.
  • Prompt does not tell the model what to do when data is missing.
  • No validation after the model responds.

Interview Q&A

Q: What makes a good production prompt?

A: A good prompt defines role, task, context, constraints, output format, and fallback behavior.

Q: Why use JSON output?

A: JSON makes the response machine-readable and easier to validate, store, route, and integrate with APIs.

Architect Takeaway

Production prompts should be treated like code: versioned, tested, reviewed, and monitored.