Streaming
Consume incremental text and handle failures after a stream begins.
Enable server-sent events
Set stream: true on Chat Completions, Messages, or Responses. The response uses text/event-stream. Use a streaming-aware SDK or SSE parser; network chunks do not necessarily align with events. For cURL, add -N to disable output buffering. Image generation and SERP do not stream.
Stream with the OpenAI SDK
Accumulate content deltas and handle errors around the entire iteration. Tool call arguments can arrive in fragments; assemble them before parsing JSON or executing a function.
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.17.wtf/v1", api_key=os.environ["LM0_API_KEY"])
stream = client.chat.completions.create(
model="MODEL_ID",
messages=[{"role": "user", "content": "Explain SSE briefly."}],
max_tokens=512,
stream=True,
)
try:
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
finally:
stream.close() Event formats
| Surface | What to consume |
|---|---|
| Chat Completions | data events with chat.completion.chunk objects; content in choices[].delta; terminal [DONE] |
| Messages | messagestart, contentblockstart, contentblockdelta, contentblockstop, messagedelta, message_stop |
| Responses | Typed response events, including response.output_text.delta and response.completed |
Keep reasoning, text, and tool-call deltas separate. Usage can arrive near completion; it is not present on every chunk.
Handle partial output
A request can fail before headers, returning a normal JSON error. Once streaming begins, HTTP status can already be 200; inspect error events and unexpected disconnects. Treat incomplete output as partial. Close streams when users cancel, and avoid blindly replaying requests that may already have produced billable output.