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

# Streaming

> Stream chat and completion responses with Server-Sent Events (SSE)

Several SynapsAI Cloud endpoints support streaming so you can display partial output as the model generates it. Streaming uses **Server-Sent Events (SSE)** over HTTP, matching the OpenAI streaming format.

## Supported endpoints

| Endpoint                        | Streaming |
| ------------------------------- | --------- |
| `POST /v1/chat/completions`     | Yes       |
| `POST /v1/completions`          | Yes       |
| `POST /v1/audio/speech`         | Yes       |
| `POST /v1/audio/transcriptions` | Yes       |

See [Supported tasks](/resources/tasks) for the full list.

## Python SDK

Pass `stream=True` to receive an iterator of chunks:

```python theme={null}
from synapsai import SynapsAI

client = SynapsAI()

stream = client.chat.completions.create(
    model="your-model-id",
    messages=[{"role": "user", "content": "Write a haiku about GPUs."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta
    if delta.content:
        print(delta.content, end="", flush=True)
```

## cURL

Set `"stream": true` in the request body:

```bash theme={null}
curl -N https://api.synapsai.cloud/v1/chat/completions \
  -H "Authorization: Bearer $SYNAPSAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-model-id",
    "messages": [{"role": "user", "content": "Hello"}],
    "stream": true
  }'
```

The `-N` flag disables buffering so chunks arrive as they are sent.

## Response format

Each SSE line is a `data:` event containing a JSON object. A typical chunk looks like:

```json theme={null}
{
  "id": "chatcmpl-...",
  "object": "chat.completion.chunk",
  "choices": [
    {
      "index": 0,
      "delta": { "content": "Hello" },
      "finish_reason": null
    }
  ]
}
```

The stream ends with:

```
data: [DONE]
```

## Tool calling with streaming

When using tools, streamed chunks may include `delta.tool_calls` with partial function arguments. Accumulate tool call fragments across chunks before invoking functions.

See [Tool calling](/examples/tool-calling) for a complete example.

## Error handling

If an error occurs mid-stream, the connection may close with a non-200 status before `[DONE]`. Wrap streaming loops in try/except and implement retry logic with exponential backoff for transient `429` or `500` responses.

See [API overview](/api-reference/introduction) and [Troubleshooting](/support/troubleshooting) for error details.

## Production tips

* Set reasonable `max_tokens` to bound latency and cost.
* Use [Always ready](/core-concepts) or **Super fast** readiness for user-facing streaming to avoid first-token delay from cold starts.
* Implement client-side timeouts and cancellation (close the HTTP connection to stop generation when the user navigates away).
