Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.6 — Streaming Structured Output
LLM Engineering · Example 1 of 1

A partial JSON reader: live and stable views

The same reply is read after every 6 characters. The Java version uses the course's Json helper; real Java projects use Jackson.

Half of a JSON document is not valid JSON, so a normal parser fails until the end. A partial-JSON reader closes the open string and brackets so you can show fields as they arrive, for display only, never for decisions.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write close_brackets(text) that, for text with no strings (only {, }, [, ] and other characters), returns the closing brackets needed, in the right order. For {[1,{ it returns }]}.

02

Write last_complete_prefix(text) that returns the text before the last comma, or an empty string if there is no comma.

Explain it without notes

01

Why is a prefix of a JSON document usually not valid JSON, and how does a partial reader deal with it?

02

What is the difference between a live and a stable view, and when do you use each?

03

Why should you never act on partial JSON?

A partial JSON reader: live and stable views
Sign in to run this example in your browser.

Expected output

 6 chars | live: {} | stable: {}
12 chars | live: {"name":"Ash"} | stable: {}
18 chars | live: {"name":"Asha Rao"} | stable: {}
24 chars | live: {"name":"Asha Rao"} | stable: {"name":"Asha Rao"}
30 chars | live: {"name":"Asha Rao","skills":[""]} | stable: {"name":"Asha Rao"}
36 chars | live: {"name":"Asha Rao","skills":["python"]} | stable: {"name":"Asha Rao"}
42 chars | live: {"name":"Asha Rao","skills":["python","sql"]} | stable: {"name":"Asha Rao"}
48 chars | live: {"name":"Asha Rao","skills":["python","sql"]} | stable: {"name":"Asha Rao","skills":["python","sql"]}
54 chars | live: {"name":"Asha Rao","skills":["python","sql"],"years":1} | stable: {"name":"Asha Rao","skills":["python","sql"]}
56 chars | live: {"name":"Asha Rao","skills":["python","sql"],"years":12} | stable: {"name":"Asha Rao","skills":["python","sql"],"years":12}