generate_content_stream — same args, iterator return
The streaming methods sit alongside the non-streaming ones and take the same arguments. The only difference is the return value: instead of a single GenerateContentResponse, you get an iterator (sync) or async iterator (async) of partial responses.
Three patterns to remember
- Sync stream:
for chunk in client.models.generate_content_stream(...) - Async stream:
async for chunk in await client.aio.models.generate_content_stream(...)(note theawaiton the call itself before theasync for) - Streaming chat:
chat.send_message_stream(...)/chat.aio.send_message_stream(...)
Concatenate as you go, grab usage at the end
Each chunk's chunk.text is the partial text for that slice. Concatenate them into the full reply. usage_metadata appears only on the final chunk — capture it lazily as you iterate.
Multimodal streams the same way
You can pass an image, an uploaded file, or any of the same multi-part contents you'd use in non-streaming. The stream begins as soon as the model produces its first text token.