Streaming shows text as the model produces it. With client and model_id from the quickstart:
stream = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": "Explain databases in two sentences."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.usage:
print("\nUsage:", chunk.usage)
print()Stream with cURL
After setting the quickstart environment variables, use -N to avoid buffering:
curl -N "$SINGLEAPI_BASE_URL/chat/completions" \
-H "Authorization: Bearer $SINGLEAPI_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"model\":\"$SINGLEAPI_MODEL_ID\",\"messages\":[{\"role\":\"user\",\"content\":\"Explain databases briefly.\"}],\"max_tokens\":100,\"stream\":true,\"stream_options\":{\"include_usage\":true}}"Events and failures
Each event starts with data:; [DONE] marks the end. A final usage event can have an empty choices array. A failure after streaming starts can arrive as an error event even though the HTTP response has already started. Keep the request ID and check its outcome before retrying: another call can incur another charge.
The SDK yields chunks rather than one final response. If valid provider usage is available, include_usage adds it in a final chunk. A stream can also contain function-call arguments in pieces; see Use a function tool. For the event format, errors and limits, see the streaming API reference.