Skip to main content

New agent application API

This topic covers the input and output parameters for using the DashScope API to call the new agent application in Alibaba Cloud Model Studio.

Related guide:Agent 2.0.
This topic is applicable only to the China (Beijing) region.

Prerequisites

Before you begin, ensure you complete the following:
  1. Create an application: Go to application management, create a new agent application in Model Studio, and record the application ID.
  2. Get an API Key: Obtain an API Key from key management and configure the API Key as an environment variable.
  3. Install the SDK (optional): Install the DashScope SDK for your programming language.

Invocation method

  • HTTP API invocation Request URL: POST https://dashscope.aliyuncs.com/api/v1/apps/APP_ID/completion
    Replace APP_ID with your actual application ID.
  • SDK invocation The Python and Java SDKs use the correct endpoint by default. To set a custom endpoint, use the base_url parameter.
Online debugging: Go to application card -> publish -> API debugging, enter the parameters, and click run.

Request body

app_idstring(Required)The application ID.Obtain this from the application card in Application Management.
In the Java SDK, this parameter is appId. For HTTP calls, replace APP_ID in the URL with your application ID.
promptstring(Required)The user input that guides the application's response.
For HTTP calls, place the prompt in the input object.
session_idstring (Optional)The identifier for the conversation history.When you pass session_id, the request automatically includes the conversation history stored in the cloud. In this case, you must pass prompt.This ID expires after 1 hour of inactivity.
In the Java SDK, use setSessionId. For HTTP calls, place the session_id in the input object.
workspace string (Optional)The workspace identifier. For more information, see Obtain Workspace ID.You need to pass the workspace ID only when you call an application in a sub-workspace.
For HTTP calls, specify the X-DashScope-WorkSpace header.
stream boolean (Optional, default: false)Specifies whether to stream the response.We recommend that you set this to true to improve the reading experience and reduce the risk of timeouts.Valid values:
  • false (default): The full response is returned only after generation is complete.
  • true (recommended): The response is streamed in chunks as it is generated. You must assemble these chunks to form the complete response.
To enable streaming output with the Java SDK, use the streamCall interface. For HTTP calls, set the X-DashScope-SSE header to enable.
incremental_output boolean (Optional, default: false)Specifies whether to enable incremental output in streaming output mode.We recommend setting this to true to improve the reading experience.Valid values:
  • false (default): Each output contains the entire sequence generated so far. The final output is the complete result.
I
I like
I like apple
I like apple.
  • true (recommended): Each chunk contains only new content. You must concatenate the chunks to get the full result.
I
like
apple
.
In the Java SDK, this parameter is incrementalOutput. For HTTP calls, place incremental_output in the parameters object.
enable_thinking boolean (Optional, default: false)This parameter toggles between thinking and non-thinking modes for deep-thinking models.Valid values:
  • False (default): Non-thinking mode. The final answer is returned directly in the text field.
  • True: Enables thinking mode. The model first outputs its thinking process and then returns the final answer.
Priority:
  • If this parameter is not set in the API call, the thinking mode setting configured for the model in the application is used.
  • If enable_thinking is set in the call, the API parameter takes precedence.
To obtain the content of the thinking process, you must set has_thoughts to True:
  1. The thinking process is retrieved from the thought field.
  2. The final answer is obtained from the text field.
Enabling enable_thinking may not always output the thinking process.
In the Java SDK, this parameter is enableThinking. For HTTP calls, place enable_thinking in the parameters object.
Requires Java Dashscope SDK version 2.20.0 or later.
has_thoughts boolean (Optional, default: false)Specifies whether to output the thinking process of a model that has thinking mode enabled. The process is returned in the thoughts field.Valid values:
  • True: Output the process.
  • False (default): Do not output the process.
In the Java SDK, this parameter is hasThoughts. For HTTP calls, place has_thoughts in the parameters object.
image_list array(Optional)A list of images. This parameter supports image URLs and Data URLs (Base64-encoded). The application must use an Image and Video Understanding model.The base64 encoding format can be constructed as a Data URL: data:[MIME_type];base64,{base64_image}. For a detailed description and code examples, see the Visual Understanding section.
In the Java SDK, this parameter is images. For HTTP calls, place image_list in the input object.
file_list array(Optional)A list of file URLs.
In the Java SDK, this parameter is files. For HTTP calls, place file_list in the input object.
Requires Python Dashscope SDK version 2.0.1 or later, or Java Dashscope SDK version 2.21.13 or later.
model_idstring(Optional)The model name.You can use this parameter to specify the model to be used for the current API call.Priority: If the model_id passed through the API is different from the one configured in the console, the API parameter value takes precedence.
In the Java SDK, this parameter is modelId. For HTTP calls, place model_id in the parameters object.
Requires Java Dashscope SDK version 2.19.3 or later.
  • Single-turn
  • Multi-turn
  • Streaming output
  • File Q&A
  • Visual understanding
  • Pass custom parameters
  • Python
  • Java
  • HTTP
Request example
import os
import dashscope
from dashscope import DashScopeError

client = dashscope.Client(
    # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
    api_key=os.getenv("DASHSCOPE_API_KEY"))
try:
    response = client.apps.completions.create(
        app_id='APP_ID',  # 替换为实际的应用 ID
        prompt='你是谁?')
    print(response.text)
except DashScopeError as e:
    print(f'request_id={e.request_id}')
    print(f'code={e.code}')
    print(f'message={e.message}')
    print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')

Response object

When a call fails, the Python SDK throws a DashScopeError exception. You can obtain error information from e.code/e.status_code/e.message/e.request_id.
Example success responseThe following shows the response object of Python SDK 2.0. (When you call the API over HTTP, fields such as text, finish_reason, and session_id are in the output object.)
{
    "request_id": "fdfc3182-bc9d-4b45-a287-cd83b13aca02",
    "text": "你好!我是千问,阿里巴巴集团旗下的超大规模语言模型。我可以帮助你回答问题、创作文字,比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等。有什么需要我帮忙的吗?",
    "finish_reason": "stop",
    "session_id": "cbb2e26ac4cc4cc3b2d114e1f73c127e",
    "thoughts": null,
    "doc_references": null,
    "workflow_message": null,
    "usage": {
        "models": [
            {
                "model_id": "qwen-plus-latest",
                "input_tokens": 142,
                "output_tokens": 296
            }
        ]
    }
}
Example error responseIf a request fails, the Python SDK throws an exception whose code and message attributes indicate the reason for the error.The following example shows an error response when an invalid API-KEY is provided.
request_id=1d14958f-0498-91a3-9e15-be477971967b,
code=401,
message=Invalid API-key provided.
request_id stringA unique identifier for this call.
In the Java SDK, this parameter is returned as requestId.
output objectThe result of the request.

output properties

text stringThe content generated by the model.finish_reason stringThe reason the generation stopped.stop indicates a natural stop (a predefined stop sequence). null indicates a forced stop (for example, the maximum length limit is reached or the generation is manually stopped).session_idstringA unique identifier for the current conversation.Pass this ID in subsequent requests to include the conversation history.thoughtsarrayWhen you make a call, set the has_thoughts parameter to True to view the thinking process of the Deep Thinking Model in thoughts.
thought stringThe thinking process of the model.Procedure
  1. In the console application, select a deep thinking model and publish it successfully;
  2. Set the has_thoughts parameter to True when calling the API.
action_type stringThe type of execution steps returned by the model. For example, reasoning indicates the thinking process of a deep thinking model.action_name stringThe name of the action being executed, such as the thinking process.action stringThe step being executed.action_input_stream stringThe streaming input for the action.action_input stringThe input for the action.
doc_references nullIn new agent applications (Agent 2.0), knowledge bases are unified as tools that the agent calls autonomously, so this field always returns null (unlike Agent 1.0 applications). To display knowledge sources in responses, enable the show response sources feature in the application configuration, and the references will be embedded in the response text as superscripts.
usage objectUsage details for this request.

usage properties

modelsarrayInformation about the model used in this call.
model_id stringThe ID of the model used for the request.input_tokens integerThe length, in tokens, of the user input text.output_tokens integerThe length, in tokens, of the model output.

QPM limit

The default QPM limit per application is 15,000.

Error codes

If a call fails, refer to the error codes for a solution.