Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 16.5 — Streaming Responses with Generators and Async
Python · Example 1 of 2

A generator stream, collected and cancelled

Notice that "[stream closed]" prints in both cases: the finally block runs when the stream ends and when you close it early.

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.

01

Write collect(stream) that returns the joined text and the number of chunks.

02

Write stream_until(stream, stop_word) that joins chunks until one contains the stop word, then closes the stream.

03

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

01

Why do chat products stream, and does it make the answer arrive sooner overall?

02

How does a generator model a stream, and how do you stop it early and clean up?

03

Why are real streams async, and how do you cancel one with a timeout?

04

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?

A generator stream, collected and cancelled
Sign in to run this example in your browser.

Expected output

Streaming shows the answer while it is still being written. 
[stream closed]
final text: Streaming shows the answer while it is still being written.
chunks: 10
--- cancel after 3 chunks ---
Streaming shows the 
[stream closed]