Streaming Responses

Updated · 5 min read
mediumPython 3.13aiapipythonstreamingux

Streaming means receiving a model's reply in small pieces while it is still being written, instead of waiting for the whole reply. It is used in chat interfaces so people see words appear within a moment, and for long replies that would otherwise take a long time or hit a request timeout.

Waiting vs Streaming

Same total time, much sooner first words
Without streamingWith streaming
First words appearWhen the whole reply is doneWithin about a second
Total timeAbout the sameAbout the same
Best forShort replies, background jobsChat screens, long replies

Syntax

MethodDescription
client.stream(...)It is used to start a streamed request (the helper sends "stream": true)
for text in stream:It is used to loop over the text pieces as they arrive
stream.replyIt is used to get the complete reply, with finish_reason and token counts, after the loop

Example

Python
from model_api import ModelClient
 
client = ModelClient()
 
stream = client.stream(
    [{"role": "user", "content": "Give me three tips for the admission interview."}],
    system="You are Campus Helper, a school assistant. Be brief and practical.",
    max_tokens=1000,
)
for text in stream:
    print(text, end="", flush=True)  # show each piece immediately
 
print()
print("Finish reason:", stream.reply.finish_reason, "| tokens out:", stream.reply.output_tokens)

Example output. Produced by an AI model on November 16, 2026, to show the shape; not captured from a live call. On screen, the three tips appear a few words at a time.

Output
Here are three tips:
1. Re-read your application so your answers match it.
2. Prepare two questions to ask about the school.
3. Arrive ten minutes early with any documents they asked for.
Finish reason: stop | tokens out: 48

Explanation: client.stream() prepares the request, and looping over it opens the connection and yields each new piece of text as soon as it arrives. The server sends the pieces as lines that start with data:, which the helper reads one by one. end="" keeps the pieces on the same line, and flush=True prints them immediately. After the loop, stream.reply holds the complete reply, just like a normal call, so you can still check the finish reason and token usage. (Some providers send token counts in streams only when asked; the helper asks for them.)

Streaming in Background Jobs

Streaming isn't only for chat screens.

Common Mistakes

Forgetting flush=True. The pieces may sit in a buffer and appear all at once.

Skipping the finished reply. You still need finish_reason to know if the reply was cut off.

Showing partial replies as final. If the stream stops early (a network error), mark the answer as incomplete.

Interview Corner

Why stream model responses?

So people see the first words within a moment instead of waiting for the whole reply, and so long replies don't hit request timeouts. The total time and cost stay about the same.

How do you get the finish reason when streaming?

Read it from the last pieces of the stream: the final chunk carries finish_reason, and the token counts come at the end. The course helper collects them into stream.reply after the loop.

Practice

Exercise: stream a longer answer. Ask Campus Helper for a one-week study plan before exams and stream it. Then add a counter that prints how many text pieces arrived.

Check your understanding:

  1. What does streaming change for the person reading?
  2. Which method gives the complete reply at the end?
  3. Does streaming make a reply cheaper?

Quick check: streaming

4 questions
1What is streaming?
2What does looping over the stream give you?
3How does streaming affect cost?
4How do you find out if a streamed reply was cut off?

Key Takeaways

Quick reference

NeedCode
Startstream = client.stream(messages, ...)
Piecesfor text in stream:
Print nowprint(text, end="", flush=True)
Finished replystream.reply

Was this tutorial helpful?

Lesson 85 of 186