Python, from Zero, for AILesson 55 of 89

Module 6 · Talking to a service: HTTP from Python, done properly

Consuming a streamed reply: server-sent events, line by line

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 7 of 11 in this chapter

This chapter’s goalWrite a client for a paid model API that reuses connections, times out, retries only what is safe to retry, walks pagination with a generator, consumes a streamed response as it arrives, runs many requests concurrently under a rate limit, validates the JSON the model returns before trusting it, and reports the cost of every call from the usage figures in the response.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:28Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

What streaming actually is

Without streaming, a model API generates the whole reply, then sends it. A 500-token answer at 50 tokens per second means ten seconds of nothing, then everything. With streaming, the server starts sending tokens as it produces them, over a single HTTP response that stays open until the generation ends.

The wire format is server-sent events, and it is text:

Text
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"The"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" answer"}}

Each event is a few lines; a blank line ends it. The data: line carries JSON. That is the whole protocol. A course on how a model decides those tokens lives elsewhere; this lesson is about reading them.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Consuming a streamed reply: server-sent events, line by line’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/consuming-a-stream (Accessed: 21 November 2026).