Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

Handling streamed responses

Understand the idea

A streamed model response arrives as pieces. Waiting for one JSON object will not parse it.

The idea

The stand-in writes a : two data: lines and a done line. readDeltas reads the body with a reader, splits complete lines, and keeps a remainder so a chunk boundary cannot cut a JSON object in half. It skips blank lines and [DONE]. Each content delta is pushed, then joined. The client of this route sees the parts and the summary, which is how you can tell the route did not pretend the stream was one object. response.json is the wrong tool for this body.

What changes

Line, action, and result
LineActionContributes
data with SumParse the deltaSum
data with maryParse the deltamary
data [DONE]SkipNothing
A partial line at the end of a chunkKeep it in pendingNothing until the rest arrives

The command

Join the deltas

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

Status 200, parts Sum and mary, summary Summary. The join is the string the client was promised.

Guided challenge

Join every delta into the summary

The route currently sets summary to only parts[0]. Change it so summary is all deltas joined into one string.

Join every delta into the summary

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

status 200
"summary":"Summary"

Hints 0/2

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does a data line carry here?
    02What does the done marker mean?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson