Validate JSON and Branch by Failure Class
Understand how to validate JSON output from AI features to ensure safe rendering. Explore error classification between parse and schema failures, learn to branch UI states accordingly, and implement fallback mechanisms to handle invalid data securely. This lesson helps you build trustworthy interfaces that respond correctly to diverse data validation outcomes.
We'll cover the following...
Buffering until done gives you a complete buffer. Complete is not the same as trustworthy, and that gap is what this lesson closes.
Buffering deltas and parsing only at the done boundary, as the last lesson established, produces a complete buffer, but a complete buffer is not the same as a trustworthy one. When a streaming response finishes, the buffer is still untrusted text. The first reliable checkpoint is a function that turns that buffer into either a validated object you can render or a typed failure you can route to a fallback UI.
Use the result type to make failures explicit. A parse_error means JSON.parse failed and you only have an exception message. A schema_error means parsing succeeded but the object did not match the expected shape and you ...