๐Ÿ“š Intermediate

7. Error Handling in Prompts

Build robust prompts that handle edge cases, malformed inputs, and unexpected scenarios gracefully.

Building Robust Prompts

Production prompts must handle the unexpected. Users will provide malformed input, ask edge-case questions, and trigger scenarios you didn't anticipate.

๐Ÿ’ก Production Reality: The prompts that work perfectly in testing often fail in production because they don't handle edge cases.

Common Failure Modes

โš ๏ธ

Missing Input

User provides empty or null values

โš ๏ธ

Malformed Input

Input doesn't match expected format

โš ๏ธ

Out of Scope

Request outside your domain

โš ๏ธ

Ambiguous Request

Unclear what the user wants

Defensive Prompt Patterns

Pattern 1: Input Validation Instructions

Before processing the request: 1. Check if the input is a valid product URL 2. If the URL is malformed, return: {"error": "invalid_url", "message": "Please provide a valid product URL"} 3. If the URL is from an unsupported site, return: {"error": "unsupported_site", "message": "We only support Amazon, eBay, and Walmart"} 4. Only proceed with analysis if validation passes

Pattern 2: Fallback Responses

Classify the customer inquiry into one of these categories: - billing - technical_support - product_question - complaint - other If you cannot confidently classify the inquiry (confidence < 70%), return "needs_human_review" and explain why in the reason field. Return format: { "category": "string", "confidence": 0.0-1.0, "reason": "string (only if needs_human_review)" }

Pattern 3: Explicit Edge Case Handling

Extract the price from the product description. Special cases to handle: - If multiple prices exist, use the current/sale price (not original) - If price range (e.g., "$10-$20"), use the lower bound - If price is "Free" or "$0", return 0 - If no price found, return null - If price is in foreign currency, return as-is with currency code Return: {"price": number|null, "currency": "USD"|"EUR"|etc, "notes": "string if any special handling applied"}

Graceful Degradation

When the AI can't fully complete a task, it should provide partial results rather than failing completely.

Analyze this financial document and extract: 1. Revenue figures 2. Expense breakdown 3. Profit margins 4. Year-over-year growth For each field: - If clearly stated: extract the value - If can be calculated: show the calculation - If partially available: extract what's possible and note what's missing - If unavailable: mark as "not_found" with reason Never fail completely - always return as much as you can extract.

Error Response Formats

When you encounter an error, return a structured error response: { "success": false, "error": { "code": "ERROR_CODE", "message": "Human-readable message", "details": "Additional context", "suggestion": "What the user can do to fix it" } } Error codes: - INVALID_INPUT: Input doesn't match expected format - MISSING_REQUIRED: Required field is missing - OUT_OF_SCOPE: Request outside supported functionality - AMBIGUOUS_REQUEST: Cannot determine user intent - PROCESSING_ERROR: Error during processing
๐Ÿ”‘ Key Takeaway: Anticipate failures before they happen. Build prompts that validate inputs, handle edge cases explicitly, and degrade gracefully when full completion isn't possible.
๐Ÿงช

Try It Yourself

Practice what you learned with our interactive tools.

โœจ Open Magic Optimizer
๐Ÿ’ก

Pro Tips

  • โ€ข Be specific with your instructions
  • โ€ข Use examples when possible
  • โ€ข Iterate and refine your prompts

Sign in to track your progress

Sign in to Complete