Streaming Output
Streaming allows the model to return content word by word, enhancing user experience. Enable streaming by setting stream: true.
Basic Information
- API URL:
https://fast-api.org/v1/chat/completions - Authentication: Bearer Token (API Key)
- Request Method: POST
Request Parameters
| Parameter | Type | Description |
|---|---|---|
| model | string | Model name |
| stream | boolean | Set to true to enable streaming |
| messages | array | List of conversation messages |
Python Streaming Request Example
python
import json
import requests
url = "https://fast-api.org/v1/chat/completions"
headers = {
"Authorization": "Bearer sk-your API Key",
"Content-Type": "application/json"
}
payload = {
"model": "gpt-4o-mini",
"stream": True,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Please write a poem about spring."}
]
}
response = requests.post(url, headers=headers, json=payload, stream=True)
buffer = ""
for chunk in response.iter_content(chunk_size=None):
if chunk:
buffer += chunk.decode("utf-8")
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
if not line.strip():
continue
if line.startswith("data: "):
data_line = line[6:].strip()
if data_line == "[DONE]":
break
try:
data = json.loads(data_line)
content = data["choices"][0]["delta"].get("content", "")
print(content, end="", flush=True)
except json.JSONDecodeError:
buffer = line + "\n" + buffer
breakNode.js Streaming Request Example
javascript
const https = require('https');
const options = {
hostname: 'fast-api.org',
path: '/v1/chat/completions',
method: 'POST',
headers: {
'Authorization': 'Bearer sk-your API Key',
'Content-Type': 'application/json'
}
};
const req = https.request(options, (res) => {
let buffer = '';
res.on('data', (chunk) => {
buffer += chunk.toString();
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
if (line.startsWith('data: ') && line.trim() !== 'data: [DONE]') {
const data = JSON.parse(line.slice(6));
const content = data.choices?.[0]?.delta?.content || '';
process.stdout.write(content);
}
}
});
});
req.write(JSON.stringify({
model: 'gpt-4o-mini',
stream: true,
messages: [{ role: 'user', content: 'Hello!' }]
}));
req.end();Notes
- During streaming, the server returns data in chunks using SSE (Server-Sent Events) format
- Each line of data starts with
data:and ends withdata: [DONE] - Please keep your API Key safe
- It is recommended to use the HTTPS protocol