Search⌘ K
AI Features

Handle Streaming Structured Output

Explore techniques to manage streaming structured JSON output effectively. Learn to buffer incomplete data, delay parsing until the stream completes, and validate JSON before rendering. Understand how to maintain a stable UI state machine during generation, handle edge cases like user interruption, and ensure reliable rendering of AI-generated structured output.

That request shape describes what arrives at the end. The next problem is everything that happens on the way there, while the object is still incomplete.

The reference request shape from the last lesson, with text.format set to json_schema and strict: true, only describes the final object; it says nothing about what happens while that object is still arriving. A common failure mode shows up the first time a UI tries to render structured output while it is still streaming. The reducer appends each response.output_text.delta into buffer, calls JSON.parse(buffer) on every chunk, and the UI alternates between exceptions, empty fallback, and briefly correct looking partial cards that immediately break on the next delta. ... ...