One call, from fetch to a validated object — streaming structured output without the plumbing
You asked the model for a JSON object and set stream: true , because you want the UI (or the next step) to start the moment the first field lands instead of blocking on the last token. Then you're here: const res = await fetch ( url , { method : " POST " , body }); const reader = res . body ! . getReader (); const decoder = new TextDecoder (); let buf = "" , json = "" ; for (;;) { const { value ,…
You instructed the model to deliver a JSON object, enabling streaming output. You initiated the request using fetch with POST method and provided the body. A reader and decoder were set up to process the response. The loop iterated, appending decoded lines to a buffer and parsing JSON. If the payload matched [DONE], it continued to the next iteration.
After each line, the buffer was updated to exclude the processed lines. On successful parsing, the data was set or appended to the JSON string. Once the loop completed, the parsed JSON was converted into a valid object using Schema.parse. The operation entailed tackling three issues - the transport, partial parsing, and validation.
I created three separate packages to address these issues, followed by another package that integrated them. With a single call to structured, you obtained both an async iterable of partial snapshots and a holder for the final validated result. The return value is both an async iterable for live partials and a holder for the final validated result.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.