streaming
Stream tokens as they are generated using Server-Sent Events (SSE). Reduces perceived latency for long outputs.
Syntax
openai-api
const stream = await client.chat.completions.create({ ..., stream: true })Example
openai-api
const stream = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Write a poem" }],
stream: true
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}