Streaming Tutorial

Get AI-generated content in real time to improve user experience

Live demo

Click the button above to start the streaming demo

Faster perceived response

Users need not wait for the entire response to finish generating

Real-time feedback

Watch content appear progressively

Flexible handling

Interrupt at any time or process partial content

Code Examples

Python implementation

import openai

# Configure API
openai.api_key = "YOUR_API_KEY"
openai.api_base = "https://api.example.com/v1"

# Create a streaming request
response = openai.ChatCompletion.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "Write an article about AI"}
    ],
    stream=True,  # Enable streaming output
    temperature=0.7
)

# Handle streaming response
for chunk in response:
    if chunk.choices[0].delta.get('content'):
        print(chunk.choices[0].delta['content'], end='', flush=True)

JavaScript EventSource

// Use EventSource to handle SSE
const evtSource = new EventSource('/api/stream');

evtSource.onmessage = (event) => {
    const data = JSON.parse(event.data);
    
    if (data.choices[0].delta?.content) {
        // Display content in real time
        document.getElementById('output').innerHTML += data.choices[0].delta.content;
    }
    
    if (data.choices[0].finish_reason === 'stop') {
        evtSource.close();
    }
};

evtSource.onerror = (error) => {
    console.error('Stream error:', error);
    evtSource.close();
};

Fetch API handling

// Use fetch API to handle stream
const response = await fetch('/api/chat', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        model: 'gpt-4o',
        messages: [{ role: 'user', content: prompt }],
        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('
');
    
    for (const line of lines) {
        if (line.startsWith('data: ')) {
            const data = JSON.parse(line.slice(6));
            if (data.choices[0].delta?.content) {
                // Handle streamed content
                handleStreamContent(data.choices[0].delta.content);
            }
        }
    }
}

SSE format example

data: {"id":"chatcmpl-123","choices":[{"delta":{"content":"Hel"}}]}
data: {"id":"chatcmpl-123","choices":[{"delta":{"content":"lo"}}]}
data: {"id":"chatcmpl-123","choices":[{"delta":{"content":"! "}}]}
data: {"id":"chatcmpl-123","choices":[{"finish_reason":"stop"}]}
data: [DONE]