An async stream with a timeout
Change the timeout to 1.35 and run again to see more chunks arrive.
Streaming sends a model's answer to you in small pieces as it is generated, so the user sees text appear instead of waiting for the whole reply. In Python this is a generator (or an async generator) that yields chunks, which you print, forward, collect and can cancel.
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 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write collect(stream) that returns the joined text and the number of chunks.
Write stream_until(stream, stop_word) that joins chunks until one contains the stop word, then closes the stream.
Write an async function first_n(agen, n) that returns the first n items of an async generator and closes it.
Explain it without notes
Why do chat products stream, and does it make the answer arrive sooner overall?
How does a generator model a stream, and how do you stop it early and clean up?
Why are real streams async, and how do you cancel one with a timeout?
The user closes the browser tab halfway through a streamed answer. What should happen in your Python service, and what goes wrong if it does not?
Expected output
got: one
got: two
[stream closed]
timed out after 2 chunks: one two