Back to feed
Dev.to
Dev.to
7/1/2026
Never trust an LLM's output directly. Here's the validation layer I put on every agent.

Never trust an LLM's output directly. Here's the validation layer I put on every agent.

Short summary

Don't blindly trust LLM outputs—structured mode doesn't guarantee semantic correctness. Implement a three-stage validation pattern (parse, validate, classify) using TypeScript and Zod to force proper error handling and distinguish transient failures from permanent ones.

  • JSON validity ≠ semantic correctness: valid JSON can contain made-up UUIDs, invalid floats, or hallucinated structure
  • Build parse-validate-classify stages between raw model output and production code
  • Use discriminated unions to force callers to handle both success and specific failure modes

Generated with AI, which can make mistakes.

Is this a good recommendation for you?

Comments

Failed to load comments. Please try again.

Explore more