Streaming
Reading the stream
Keep the connection open and parse events separated by a blank line. Stop at [DONE].
JavaScript
const res = await fetch(http://167.254.242.38:3010/v1/chat/completions", {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${key}` },
body: JSON.stringify({ model: "gpt-5.2", stream: true, messages: [{ role: "user", content: "hi" }] })
});
const reader = res.body.getReader();
const dec = new TextDecoder();
let buf = "";
for (;;) {
const { value, done } = await reader.read(); if (done) break;
buf += dec.decode(value, { stream: true });
let i; while ((i = buf.indexOf("\n\n")) >= 0) {
const line = buf.slice(0, i).trim(); buf = buf.slice(i + 2);
if (line.startsWith("data: ") && line !== "data: [DONE]") console.log(JSON.parse(line.slice(6)));
}
}
Python
import json, requests
r = requests.post(http://167.254.242.38:3010/v1/chat/completions",
headers={"Authorization": f"Bearer {key}"},
json={"model": "gpt-5.2", "stream": True, "messages": [{"role": "user", "content": "hi"}]},
stream=True)
for line in r.iter_lines():
if line and line.startswith(b"data: ") and line != b"data: [DONE]":
print(json.loads(line[6:])["choices"][0]["delta"].get("content", ""), end="")