Sub topic 5
Streaming replies by yielding partial text
This chapter assumes you can write Python generators, have a working gr.ChatInterface app, and know the (message, history) function contract from earlier chapters. You will turn a function that returns one string into one that streams its reply.
Why stream
A language model can take several seconds to finish a long answer. If your function uses return, the user sees nothing until the whole reply exists. Streaming shows text as it is produced, so the chat feels responsive even when total time is unchanged.
Gradio supports this with ordinary Python generators. If your chat function contains yield, Gradio treats each yielded value as an update to the same assistant message.
The key rule: yield the full text so far
Each yield replaces the displayed reply. It does not append to it. You must yield the accumulated text, not only the newest piece.
import time
import gradio as gr
def respond(message, history):
reply = ""
for word in f"You said: {message}".split():
reply += word + " "
time.sleep(0.1)
yield reply
gr.ChatInterface(fn=respond, type="messages").launch()Walk through the loop for the message hi there:
replystarts as an empty string.- After the first word,
replyis"You ", and that is yielded. - After the second,
replyis"You said: ", and that replaces the previous text. - This continues until the last word. The final yield is what stays in the chat history.
The time.sleep(0.1) only makes the effect visible. Remove it in real code.
Streaming from a model
Real model clients return an iterator of chunks when you ask for streaming. Each chunk holds a small piece of text, called a delta. You append each delta to a running string and yield that string. The pattern from the LLM chapter looks like this:
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
stream=True,
)
reply = ""
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
reply += delta
yield replyThe or "" matters. Some chunks, such as the first and last, carry no text, and delta.content is None for them. Without the fallback, reply += None raises a TypeError.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Reply shows only the last word or chunk | You yielded `delta` instead of `reply` | Yield the accumulated string |
| Nothing appears until the end | The function uses `return`, or the client call lacks `stream=True` | Use `yield` and enable streaming on the client |
| `TypeError` on `+=` | A chunk had `content=None` | Use `delta or ""` |
| Reply appears twice | You both yielded the text and returned it | Use only `yield`; a bare `return` at the end is fine |
<br>
Stopping and slow functions
While a reply streams, the interface shows a stop button. Pressing it ends the generator, and Gradio keeps the text yielded so far. Because generators are paused at each yield, any cleanup code you need should go in a try/finally block.
Exercise
Your streaming chat function shows only the most recent piece of text, and each new piece erases the previous one. What is wrong, and how do you fix it?
Show answer
The function yields each new delta by itself. Each yield replaces the whole displayed reply, so you must keep a running string, add each delta to it (reply += delta), and yield reply.