Streaming output lets you retrieve the real-time running status of an assistant. This lets you display the content generated by the Large Language Model (LLM) to users word by word.
Streaming output has the following features:
To enable streaming output, run the assistant using
In the Assistant API, rendering streaming output involves two steps:
To run the assistant in stream mode, set the
In a stream run, the assistant produces two main types of data streams:
In the Assistant API, the assistant produces two types of conversation message streams:
Code interpreter, Quark search, text-to-image, and calculator support streaming output. Other Assistant API tools do not support streaming output. For more information, see Run step delta object.
You have now learned the basic method for enabling streaming output. The following example demonstrates how to build a streaming output with tool calls.
In this simple example, the assistant is configured with the code interpreter tool. This enables streaming output for text messages, code generation, and code execution. For more information about how to configure the code interpreter, see Code interpreter - Features.
Make sure you have configured the API key as an environment variable and installed the DashScope SDK.
If you encounter code execution errors while configuring streaming output, see Error messages to troubleshoot the error.
- More natural conversation flow: It simulates human communication patterns for a smoother and more natural interaction.
- Finer control: You can monitor the assistant's running status in real time to display the generation progress or stop the process if an error occurs.
- More reliable connection: The response is returned in chunks. This prevents long wait times on the client side.
Getting started
To enable streaming output, run the assistant using Run.create(stream=True).
Make sure you have configured the API key as an environment variable and installed the DashScope SDK.
| Non-streaming output | Streaming output |
Configuration method
In the Assistant API, rendering streaming output involves two steps:
- Run the assistant in stream mode.
- Receive the stream data.
Run the assistant in stream mode
To run the assistant in stream mode, set the stream parameter to True when you create a run.
Receive the stream data
In a stream run, the assistant produces two main types of data streams:
- Status information stream: Tracks the run status and progress.
- Conversation message stream: Contains the actual output content.
Conversation message stream
In the Assistant API, the assistant produces two types of conversation message streams:
- Text message stream: The stream of text messages generated by the LLM in the assistant.
- Tool message stream: The stream of tool messages returned from tool calls in the assistant. The following example uses the code interpreter: