# Streaming Real-time streaming responses Enable streaming by setting `stream: true` in your request. ## Request ```json { "model": "myapp-123", "messages": [{"role": "user", "content": "Hello!"}], "stream": true } ``` ## Response Format Server-sent events with JSON chunks: ``` data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1699451234,"model":"myapp-123","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]} data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1699451234,"model":"myapp-123","choices":[{"index":0,"delta":{"content":" there!"},"finish_reason":null}]} data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1699451234,"model":"myapp-123","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]} data: [DONE] ``` ## JavaScript Example ```javascript async function streamChat(model, messages) { const response = await fetch('https://app.chipp.ai/api/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.CHIPP_API_KEY}` }, body: JSON.stringify({ model, messages, stream: true }) }); const reader = response.body.getReader(); const decoder = new TextDecoder(); while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value); const lines = chunk.split('\n'); for (const line of lines) { if (line.startsWith('data: ')) { const data = line.slice(6); if (data === '[DONE]') return; try { const parsed = JSON.parse(data); const content = parsed.choices[0]?.delta?.content || ''; process.stdout.write(content); } catch (e) { // Skip parsing errors } } } } } ``` ## Python Example ```python import requests import json response = requests.post( 'https://app.chipp.ai/api/v1/chat/completions', headers={ 'Content-Type': 'application/json', 'Authorization': f'Bearer {os.environ["CHIPP_API_KEY"]}' }, json={ 'model': 'myapp-123', 'messages': [{'role': 'user', 'content': 'Hello!'}], 'stream': True }, stream=True ) for line in response.iter_lines(): if line: line = line.decode('utf-8') if line.startswith('data: '): data = line[6:] if data == '[DONE]': break try: chunk = json.loads(data) content = chunk['choices'][0]['delta'].get('content', '') print(content, end='', flush=True) except: pass ``` ## OpenAI SDK ```javascript import OpenAI from 'openai'; const openai = new OpenAI({ apiKey: process.env.CHIPP_API_KEY, baseURL: 'https://app.chipp.ai/api/v1' }); const stream = await openai.chat.completions.create({ model: 'myapp-123', messages: [{ role: 'user', content: 'Hello!' }], stream: true }); for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content || ''); } ``` ## Chunk Structure Each chunk contains: - `id`: Same for all chunks in a response - `object`: Always `"chat.completion.chunk"` - `created`: Unix timestamp - `model`: Your app name - `choices[0].delta`: Content increment - `choices[0].finish_reason`: `null` until the last chunk