Skip to main content

Synchronous Invocation API Reference

This topic describes how to use the OpenAI-compatible Responses API to make a synchronous call to an Alibaba Cloud Model Studio application, such as an agent or a workflow . This method is suitable for real-time interactive scenarios that require immediate results and lets you easily reuse existing OpenAI codebases or quickly integrate various tools from the OpenAI ecosystem.

References
This topic is applicable only to the China (Beijing) region.

Prerequisites

The base_url to configure for SDK calls: https://dashscope.aliyuncs.com/api/v2/apps/agent/{APP_ID}/compatible-mode/v1 The Endpoint to configure for HTTP calls: POST https://dashscope.aliyuncs.com/api/v2/apps/agent/{APP_ID}/compatible-mode/v1/responses
Replace {APP_ID} with your actual application ID.

Request body

app_id string(Required)The ID of the application.On the Application Management page, you can obtain the application ID from the application card.
When you make an HTTP call, replace APP_ID in the URL with your actual application ID.
inputstring/array(Required)The core input content for the request. This can be a simple string or an array of message objects that contains the multi-turn conversation history.
  • Simple string: Used for single-turn text conversations, such as "Hello".
  • Message array (Messages): Used for multi-turn conversations or inputs that contain multimedia, such as images or files. Each element in the array is a message object.
    Context management based on pre_response_id or conversation_id will be supported in a future release. For now, pass the complete conversation history in each request.

    Message types

    System Messageobject(Optional)A system message that sets the role, tone, task objectives, or constraints for the large language model. It is typically placed as the first element in the messages array.
    contentstring(Required)The content of the message.rolestring(Required)The value must be system.
    User Messageobject(Required)A user message that passes questions, instructions, or context to the model.

    Properties

    contentstring or array(Required)The content of the message.
    • Plain text input: content is a string, such as "Hello".
    • Multimodal input: content is an array that contains text, image, or file objects.

      Sub-properties

      Texttype string (Required)The value must be input_text.text string(Required)The text content.Imagetype string (Required)The value must be input_image.image_url string(Required)The URL of the image.FileFile input is supported only for agent applications.type string (Required)The value must be input_file.file_url string(Required)The URL of the file.
    rolestring(Required)The value must be user.
    Assistant Message object(Optional)The model's reply to a user message.
    contentstring(Optional)The message content.rolestring(Required)The value must be assistant.
stream boolean(Optional)Specifies whether to stream the reply.
  • false (default): The model returns the result in a single response after generating all content.
  • true: The model outputs content as it is generated. Each chunk is sent as soon as it is ready.
background boolean(Optional)Specifies whether to execute the task asynchronously.
Asynchronous invocation does not support streaming output.
  • false (default): Synchronous. The API maintains the connection until the task is complete.
  • true: Asynchronous. The API immediately returns a task ID. You can then use a query API to retrieve the result.
  • Text input
  • Streaming output
  • Image input
  • File input
Single-turn conversation
from openai import OpenAI
import os

# If you have not configured the environment variable, you can replace the following line with api_key="sk-xxx" and use your Model Studio API key.
# However, do not hard-code the API key into your code in a production environment to reduce the risk of API key leakage.
api_key=os.getenv("DASHSCOPE_API_KEY")
app_id='APP_ID' # Replace with your actual application ID.
base_url = f'https://dashscope.aliyuncs.com/api/v2/apps/agent/{app_id}/compatible-mode/v1/'

client = OpenAI(
    api_key = api_key,
    base_url = base_url
)

response = client.responses.create(
   input="Who are you?",
)

print(response.model_dump_json(indent=2))
Multi-turn conversationPass a message object array that contains the complete conversation history to the input parameter.
from openai import OpenAI
import os

# If you have not configured the environment variable, you can replace the following line with api_key="sk-xxx" and use your Model Studio API key.
# However, do not hard-code the API key into your code in a production environment to reduce the risk of API key leakage.
api_key=os.getenv("DASHSCOPE_API_KEY")
app_id='APP_ID' # Replace with your actual application ID.
base_url = f'https://dashscope.aliyuncs.com/api/v2/apps/agent/{app_id}/compatible-mode/v1/'

# Initialize the client.
client = OpenAI(
    api_key=api_key,
    base_url=base_url
)

messages = [
    {"role": "user", "content": "Who are you?"},
    {"role": "assistant", "content": "I am an AI assistant that can help you answer questions, provide information, and assist with various tasks."},
    {"role": "user", "content": "What can you do?"}
]
response = client.responses.create(
    input=messages
)

# Print the complete response JSON object.
print(response.model_dump_json(indent=2))

Response object (non-streaming)

id string
The unique identifier (ID) for this request. You can use it for log records and issue tracking.

objectstringThe object type, which is always response for this API.created_at integer
The Unix timestamp (in seconds) when the response was created.

statusstringThe final status of the response task. A value of completed indicates that the task finished successfully.outputarray
An array that contains all the output content generated by the model.

Sub-properties

output messageobjectA message object that contains the model's output content.

Sub-properties

content array
An array of core message content, which can include various types of content blocks, such as text, code, and images.

Sub-properties

text string
The actual text reply generated by the model.

type string
The type of the content block. A value of output_text indicates an output text block.

id string
The unique ID of this output message.

role string
The role of the message. A value of assistant indicates that this message was generated by the AI assistant.

status string
The generation status of the message. A value of completed indicates that the message was generated successfully.

type string
The type of the element in the output array. A value of message indicates a message object.

{
    "created_at": 1758624774,
    "id": "ce65e29a-3d14-4b21-a5c0-ac601f3af888",
    "model": "",
    "object": "response",
    "output": [
        {
            "content": [
                {
                    "annotations": [
                    ],
                    "text": "I can help you with many things, including but not limited to:\n\n1. **Answering questions**: I can do my best to provide you with accurate information in fields such as science, history, culture, and technology.\n2. **Writing assistance**: I can help you write articles, emails, reports, stories, poems, and more. I can also polish and revise your text.\n3. **Language translation**: I can translate text from one language to another.\n4. **Learning support**: I can help you understand complex concepts and answer questions in subjects like math and physics.\n5. **Programming help**: I can assist you with writing code, debugging programs, and explaining algorithms.\n6. **Providing suggestions**: I can offer practical advice and ideas for life, work, and study.\n7. **Entertainment**: I can tell jokes, play word games, and engage in simple chats.\n\nIf you have any specific needs or questions, feel free to let me know, and I will do my best to help you!",
                    "type": "output_text"
                }
            ],
            "id": "msg_f25e8129-a130-447c-8e03-3f2b148c4e1d",
            "role": "assistant",
            "status": "completed",
            "type": "message"
        }
    ],
    "parallel_tool_calls": false,
    "status": "completed",
    "tool_choice": "auto",
    "tools": [
    ]
}

Response object (streaming)

id stringThe message ID of the event.code stringThe error code. This field is empty if the call is successful.messagestringThe detailed error message. This field is ignored if the request is successful.event stringThe event type, which indicates the current status of the response.

Common event data

sequence_number integerThe serial number of the event, which starts from 0 and increments.type stringThe event type. The value is the same as the event field.

Detailed event types

Overall response lifecycle events

response.created: Indicates that the response has been created.response.in_progress: Indicates that the response is being processed.response.completed: The response is complete.

Common data

responseobjectThe response object, which contains detailed information about the response*.*
id stringThe unique ID of the response.statusstringThe final status of the response.objectstringThe object type. The value is always "response".created_atinteger
The Unix timestamp (in seconds) when the response was created.

output array
A list of output content.

output messageobjectA message object that contains the model's output content.
idstringThe unique ID of the output item.type stringThe type of the output item.
  • "reasoning": The model's thought process.
  • "message": The final reply.
role stringThe role of the message.contentarrayA list of output content parts.
type stringThe type of the content part. For example, "output_text" indicates a text part.textstringThe text content.annotationsarrayA list of annotations.
statusstringThe current status of the response.

Content construction events

response.output_item.added: Indicates that an output item has been added.
output_indexintegerThe index of the item in the output array.itemobjectThe newly added output item object.
output messageobjectA message object that contains the model's output content.
idstringThe unique ID of the output item.type stringThe type of the output item.
  • "reasoning": The model's thought process.
  • "message": The final reply.
role stringThe role of the message.contentarrayA list of content parts.statusstringThe current status of the response.
response.output_item.done: The output item is complete.
output_indexintegerThe index of the completed item in the output array.itemobjectThe complete output item object.
idstringThe unique ID of the output item.type stringThe type of the output item.
  • "reasoning": The model's thought process.
  • "message": The final reply.
role stringThe role of the message.contentarrayA list of output content parts.
type stringThe type of the content part. For example, "output_text" indicates a text part.textstringThe text content.annotationsarrayA list of annotations.
statusstringThe current status of the response.
response.content_part.added: Indicates that a new content block, such as text or an image, has been generated within an output item, such as a message.
output_indexintegerThe index in the response.output array.content_indexintegerThe index in the item.content array.item_idstringThe ID of the associated output item.partobjectThe newly added content part object.
type stringThe type of the content part. For example, "output_text" indicates a text part.text stringThe text content.annotationsarrayA list of annotations.
response.content_part.done: The content part is complete.
output_indexintegerThe index in the response.output array.content_indexintegerThe index in the item.content array.item_idstringThe ID of the associated output item.partobjectThe newly added content part object.
type stringThe type of the content part. For example, "output_text" indicates a text part.text stringThe text content.annotationsarrayA list of annotations.

Text stream events

response.output_text.delta: A text increment of the output content.
delta stringAn incremental chunk of the output text.output_indexintegerThe index in the response.output array.content_indexintegerThe index in the item.content array.item_idstringThe ID of the associated output item.
response.output_text.done: The output text is complete.
text stringThe complete output text content.output_indexintegerThe index in the response.output array.content_indexintegerThe index in the item.content array.item_idstringThe ID of the associated output item.
response.reasoning_text.delta: A text increment of the reasoning process.
delta stringAn incremental chunk of the reasoning text.output_indexintegerThe index in the response.output array.content_indexintegerThe index in the item.content array.item_idstringThe ID of the associated output item.
response.reasoning_text.done: The streaming output of the reasoning process is complete.
text stringThe complete reasoning process.output_indexintegerThe index in the response.output array.content_indexintegerThe index in the item.content array.item_idstringThe ID of the associated output item.
id:1 | event:response.created | :HTTP_STATUS/200 | data:{"sequence_number":0,"type":"response.created","response":{"output":[],"parallel_tool_calls":false,"created_at":1760076609,"tool_choice":"auto","model":"","id":"508f1306-3760-49da-9e43-380fd952c297","tools":[],"object":"response","status":"queued"}}
id:2 | event:response.in_progress | :HTTP_STATUS/200 | data:{"sequence_number":1,"type":"response.in_progress","response":{"output":[],"parallel_tool_calls":false,"created_at":1760076609,"tool_choice":"auto","model":"","id":"508f1306-3760-49da-9e43-380fd952c297","tools":[],"object":"response","status":"in_progress"}}
id:3 | event:response.output_item.added | :HTTP_STATUS/200 | data:{"sequence_number":2,"item":{"id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","role":"assistant","type":"message","content":[],"status":"in_progress"},"output_index":0,"type":"response.output_item.added"}
id:4 | event:response.content_part.added | :HTTP_STATUS/200 | data:{"sequence_number":3,"output_index":0,"type":"response.content_part.added","content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","part":{"type":"output_text","annotations":[],"text":""}}
id:5 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":4,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":"Hello","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:6 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":5,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":"! I am ","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:7 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":6,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":"Qwen","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:8 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":7,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":", a large-scale","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:9 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":8,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" language model developed by Alibaba Cloud.","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:10 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":9,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" I can answer questions and create","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:11 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":10,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" text, such as writing stories,","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:12 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":11,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" official documents,","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:13 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":12,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" emails,","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:14 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":13,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" scripts, logical reasoning,","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:15 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":14,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" programming, and more. I can also express opinions","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:16 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":15,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" and play games. I support","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:17 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":16,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" multiple languages, including but not limited to","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:18 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":17,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" Chinese, English, German","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:19 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":18,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":", French, and Spanish","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:20 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":19,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" to meet international","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:21 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":20,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" usage needs. I","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:22 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":21,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" am good at handling various tasks","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:23 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":22,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":", whether it's answering questions in professional fields","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:24 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":23,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" or consulting on","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:25 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":24,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" daily life issues","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:26 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":25,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":", I will do my best to help","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:27 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":26,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":". My goal is to become","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:28 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":27,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" your most reliable intelligent","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:29 | event:response.output_text.delta | :HTTP_STATUS/200 | data:{"sequence_number":28,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","delta":" partner!","output_index":0,"type":"response.output_text.delta","logprobs":[]}
id:30 | event:response.output_text.done | :HTTP_STATUS/200 | data:{"sequence_number":29,"content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","text":"Hello! I am Qwen, a large-scale language model developed by Alibaba Cloud. I can answer questions and create text, such as writing stories, official documents, emails, and scripts, performing logical reasoning, programming, and more. I can also express opinions and play games. I support multiple languages, including but not limited to Chinese, English, German, French, and Spanish, to meet international usage needs. I am good at handling various tasks, whether it's answering questions in professional fields or consulting on daily life issues, I will do my best to help. My goal is to become your most reliable intelligent partner!","output_index":0,"type":"response.output_text.done","logprobs":[]}
id:31 | event:response.content_part.done | :HTTP_STATUS/200 | data:{"sequence_number":30,"output_index":0,"type":"response.content_part.done","content_index":0,"item_id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","part":{"type":"output_text","annotations":[],"text":"Hello! I am Qwen, a large-scale language model developed by Alibaba Cloud. I can answer questions and create text, such as writing stories, official documents, emails, and scripts, performing logical reasoning, programming, and more. I can also express opinions and play games. I support multiple languages, including but not limited to Chinese, English, German, French, and Spanish, to meet international usage needs. I am good at handling various tasks, whether it's answering questions in professional fields or consulting on daily life issues, I will do my best to help. My goal is to become your most reliable intelligent partner!"}}
id:32 | event:response.output_item.done | :HTTP_STATUS/200 | data:{"sequence_number":31,"item":{"id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","role":"assistant","type":"message","content":[{"type":"output_text","annotations":[],"text":"Hello! I am Qwen, a large-scale language model developed by Alibaba Cloud. I can answer questions and create text, such as writing stories, official documents, emails, and scripts, performing logical reasoning, programming, and more. I can also express opinions and play games. I support multiple languages, including but not limited to Chinese, English, German, French, and Spanish, to meet international usage needs. I am good at handling various tasks, whether it's answering questions in professional fields or consulting on daily life issues, I will do my best to help. My goal is to become your most reliable intelligent partner!"}],"status":"completed"},"output_index":0,"type":"response.output_item.done"}
id:33 | event:response.completed | :HTTP_STATUS/200 | data:{"sequence_number":32,"type":"response.completed","response":{"output":[{"id":"msg_8087bfdd-ba53-45db-b309-6a3b92ca4f3e","role":"assistant","type":"message","content":[{"type":"output_text","annotations":[],"text":"Hello! I am Qwen, a large-scale language model developed by Alibaba Cloud. I can answer questions and create text, such as writing stories, official documents, emails, and scripts, performing logical reasoning, programming, and more. I can also express opinions and play games. I support multiple languages, including but not limited to Chinese, English, German, French, and Spanish, to meet international usage needs. I am good at handling various tasks, whether it's answering questions in professional fields or consulting on daily life issues, I will do my best to help. My goal is to become your most reliable intelligent partner!"}],"status":"completed"}],"parallel_tool_calls":false,"created_at":1760076609,"tool_choice":"auto","model":"","id":"508f1306-3760-49da-9e43-380fd952c297","tools":[],"object":"response","status":"completed"}}

FAQ

  1. How do I pass the context for a multi-turn conversation? You must maintain the complete conversation history on the client side and pass all historical messages in the input array with each API request. Context management based on pre_response_id or conversation_id will be supported in a future release.
  2. Why are some fields in the response examples not described in this topic? If you use the official OpenAI SDK, the SDK may return extra fields that are usually null. These fields are part of the OpenAI protocol's data model but are not supported by this service. You can safely ignore these fields and focus on the ones described in this document.

Error codes

If the application call fails and returns an error message, see Error messages for troubleshooting.
Managed Agent API
Flow Agent API
Sandbox API
RAG API
Long-term Memory
ParseX API
Security API
Framework Integration