Skip to main content
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

EndpointStreaming
POST /v1/chat/completionsYes
POST /v1/completionsYes
POST /v1/audio/speechYes
POST /v1/audio/transcriptionsYes
See Supported tasks for the full list.

Python SDK

Pass stream=True to receive an iterator of chunks:
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:
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:
{
  "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 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 and Troubleshooting for error details.

Production tips

  • Set reasonable max_tokens to bound latency and cost.
  • Use Always ready 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).