This topic describes the Thread class in the assistant API, including how to create, retrieve, modify, and delete threads.
The Assistant API is being deprecated. Migrate to the Responses API as an alternative. The Responses API includes multiple built-in tools and supports multi-turn context management.
Overview: For more information about the features and basic usage of the assistant API, see Assistant API overview.
Retention period: All Thread instances are stored on the Alibaba Cloud Model Studio server and do not expire. You can use the thread ID to retrieve context information.
Response parametersThe response returns a thread object with the following additional fields:
id: The thread ID.
request_id: The request ID.
Sample code
Copy
import jsonfrom dashscope import Threadsimport dashscopeimport osdashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'thread = Threads.create( # We recommend that you set the API key as an environment variable. If not, replace the following line with api_key="sk-xxx" using your Model Studio API key. api_key=os.getenv("DASHSCOPE_API_KEY"), messages=[{"role": "user", "content": "How does AI work? Explain it in simple terms."}])print(json.dumps(thread, default=lambda o: o.__dict__, sort_keys=True, indent=4))
Request parameters
Parameter
Type
Default value
Description
messages
List[Dict]
None
The initial messages of the thread.
metadata
Dict
None
Key-value pairs to associate with the thread.
workspace
str
None
The workspace ID of Alibaba Cloud Model Studio. This parameter is required only when `api_key` is a sub-workspace API key.
api_key
str
None
The API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable.
ResultThe result is a Thread object. The following code shows the object in JSON format:
Response parametersThe response contains the retrieved thread object, which includes the following additional fields:
ID: Thread ID
request_id: The ID of the request.
Sample code
Copy
from dashscope import Threadsimport dashscopeimport osdashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'thread = Threads.retrieve( 'thread_id', # We recommend that you set the API key as an environment variable. If not, replace the following line with api_key="sk-xxx" using your Model Studio API key. api_key=os.getenv("DASHSCOPE_API_KEY"))
Request parameters
Parameter
Type
Default value
Description
thread_id
str
-
The ID of the thread to query.
workspace
str
None
The workspace ID of Alibaba Cloud Model Studio. This parameter is required only when `api_key` is a sub-workspace API key.
api_key
str
None
The API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable.
Response parametersThe output contains the retrieved thread class and additional fields that are not specified in the user-provided parameters:
id: thread_id
request_id: The request ID.
Sample code
Copy
from dashscope import Threadsimport dashscopeimport osdashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'thread = Threads.update( 'thread_id', # We recommend that you set the API key as an environment variable. If not, replace the following line with api_key="sk-xxx" using your Model Studio API key. api_key=os.getenv("DASHSCOPE_API_KEY"), metadata={'key': 'value'})
Request parameters
Parameter
Type
Default value
Description
thread_id
str
-
The ID of the thread to update.
metadata
Dict
None
The information to associate with the thread.
workspace
str
None
The workspace ID of Alibaba Cloud Model Studio. This parameter is required only when `api_key` is a sub-workspace API key.
api_key
str
None
The API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable.
Response parametersThis operation is the same as the create operation.
Response parametersThe status of the thread after deletion.
Sample code
Copy
from dashscope import Threadsimport dashscopeimport osdashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'thread = Threads.delete( 'thread_id', # We recommend that you set the API key as an environment variable. If not, replace the following line with api_key="sk-xxx" using your Model Studio API key. api_key=os.getenv("DASHSCOPE_API_KEY"))
Request parameters
Parameter
Type
Default value
Description
thread_id
str
-
The ID of the thread to delete.
workspace
str
None
The workspace ID of Alibaba Cloud Model Studio. This parameter is required only when `api_key` is a sub-workspace API key.
api_key
str
None
The API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable.