Handling Inconsistent Outputs
You'll learn how to build resilience into your prompts when LLMs don't return data in the format you expect.
The Problem of Unpredictability
Even with perfect schema specification and delimiters, LLMs sometimes "drift" from your format. They might wrap JSON in markdown code blocks, add explanatory text before the data, or subtly change field names. Since you've already learned to define schemas and use structured output patterns (lessons 157-159), now you need to handle the times those patterns fail.
Three-Layer Defense Strategy
1. Validation
Check the output against your expected format before using it. Parse JSON, verify required fields exist, and confirm data types match your schema. If validation fails, don't crash—move to step 2.
2. Retry Logic
When output is malformed, send the request again—sometimes with a modified prompt that emphasizes the format requirement. You might retry 2-3 times with slightly higher temperature penalties or more explicit instructions from your template system (lesson 147-156).
3. Fallbacks
If retries fail, have a backup plan: return a safe default value, log the error for review, or switch to a simpler prompt pattern that's more reliable. This is similar to the API fallback strategies you learned (lesson 96), but applied to output quality instead of service availability.
Analogy
Think of ordering food delivery: validation checks if you got what you ordered, retry logic means calling the restaurant when it's wrong, and fallback is having frozen pizza ready just in case.
Key Takeaway: Inconsistent LLM outputs are inevitable—build validation, retry logic, and fallbacks into your pipeline so one bad response doesn't break your entire application.