Streaming
Overview
Basic Usage
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.tensorx.ai/v1"
)
stream = client.chat.completions.create(
model="deepseek/deepseek-chat-v3.1",
messages=[{"role": "user", "content": "Write a haiku about coding"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'YOUR_API_KEY',
baseURL: 'https://api.tensorx.ai/v1'
});
const stream = await client.chat.completions.create({
model: 'deepseek/deepseek-chat-v3.1',
messages: [{ role: 'user', content: 'Write a haiku about coding' }],
stream: true
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
}Getting Token Usage
Server-Sent Events Format
Raw HTTP Streaming
Accumulating the Full Response
When to Use Streaming
✅ Use Streaming For
Use Case
Why
❌ Don't Use Streaming For
Use Case
Why
Handling Finish Reasons
Error Handling
Streaming with Function Calling
See Also
Last updated
Was this helpful?

