Command Palette

Search for a command to run...

Hectal
Phase 5: Streaming and UX
LLM Engineering · Exercise 4 of 5 PreviousNext
Intermediate

Stop reading and close the stream

Write read_until_stop(stream, should_stop). stream is a generator of text pieces. Read pieces and join them into text. After adding each piece, call should_stop(text); when it returns True, stop reading.

Always close the generator before returning (so its cleanup code runs), whether you stopped early or read everything. Return the text.

What Check tests

  1. Reads everything when it never stops
  2. Stops as soon as should_stop is true
  3. The generator's cleanup runs after a stop
  4. The cleanup also runs when the stream finishes

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.