In real-time chat or long-text generation applications, long wait times degrade user experience and may trigger server-side timeouts, causing tasks to fail. Streaming output addresses these issues by continuously returning fragments of text as the model generates them.
How it works
Streaming output uses the Server-Sent Events (SSE) protocol. After a streaming request starts, the server establishes an HTTP persistent connection with the client. Each time the model generates a text block (called a chunk), it immediately pushes it through this connection. Once all content is generated, the server sends an end signal.
The client listens to the event stream and receives and processes text chunks in real time—for example, rendering characters one by one on the interface. This contrasts with non-streaming calls, which return all content at once.
Streaming and non-streaming responses differ in structure: non-streaming returns a single JSON object with the text in
choices[0].message.content; streaming returns chunks over SSE with the text in choices[0].delta.content, which the client must concatenate, ending with data: [DONE].Billing
Streaming output uses the same billing rule as non-streaming calls, charging based on the number of input tokens and output tokens in the request.
If a request is interrupted, output tokens are counted only for the portion generated before the server received the termination request.
How to use
Step 1: Configure your API key and select a region
You must have obtained an API key and configured it as an environment variable.
Configuring your API key as an environment variable (DASHSCOPE_API_KEY) is more secure than hard coding it in your code.
Step 2: Make a streaming request
- OpenAI compatible
- DashScope
-
How to enable
Set
streamtotrue. -
View token usage
The OpenAI protocol does not return token usage by default. Set
stream_options={"include_usage": true}so the last returned data chunk includes token usage information.
- Python
- Node.js
- curl
Response
Streaming output for multimodal models
Multimodal models support adding images, audio, and other content to conversations. Their streaming output implementation differs from text-only models in the following ways:
- User message construction: Multimodal model inputs include not only text but also images, audio, and other multimodal information.
- DashScope SDK interface: Use the
client.multimodal.chat.createinterface in DashScope Python SDK 2.0. Use the MultiModalConversation class in the DashScope Java SDK.
For multimodal models, see Image and video understanding, Text extraction, Interface interaction, Audio understanding—Qwen3-Omni-Captioner, Kimi, etc. Qwen3.5-Omni and Qwen3-Omni-Flash support only streaming output. Their output can include text or audio and other multimodal content. Their result parsing differs from other models. For details, see Omni-modal.
- OpenAI compatible
- DashScope
Python
Streaming output for thinking models
Thinking models first return reasoning_content (the thought process), then return content (the response). Determine whether the current stage is thinking or responding based on the data packet status.
For details on thinking models, see Deep thinking and Image and video understanding.
For streaming output implementation of Qwen3-Omni-Flash (thinking mode), see Omni-modal.
- OpenAI compatible
- DashScope
Below is the response format when calling the thinking mode of the qwen-plus model using the OpenAI Python SDK in streaming mode:
- If
reasoning_contentis not None andcontentisNone, the current stage is thinking. - If
reasoning_contentis None andcontentis notNone, the current stage is responding. - If both are
None, the stage remains the same as the previous packet.
- Python
- Node.js
- HTTP
Example code
Response
Going live
- Performance and resource management: In backend services, maintaining an HTTP persistent connection for each streaming request consumes resources. Configure your service with appropriate connection pool size and timeout values. In high concurrency scenarios, monitor file descriptor usage to prevent exhaustion.
-
Client-side rendering: On web frontends, use the
ReadableStreamandTextDecoderStreamAPIs to smoothly handle and render SSE event streams, delivering the best user experience. -
Model monitoring:
- Key metrics: Monitor Time to First Token (TTFT), the core metric for streaming experience. Also monitor API error rate and average response time.
- Alerting: Set alerts for abnormal API error rates, especially 4xx and 5xx errors.
-
Nginx proxy configuration: If using Nginx as a reverse proxy, its default output buffering (proxy_buffering) breaks the real-time nature of streaming responses. To ensure data is pushed to clients immediately, disable this feature by setting
proxy_buffering offin your Nginx configuration file.
Error codes
If the model call fails and returns an error message, see Error codes for resolution.
FAQ
Q: Why is there no usage information in the response?
A: The OpenAI protocol does not return usage information by default. Set the stream_options parameter to include usage information in the final returned packet.
Q: Does enabling streaming output affect the model's response quality?
A: No. However, some models support only streaming output, and non-streaming calls might cause timeout errors. We recommend using streaming output.
Q: What is the difference between non-streaming and streaming calls?
A: Key differences:
- Timeout limit: For non-streaming calls, the maximum timeout is at least 300 seconds and varies by region and model. If not completed in time, the request is terminated.
- Output structure: Non-streaming calls return the complete response (a single JSON object) at once. Streaming calls return data chunks progressively via the SSE protocol, with each chunk containing part of the generated content. The client must assemble these chunks.
- Feature compatibility: Both support features like JSON Mode and Function Call with no functional differences.
Q: Does streaming output support JSON Mode (structured output)?
A: Yes. Set stream to true and response_format to {"type": "json_object"} in the request. The model will return JSON-formatted content fragments progressively. The final assembled output will be valid JSON.