Skip to content

Messages (Deprecated)

This topic describes how to use the Message class in the assistant API to create, list, retrieve, and modify messages. Important

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.

Function nameType
createCreate a message class
retrieveRetrieve a message class
modifyModify a message class
listList message classes

Create a message

HTTP

Code example

HELPCODEESCAPE-curl
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
    "role": "user",
    "content": "Who are you",
    "metadata": {}
}'

Request parameters

Parameter nameInput ParametersParameter typeRequired
thread idThe thread ID to which the message is passed.stringYes
contentThe content of the message.stringYes
roleThe role of the entity that provides the message. Only `user` is supported.stringNo
metadataOther related information.stringNo

Response

HELPCODEESCAPE-json
{
    "id": "message_f1933671-19e1-4162-ad25-7326165123e1",
    "object": "thread.message",
    "created_at": 1711508433283,
    "thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
    "incomplete_details": {},
    "completed_at": null,
    "incomplete_at": null,
    "assistant_id": "",
    "run_id": "",
    "file_ids": [],
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": {
                "value": "Who are you",
                "annotations": []
            }
        }
    ],
    "metadata": {},
    "name": "",
    "plugin_call": {},
    "tool_calls": [],
    "status": "",
    "request_id": "b3ad40b9-f052-9665-a064-dab11c34625f"
}

Response parameters

The 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.

SDK

Code example Python

HELPCODEESCAPE-python
from dashscope import Messages
import dashscope
import os

dashscope.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'}
)

Java

HELPCODEESCAPE-java
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.threads.messages.TextMessageParam;
import com.alibaba.dashscope.threads.messages.ThreadMessage;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        // create a message to thread
        Messages messages = new Messages();
        TextMessageParam param = TextMessageParam.builder()
                // Set the API key using an environment variable. If an environment variable is not set, replace the following line with .apiKey("sk-xxx") and your Model Studio API key.
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .role("user")
                .content("How to make delicious beef and potato stew?")
                .build();
        ThreadMessage message = messages.create("threadId", param);
    }
}

Request parameters

ParameterTypeDefaultDescription
thread_idstring-The thread ID.
contentstring-The message content.
rolestr'user'The role of the message. Default value: user.
metadataDictNoneThe key-value information associated with the message.
workspacestringNoneThe 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_keystringNoneThe API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable (to be unpublished and merged into 'Configure API Key').

Response

The result is a Message object. The following code shows an example of the JSON-formatted content:

HELPCODEESCAPE-json
{
    "id": "message_05494921-a646-484e-85fc-76329acba842",
    "object": "thread.message",
    "created_at": 1711345341301,
    "thread_id": "thread_f1e7737e-b045-479f-99d1-510db49d535b",
    "incomplete_details": {},
    "completed_at": null,
    "incomplete_at": null,
    "assistant_id": "",
    "run_id": "",
    "file_ids": [],
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": {
                "value": "sdhafjdasf",
                "annotations": []
            }
        }
    ],
    "metadata": {
        "key": "value"
    },
    "name": "",
    "plugin_call": {},
    "tool_calls": [],
    "status": "",
    "status_code": 200,
    "request_id": "631de0b3-7e50-9c9e-8444-0924d1b7e7a5"
}

Response parameters

Field nameField typeField description
status_codeintThe HTTP status code of the call. A value of 200 indicates that the call is successful. Other values indicate that the call failed.
idstringThe message ID, which is a UUID string.
contentList[dict]The message content.
content.typestringThe content type, such as text.
content.textdictContent
content.text.valuestringtext value of the content
metadataDictThe key-value information associated with this message.
tool_callsDictThe tool call information.
plugin_callDictThe plugin call information.
created_attimestampThe time when the assistant was created.
gmt_createddatetime2024-03-22 17:12:31
gmt_modifieddatetime2024-03-22 17:12:31
codestringIndicates that the request failed. This parameter specifies the error code. This parameter is ignored if the request is successful.Python only.
messagestringIndicates that the request failed. This parameter provides detailed information about the failure. This parameter is ignored if the request is successful.Python only.

List messages

HTTP

Code example

HELPCODEESCAPE-curl
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages?limit=2&order=desc' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

Request parameters

Parameter nameDescriptionParameter typeRequired
thread idThe thread ID to which the message is passed.stringYes
limitNumber of messagesintegerNo
orderThe sorting order by creation time: `asc` or `desc`.stringNo (Default: `desc`)

Response

HELPCODEESCAPE-json
{
    "object": "list",
    "data": [
        {
            "id": "message_f1933671-19e1-4162-ad25-7326165123e1",
            "object": "thread.message",
            "created_at": 1711508433283,
            "thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
            "assistant_id": "",
            "run_id": "",
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": {
                        "value": "Who are you",
                        "annotations": []
                    }
                }
            ],
            "metadata": {},
            "name": "",
            "plugin_call": {},
            "tool_calls": [],
            "status": ""
        }
    ],
    "first_id": "message_f1933671-19e1-4162-ad25-7326165123e1",
    "last_id": "message_f1933671-19e1-4162-ad25-7326165123e1",
    "has_more": false,
    "request_id": "78f7d607-4a9a-90c6-8040-d3f81c84d60a"
}

Response parameters

A list of message classes is returned. The response also includes the original request parameters and the following additional fields:

  • A list of multiple messages.

SDK

Code example Python

HELPCODEESCAPE-python
from dashscope import Messages
import dashscope
import os

dashscope.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'
)

Java

HELPCODEESCAPE-java
import com.alibaba.dashscope.common.GeneralListParam;
import com.alibaba.dashscope.common.ListResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.threads.messages.ThreadMessage;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Messages messages = new Messages();
        // Set the API key using an environment variable. If an environment variable is not set, replace the following line with .apiKey("sk-xxx") and your Model Studio API key.
        GeneralListParam listThreadMessages = GeneralListParam.builder()
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build();
        ListResult<ThreadMessage> message = messages.list("threadId", listThreadMessages);
    }
}

Request parameters

ParameterTypeDefaultDescription
thread_idstring-The ID of the thread to query.
limitintNoneThe number of messages to retrieve.
orderstringNoneThe sorting order by `created_at`.
workspacestringNoneThe Alibaba Cloud Model Studio Workspace ID, which is required only when the\ `api_key`\ is an API key for a sub-workspace.
api_keystringNoneThe API key for Alibaba Cloud Model Studio. We recommend that you set the API key as an environment variable (This topic will be unpublished and merged into "Configure API Key").

Response Parameters

Field nameField typeField description
has_morebooleanIndicates whether more messages can be retrieved.
last_idstringThe ID of the last message in the returned list.
first_idstringThe ID of the first message in the returned list.
datalist[Message]A list of Message objects.

Retrieve a message

HTTP

Code example

HELPCODEESCAPE-curl
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages/message_ea26d29d-4509-490e-98e9-9f6238bd821b' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

Request parameters

Parameter nameDescriptionParameter typeRequired
thread idThe thread ID of the message to retrieve.stringYes
message_idThe ID of the message to retrieve.stringYes

Response

HELPCODEESCAPE-json
{
    "id": "message_ea26d29d-4509-490e-98e9-9f6238bd821b",
    "object": "thread.message",
    "created_at": 1711508622598,
    "thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
    "assistant_id": "",
    "run_id": "",
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": {
                "value": "Hello",
                "annotations": []
            }
        }
    ],
    "metadata": {},
    "name": "",
    "plugin_call": {},
    "tool_calls": [],
    "status": "",
    "request_id": "4d5ce962-91c3-9edb-87f7-00bbf985135e"
}

Response parameters

The 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.

SDK

Code example Python

HELPCODEESCAPE-python
from dashscope import Messages
import dashscope
import os

dashscope.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'
)

Java

HELPCODEESCAPE-java
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.threads.messages.ThreadMessage;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Messages messages = new Messages();
        // Set the API key using an environment variable. If an environment variable is not set, replace the following line with .apiKey("sk-xxx") and your Model Studio API key.
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        ThreadMessage message = messages.retrieve("threadId", "messageId", apiKey);
    }
}

Request parameters

ParameterTypeDefaultDescription
message_idstring-The ID of the message to query.
thread_idstring-The ID of the thread to which the message belongs.
workspacestringNoneThe Workspace ID of Alibaba Cloud Model Studio. This parameter is required only when the api_key is a sub-workspace API Key.
api_keystringNoneThe API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable (Note: This method is being deprecated and will be merged into the 'Configure API Key' topic).

Response parameters

See the response for the create operation.

Modify a message

HTTP

Code example

HELPCODEESCAPE-curl
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_e99a9fe7-0433-426f-98ad-a5139c36579c/messages/message_ea26d29d-4509-490e-98e9-9f6238bd821b' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
    "metadata": {
        "modified": "true",
        "user": "abc123"
    }
}'

Request parameters

Parameter nameInput ParametersParameter typeRequired
thread_idThe ID of the thread to modify.stringYes
message_idThe ID of the message to modify.stringYes
metadataThe metadata.dict

Response

HELPCODEESCAPE-json
{
    "id": "message_ea26d29d-4509-490e-98e9-9f6238bd821b",
    "object": "thread.message",
    "created_at": 1711508622598,
    "thread_id": "thread_e99a9fe7-0433-426f-98ad-a5139c36579c",
    "incomplete_details": {},
    "completed_at": null,
    "incomplete_at": null,
    "assistant_id": "",
    "run_id": "",
    "file_ids": [],
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": {
                "value": "Hello",
                "annotations": []
            }
        }
    ],
    "metadata": {
        "modified": "true",
        "user": "abc123"
    },
    "name": "",
    "plugin_call": {},
    "tool_calls": [],
    "status": "",
    "request_id": "7877b011-cb94-9df1-9add-dc42b7d611f6"
}

Output Parameters

The 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.

SDK

Code example Python

HELPCODEESCAPE-python
from dashscope import Messages
import dashscope
import os

dashscope.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'}
)

Java

HELPCODEESCAPE-java
import java.util.Collections;
import com.alibaba.dashscope.common.UpdateMetadataParam;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Messages messages = new Messages();
        // Set the API key using an environment variable. If an environment variable is not set, replace the following line with .apiKey("sk-xxx") and your Model Studio API key.
        UpdateMetadataParam updateMetadataParam = UpdateMetadataParam.builder()
                .metadata(Collections.singletonMap("key", "value"))
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .build();
        messages.update("thread_id", "message_Id", updateMetadataParam);
    }
}

Request parameters

ParameterTypeDefaultDescription
message_idstring-The ID of the message to update.
thread_idstring-The ID of the thread to which the message to be updated belongs.
metadataDictNoneThe information associated with the thread.
workspacestringNoneThe Workspace ID of Alibaba Cloud Model Studio, which is required only when the\ api_key\ is a sub-workspace API Key.
api_keystringNoneThe API key for Alibaba Cloud Model Studio. We recommend that you configure the API key as an environment variable (to be unpublished and merged into Configure API Key).

Response parameters

See the response for the create operation.

Error codes

If an assistant API call fails and returns an error message, see Error messages to resolve the issue.

Mirror of Alibaba Cloud Model Studio docs for reference and RAG. Not affiliated with Alibaba Cloud.