LiteLLMLiteLLM 1.101 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
20 small wins to finish your pathNext lesson

Streaming: the answer a piece at a time

stream=True returns the answer in chunks as it is produced, in the same chunk format for every provider, so one loop shows a reply appearing word by word.

Example
from litellm import completion

chunks = completion(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Apologise for a late parcel"}],
    mock_response="Sorry your parcel is late, we are on it.",
    stream=True,
)
for chunk in chunks:
    print(repr(chunk.choices[0].delta.content))

Each chunk carries a delta: the new piece of text, not the whole answer so far. The last chunk has None content, and its finish_reason says the answer is complete. The SDK docs note mock_response streams too, which is why no key is needed here; the pieces are cut by LiteLLM, where a real model would send tokens.

Putting the answer together

Example
parts = []
for chunk in completion(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Apologise for a late parcel"}],
    mock_response="Sorry your parcel is late, we are on it.",
    stream=True,
):
    parts.append(chunk.choices[0].delta.content or "")

print("".join(parts))

or "" turns the final None into an empty string, so joining works. A chat interface prints each piece as it arrives instead of collecting them.

A provider class streams only if it implements the streaming method; the ShopLLM from lesson 4 does not, which is why this lesson uses mock_response.

Try it yourself
  • Print chunk.choices[0].finish_reason for every chunk.
  • Use acompletion with stream=True and async for.
  • Count the chunks for a longer mock_response.

Every expert started right here.