Read a streaming Chat Completions response¶
Set stream to true in a Chat Completions request to continuously read generated increments through server-sent events (SSE). This page applies only to Chat Completions; Responses and Anthropic Messages use their own event shapes.
POST https://token.moi.matrixorigin.cn/v1/chat/completions
Before you call¶
Prepare credentials with Genesis access and a model ID that supports Chat Completions. See Chat Completions for request fields and the non-streaming response.
Request example¶
curl -N -X POST "https://token.moi.matrixorigin.cn/v1/chat/completions" \
-H "Authorization: Bearer $GENESIS_ACCESS_TOKEN" \
-H 'Content-Type: application/json' \
-H 'Accept: text/event-stream' \
-d '{
"model": "'"$MODEL_ID"'",
"messages": [{"role": "user", "content": "Explain the purpose of a primary key in one sentence."}],
"stream": true
}'
-N disables curl output buffering so that SSE events can be read line by line.
Successful response¶
The service returns text/event-stream. Parse complete events one by one; append text only from events containing delta.content, and mark the result complete only after receiving data: [DONE].
data: {"object":"chat.completion.chunk","choices":[{"delta":{"content":"A primary key"}}]}
data: {"object":"chat.completion.chunk","choices":[{"delta":{"content":" uniquely identifies each row in a table."}}]}
data: [DONE]
Network chunks can split an SSE event. Retain incomplete state until a complete event and [DONE] are received.
Error response¶
Before the stream is established, errors use ordinary HTTP status codes and a JSON error object. An exception after the stream starts can appear as an event error or connection interruption; treat a connection that ends before [DONE] as interrupted rather than complete.
Next steps¶
See the full Chat Completions request, response, and field descriptions.