# Streaming

Receive output as it is generated instead of waiting for a complete response.

## Stream a request

For Chat Completions, Responses, and Messages, set `stream` to `true` when the model supports streaming. [Google's native API](https://mia-ai-proxy-sit.ambr.io/docs/api/google.md#streaming) uses `streamGenerateContent`. This example uses a Chat Completions model:

```bash
curl -N "https://mia-ai-proxy-sit.ambr.io/v1/chat/completions" \
  -H "Authorization: Bearer $MIA_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"model\":\"$MIA_MODEL\",\"stream\":true,\"messages\":[{\"role\":\"user\",\"content\":\"Hello\"}]}"
```

The response uses server-sent events (SSE). An HTTP `200` means the stream started; it does not prove generation succeeded.

## Check completion

| API | Completion signal | What to inspect |
| --- | --- | --- |
| Chat Completions | `data: [DONE]` ends the stream. | Check each choice’s `finish_reason`: `stop` is a natural stop, `length` means the output hit a limit, and `tool_calls` requires tool handling. Check for an `error` object before `[DONE]`; the marker alone does not mean success. |
| Responses | `response.completed`, `response.failed`, or `response.incomplete` | Only `response.completed` means completed. For incomplete output, inspect `response.incomplete_details`. |
| Messages | `message_stop` ends the message. | Read `message_delta.delta.stop_reason`: `end_turn` is a natural stop, `max_tokens` means a limit was reached, and `tool_use` requires tool handling. An `error` event means failure. |
| Google | Each candidate's `finishReason` | Inspect the [completion reason](https://mia-ai-proxy-sit.ambr.io/docs/api/google.md#check-completion) and any error or blocked content. |

Models may return other stop reasons. Handle them according to the selected interface. If the connection closes without a terminal signal, treat the result as incomplete or unknown.

## Handle stream errors

Errors can arrive after output has started. For example, a Messages stream can contain:

```text
event: error
data: {"type":"error","error":{"type":"api_error","message":"Generation could not complete."}}
```

Stop consuming the result as a successful answer when an error arrives. Keep any partial output separate from a completed result.

## Long generations

Use streaming for long generations. A non-streaming request can exceed the response time limit. If the outcome is uncertain, do not automatically repeat a paid request. See [Errors and retries](https://mia-ai-proxy-sit.ambr.io/docs/errors.md).
