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

# 02 streaming

# Streaming Responses

Watch tokens arrive in real-time using Server-Sent Events (SSE).

## Enable Streaming

Set `"stream": true` in your request:

```json theme={null}
{
  "messages": [{"role": "user", "content": "Count to 5"}],
  "stream": true
}
```

## Response Format (SSE)

Instead of a single JSON response, you receive a stream of events:

```
data: {"choices":[{"delta":{"role":"assistant"}}]}

data: {"choices":[{"delta":{"content":"1"}}]}

data: {"choices":[{"delta":{"content":"."}}]}

data: {"choices":[{"delta":{"content":" One"}}]}

data: [DONE]
```

### Key Points

* Each line starts with `data: `
* Content arrives in `delta.content` fragments
* Stream ends with `data: [DONE]`
* Empty lines separate events

## Parsing Streaming Responses

```python theme={null}
import json
import httpx

def stream_chat(message: str):
    with httpx.Client(timeout=120.0) as client:
        with client.stream(
            "POST",
            "https://hetty.cfosilvia.com/v1/chat/completions",
            json={"messages": [{"role": "user", "content": message}], "stream": True}
        ) as response:
            for chunk in response.iter_text():
                for line in chunk.split("\n"):
                    if not line.startswith("data: "):
                        continue
                    
                    data = line[6:]  # Remove "data: " prefix
                    if data == "[DONE]":
                        return
                    
                    parsed = json.loads(data)
                    content = parsed["choices"][0].get("delta", {}).get("content", "")
                    if content:
                        print(content, end="", flush=True)
```

## Accumulating Full Response

If you need the complete text:

```python theme={null}
content_parts = []
for chunk in response.iter_text():
    # ... parse as above ...
    if content:
        content_parts.append(content)

full_response = "".join(content_parts)
```

## Streaming Response Schema

```json theme={null}
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion.chunk",
  "choices": [{
    "index": 0,
    "delta": {
      "content": "Hello"      // Only present fields are included
    },
    "finish_reason": null     // "stop" on last chunk
  }]
}
```

## See Also

* [examples/02\_streaming.py](../examples/02_streaming.py) - Runnable example
* [Streaming with Tools](04-streaming-tools.md) - Tool calls in streams


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.