Skip to main content

Overview

Streaming allows you to receive and display tokens as they are generated, providing a more responsive user experience. This is particularly useful for chat applications and interactive interfaces where users expect immediate feedback.

Why Use Streaming?

Better UX

Users see responses immediately, not after full generation

Perceived Speed

Feels faster even if total time is the same

Early Termination

Stop generation early if needed

Real-time Feedback

Perfect for chatbots and assistants

Basic Streaming

Qwen provides the chat_stream method for streaming responses:

CLI Demo Implementation

Here’s the complete streaming implementation from the official CLI demo:
cli_demo.py
The CLI demo uses chat_stream to provide a smooth, real-time chat experience. See cli_demo.py:198 in the source code.

Web Demo with Gradio

Here’s how streaming works in a web interface:
web_demo.py
The web demo uses streaming with yield to update the Gradio interface progressively. See web_demo.py:124 in the source.

Streaming with Custom Display

Create custom display logic for different use cases:

Handling Interruptions

Gracefully handle user interruptions:

Multi-turn Streaming Chat

Maintain conversation history with streaming:

Advanced: Streaming with Callbacks

Implement custom callbacks for token generation:

Performance Considerations

Streaming reduces time-to-first-token but doesn’t necessarily increase overall throughput:
  • Time to First Token: Much faster with streaming
  • Total Generation Time: Similar to non-streaming
  • User Experience: Significantly better with streaming
For remote APIs, streaming may have overhead:
Streaming doesn’t reduce memory usage:

Next Steps

Multi-GPU Inference

Scale streaming across multiple GPUs

Batch Inference

Process multiple requests efficiently

Web Deployment

Build web interfaces with streaming

API Server

Create streaming API endpoints