This topic describes how to use the Message class in the assistant API to create, list, retrieve, and modify messages.
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.
Response parametersThe message class is returned. In addition to the request parameters, the response contains the following fields:
id: the message ID.
request_id: the request ID.
Code example
Copy
from dashscope import Messagesimport dashscopeimport osdashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'msg = Messages.create( 'the_thread_id', # Set the API key using an environment variable. If an environment variable is not set, replace the following line with api_key="sk-xxx" and your Model Studio API key. api_key=os.getenv("DASHSCOPE_API_KEY"), content='The message content.', role='user', metadata={'key': 'value'})
Request parameters
Parameter
Type
Default
Description
thread_id
string
-
The thread ID.
content
string
-
The message content.
role
str
'user'
The role of the message. Default value: user.
metadata
Dict
None
The key-value information associated with the message.
workspace
string
None
The Workspace ID of Alibaba Cloud Model Studio. This parameter is required only when the `api_key` is an API key for a sub-workspace.
api_key
string
None
The API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable.
ResponseThe result is a Message object. The following code shows an example of the JSON-formatted content:
The HTTP status code of the call. A value of 200 indicates that the call is successful. Other values indicate that the call failed.
id
string
The message ID, which is a UUID string.
content
List[dict]
The message content.
content.type
string
The content type, such as text.
content.text
dict
Content
content.text.value
string
text value of the content
metadata
Dict
The key-value information associated with this message.
tool_calls
Dict
The tool call information.
plugin_call
Dict
The plugin call information.
created_at
timestamp
The time when the assistant was created.
gmt_created
datetime
2024-03-22 17:12:31
gmt_modified
datetime
2024-03-22 17:12:31
code
string
Indicates that the request failed. This parameter specifies the error code. This parameter is ignored if the request is successful.
Python only.
message
string
Indicates that the request failed. This parameter provides detailed information about the failure. This parameter is ignored if the request is successful.
Response parametersA list of message classes is returned. The response also includes the original request parameters and the following additional fields:
A list of multiple messages.
Code example
Copy
from dashscope import Messagesimport dashscopeimport osdashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'messages = Messages.list( 'thread_id', # Set the API key using an environment variable. If an environment variable is not set, replace the following line with api_key="sk-xxx" and your Model Studio API key. api_key=os.getenv("DASHSCOPE_API_KEY"), limit=1, order='desc')
Request parameters
Parameter
Type
Default
Description
thread_id
string
-
The ID of the thread to query.
limit
int
None
The number of messages to retrieve.
order
string
None
The sorting order by `created_at`.
workspace
string
None
The Alibaba Cloud Model Studio Workspace ID, which is required only when the `api_key` is an API key for a sub-workspace.
api_key
string
None
The API key for Alibaba Cloud Model Studio. We recommend that you set the API key as an environment variable.
Response parametersThe retrieved message class is returned. In addition to the request parameters, the response contains the following fields:
id: Message ID
request_id: the request ID.
Code example
Copy
from dashscope import Messagesimport dashscopeimport osdashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'message = Messages.retrieve( 'message_id', # Set the API key using an environment variable. If an environment variable is not set, replace the following line with api_key="sk-xxx" and your Model Studio API key. api_key=os.getenv("DASHSCOPE_API_KEY"), thread_id='thread_id')
Request parameters
Parameter
Type
Default
Description
message_id
string
-
The ID of the message to query.
thread_id
string
-
The ID of the thread to which the message belongs.
workspace
string
None
The Workspace ID of Alibaba Cloud Model Studio. This parameter is required only when the api_key is a sub-workspace API Key.
api_key
string
None
The API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable.
Response parametersSee the response for the create operation.
Output ParametersThe modified message class is returned. In addition to the request parameters, the response contains the following fields:
id: Message ID
request_id: the request ID.
Code example
Copy
from dashscope import Messagesimport dashscopeimport osdashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'thread = Messages.update( 'message_id', # Set the API key using an environment variable. If an environment variable is not set, replace the following line with api_key="sk-xxx" and your Model Studio API key. api_key=os.getenv("DASHSCOPE_API_KEY"), thread_id='the_message_thread_id', metadata={'key': 'value'})
Request parameters
Parameter
Type
Default
Description
message_id
string
-
The ID of the message to update.
thread_id
string
-
The ID of the thread to which the message to be updated belongs.
metadata
Dict
None
The information associated with the thread.
workspace
string
None
The Workspace ID of Alibaba Cloud Model Studio, which is required only when the api_key is a sub-workspace API Key.
api_key
string
None
The API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable.
Response parametersSee the response for the create operation.