> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mibyanai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming responses

> Read Chat Completions and Responses as server-sent events

Set `stream` to `true` to receive incremental output. Clients should handle a complete stream and a terminal event.

## Chat Completions stream

```bash theme={null}
curl "$MIBYAN_BASE_URL/chat/completions" \
  -H "Authorization: Bearer $MIBYAN_API_KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "model": "mibyan-4.1",
    "stream": true,
    "messages": [{"role": "user", "content": "اكتب ثلاث أفكار لحملة سياحية."}]
  }'
```

Each data frame is JSON with the Chat Completions chunk shape. The stream terminates with:

```text theme={null}
data: [DONE]
```

Accumulate `choices[0].delta.content` and stop when the `[DONE]` marker arrives. Usage can be returned in the final usage chunk when enabled by the model configuration.

## Responses stream

`POST /v1/responses` uses named events:

* `response.output_text.delta` contains a text fragment in `delta`.
* `response.completed` marks successful completion and includes the public model and usage summary.

Unknown events should be ignored so your client remains forward-compatible.

## Cancellation and retries

* Abort the HTTP request when the user stops generation.
* Do not retry a stream after partial output unless your application can safely start a new request.
* Retry a connection failure or `502`/`503` with exponential backoff and jitter.
* Do not retry `400`, `401`, `403`, or `404` without changing the request or credentials.
* Treat `429` as a policy response and wait using the reset headers when present.

## Streaming safety

Treat all streamed text as untrusted model output and render it safely in your application.
