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

# Streaming Responses

> Real-time streaming for chat completions

## Overview

Streaming allows you to receive responses in real-time as they're generated, instead of waiting for the complete response.

## Enable Streaming

Set `stream: true` in your request:

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="YOUR_API_KEY",
      base_url="https://api.redpill.ai/v1"
  )

  stream = client.chat.completions.create(
      model="openai/gpt-5",
      messages=[{"role": "user", "content": "Write a long story"}],
      stream=True
  )

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

  ```javascript JavaScript theme={null}
  import OpenAI from 'openai';

  const stream = await client.chat.completions.create({
    model: 'openai/gpt-5',
    messages: [{ role: 'user', content: 'Write a long story' }],
    stream: true
  });

  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta?.content || '');
  }
  ```

  ```bash cURL theme={null}
  curl https://api.redpill.ai/v1/chat/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-5",
      "messages": [{"role": "user", "content": "Write a story"}],
      "stream": true
    }'
  ```
</CodeGroup>

## Stream Format

Responses are sent as Server-Sent Events (SSE):

```
data: {"id":"chatcmpl-123","choices":[{"delta":{"content":"Hello"}}]}

data: {"id":"chatcmpl-123","choices":[{"delta":{"content":" world"}}]}

data: [DONE]
```

## Error Handling

```python theme={null}
try:
    stream = client.chat.completions.create(
        model="openai/gpt-5",
        messages=[...],
        stream=True
    )
    for chunk in stream:
        print(chunk.choices[0].delta.content or "", end="")
except Exception as e:
    print(f"Stream error: {e}")
```

## Use Cases

* **Chatbots**: Display responses as they're typed
* **Code generation**: Show code as it's written
* **Long-form content**: Stream articles/essays
* **Better UX**: Reduce perceived latency

<Info>
  All chat models support streaming, including GPU TEE confidential models.
</Info>
