Skip to content

Streaming with SSE

Set stream: true in a Chat Completions request. With cURL, -N disables local output buffering:

Terminal window
curl -N https://wudiapi.com/v1/chat/completions \
-H "Authorization: Bearer $WUDI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"deepseek-v4.1-flash","messages":[{"role":"user","content":"Explain caching in three points."}],"stream":true}'

SSE events are separated by blank lines. Chat Completions text commonly arrives in choices[0].delta.content, followed by an end marker such as data: [DONE]. A network packet can contain only part of an event, or several events. Do not parse each packet as a complete JSON document.

An SDK handles much of this parsing:

import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["WUDI_API_KEY"], base_url="https://wudiapi.com/v1")
stream = client.chat.completions.create(
model="deepseek-v4.1-flash",
messages=[{"role":"user","content":"Explain caching."}], stream=True,
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)

Responses and Messages have their own event types. Use their respective SDKs or parsers rather than looking only for delta.content. Tool arguments can also arrive in fragments: collect the complete function name and argument text before parsing or executing them.

Time to first output differs from total generation duration. Configure reasonable connection and read timeouts for your client. If output stops midway, inspect the error event and usage record before retrying. A disconnected client does not prove that generation or charging stopped upstream.

Limit retries, and do not silently concatenate a restarted request onto partial output. A successful model-list request does not verify streaming. Test a short streamed request with the exact model and protocol you intend to use.