Skip to main content
Get a free API key to get started.
The Cerebras API supports streaming responses, which send messages back in chunks and display them incrementally as the model generates them. To enable this feature, set the stream parameter to True within the chat.completions.create method. The API then returns an iterable containing the chunks of the message. Similarly, the same can be done in TypeScript by setting the stream property to true within the chat.completions.create method.
1

Initial Setup

Begin by importing the Cerebras SDK and setting up the client.
import os
from cerebras.cloud.sdk import Cerebras

client = Cerebras(
    # This is the default and can be omitted
    api_key=os.environ.get("CEREBRAS_API_KEY"),
)
2

Streaming Responses

Set the stream parameter to True within the chat.completions.create method to enable streaming responses.
stream = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Why is fast inference important?",
        }
    ],
    model="gpt-oss-120b",
    stream=True,
)

for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")