Skip to main content
Responses API

同步调用 API 参考

本文介绍如何通过 OpenAI 兼容模式的 Responses API 同步调用 阿里云百炼应用( 智能体 、 工作流 )。适用于需要 即时获取结果 的实时交互场景,可轻松复用现有的 OpenAI 代码库,或快速集成来自 OpenAI 生态的各类工具。

相关参考
本文档仅适用于华北2(北京)地域。

前提条件

使用SDK调用时需配置的base_urlhttps://dashscope.aliyuncs.com/api/v2/apps/agent/{APP_ID}/compatible-mode/v1 使用HTTP方式调用时需配置的EndpointPOST https://dashscope.aliyuncs.com/api/v2/apps/agent/{APP_ID}/compatible-mode/v1/responses
请将 {APP_ID} 替换为实际的应用ID。

请求体

app_id string(必选)应用的标识。应用管理页面的应用卡片上获取应用ID。
通过 HTTP 调用时,请将实际的应用ID放入 URL 中,替换APP_ID
inputstring/array(必选)请求的核心输入内容。可以是一个简单的字符串,也可以是一个包含多轮对话历史的消息数组。
  • 简单字符串: 用于单轮文本对话,例如 "你好"
  • 消息数组 (Messages): 用于多轮对话或包含多媒体(如图片、文件)的输入。数组中的每个元素都是一个消息对象。
    基于pre_response_idconversation_id的上下文功能将在后续支持。目前请在每次请求时传递完整的对话历史。

    消息类型

    System Messageobject(可选)系统消息,用于设定大模型的角色、语气、任务目标或约束条件等。一般放在messages数组的第一位。
    contentstring(必选)消息内容。rolestring(必选)固定为system
    User Messageobject(必选)用户消息,用于向模型传递问题、指令或上下文等。

    属性

    contentstring 或 array(必选)消息内容。
    • 纯文本输入: content 为字符串,例如 "你好"
    • 多模态输入: content 为一个数组,包含文本、图片或文件对象。

      子属性

      文本type string (必选)固定为 input_texttext string(必选)文本内容。图像type string (必选)固定为 input_imageimage_url string(必选)图片的 URL。文件智能体应用支持文件传入。type string (必选)固定为 input_filefile_url string(必选)文件的URL。
    rolestring(必选)固定为user
    Assistant Message object(可选)模型对用户消息的回复。
    contentstring(可选)消息内容。rolestring(必选)固定为assistant
stream boolean(可选)是否流式输出回复。
  • false(默认值):模型生成完所有内容后一次性返回结果。
  • true:边生成边输出,即每生成一部分内容就立即输出一个片段(chunk)。
background boolean(可选)是否以异步方式执行任务。
异步调用暂不支持流式输出。
  • false(默认值):同步。API将保持连接直到任务完成。
  • true:异步。API将立即返回一个任务ID,可通过查询接口来获取结果。
  • 文本输入
  • 流式输出
  • 图像输入
  • 文件输入
单轮对话
from openai import OpenAI
import os

# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY")
app_id='APP_ID' # 替换为实际的应用 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="你是谁?",
)

print(response.model_dump_json(indent=2))
多轮对话将包含完整历史消息的消息对象数组传递给input 参数。
from openai import OpenAI
import os

# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY")
app_id='APP_ID' # 替换为实际的应用 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
)

messages = [
    {"role": "user", "content": "你是谁?"},
    {"role": "assistant", "content": "我是一个AI助手,可以帮助你解答问题、提供信息和协助完成各种任务。"},
    {"role": "user", "content": "你能做什么?"}
]
response = client.responses.create(
    input=messages
)

# 打印完整的响应JSON对象
print(response.model_dump_json(indent=2))

响应对象(非流式输出)

id string
本次请求的唯一标识符(ID),可用于日志记录和问题追踪。
objectstring对象类型,对于本API,其值固定为 responsecreated_at integer
响应创建时间的Unix时间戳(以秒为单位)。
statusstring整个响应任务的最终状态。completed 表示任务已成功结束。outputarray
一个数组,包含了模型生成的所有输出内容。

子属性

output messageobject包含了模型输出内容的消息对象。

子属性

content array
消息的核心内容数组,包含多种类型的内容块(如文本、代码、图片等)。

子属性

text string
模型实际生成的文本回复。
type string
内容块的类型。output_text 表示这是一个输出的文本块。
id string
此条输出消息的唯一ID。
role string
消息的角色。assistant 表示这条消息是由AI助手生成的。
status string
表示该条消息的生成状态。completed 表示该条消息已成功生成。
type string
output数组中元素的类型。message 表示这是一个消息对象。
{
    "created_at": 1758624774,
    "id": "ce65e29a-3d14-4b21-a5c0-ac601f3af888",
    "model": "",
    "object": "response",
    "output": [
        {
            "content": [
                {
                    "annotations": [
                    ],
                    "text": "我可以帮助你做很多事情,包括但不限于:\n\n1. **回答问题**:无论是科学、历史、文化、技术等领域的知识,我都可以尽力为你提供准确的信息。\n2. **写作帮助**:我可以帮你写文章、邮件、报告、故事、诗歌等,也可以进行润色和修改。\n3. **语言翻译**:我可以将文本从一种语言翻译成另一种语言。\n4. **学习辅导**:我可以帮助你理解复杂的概念,解答数学题、物理题等学科问题。\n5. **编程帮助**:我可以协助你编写代码、调试程序、解释算法等。\n6. **提供建议**:在生活、工作、学习等方面,我可以提供一些实用的建议和思路。\n7. **娱乐互动**:我可以讲笑话、玩文字游戏可以将进行简单的聊天互动。\n\n如果你有任何具体的需求或问题,随时告诉我,我会尽力帮助你!",
                    "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": [
    ]
}

响应对象(流式输出)

id string事件的消息ID。code string错误码,调用成功时为空值。messagestring表示错误详细信息,请求成功则忽略。event string事件类型,表示当前响应的状态。

事件通用数据

sequence_number integer事件的序列号,从0开始递增。type string事件类型,与event内容相同。

事件类型详解

整体响应生命周期事件

response.created: 表示响应已创建。response.in_progress: 表示响应处理中。response.completed: 响应完成。

通用数据

responseobject响应对象,包含响应的详细信息*。*
id string响应的唯一标识符。statusstring响应的最终状态。objectstring对象类型固定值为 "response"created_atinteger
响应创建时间的Unix时间戳(以秒为单位)。
output array
输出内容列表。
output messageobject包含了模型输出内容的消息对象。
idstring输出项的唯一标识符。type string输出项的类型。
  • "reasoning": 模型的思考过程。
  • "message": 最终回复。
role string消息的角色。contentarray输出内容部分的列表。
type string内容部分的类型。例如 "output_text" 表示这是一个文本部分。textstring文本内容。annotationsarray注解列表。
statusstring响应的当前状态。

内容构建事件

response.output_item.added: 表示输出项已添加。
output_indexintegeritem 在 output 数组中的索引。itemobject新增的输出项对象。
output messageobject包含了模型输出内容的消息对象。
idstring输出项的唯一标识符。type string输出项的类型。
  • "reasoning": 模型的思考过程。
  • "message": 最终回复。
role string消息的角色。contentarray内容部分列表。statusstring响应的当前状态。
response.output_item.done: 输出项完成。
output_indexinteger已完成的 item 在 output 数组中的索引。itemobject完整的输出项对象。
idstring输出项的唯一标识符。type string输出项的类型。
  • "reasoning": 模型的思考过程。
  • "message": 最终回复。
role string消息的角色。contentarray输出内容部分的列表。
type string内容部分的类型。例如 "output_text" 表示这是一个文本部分。textstring文本内容。annotationsarray注解列表。
statusstring响应的当前状态。
response.content_part.added: 表示在一个输出项(如一条消息)中,新生成了一个内容块(如文本、图片等)。
output_indexinteger关联的 response.output 数组索引。content_indexinteger关联的 item.content 数组索引。item_idstring关联的输出项ID。partobject新添加的内容部分对象。
type string内容部分的类型。例如 "output_text" 表示这是一个文本部分。text string文本内容。annotationsarray注解列表。
response.content_part.done: 内容部分完成。
output_indexinteger关联的 response.output 数组索引。content_indexinteger关联的 item.content 数组索引。item_idstring关联的输出项ID。partobject新添加的内容部分对象。
type string内容部分的类型。例如 "output_text" 表示这是一个文本部分。text string文本内容。annotationsarray注解列表。

文本流事件

response.output_text.delta:输出内容的文本增量。
delta string输出文本的增量片段。output_indexinteger关联的 response.output 数组索引。content_indexinteger关联的 item.content 数组索引。item_idstring关联的输出项ID。
response.output_text.done: 输出文本完成。
text String完整的输出文本内容。output_indexinteger关联的 response.output 数组索引。content_indexinteger关联的 item.content 数组索引。item_idstring关联的输出项ID。
response.reasoning_text.delta:思考过程的文本增量。
delta string思考文本的增量片段。output_indexinteger关联的 response.output 数组索引。content_indexinteger关联的 item.content 数组索引。item_idstring关联的输出项ID。
response.reasoning_text.done:思考过程的流式输出已全部结束。
text String完整的思考过程。output_indexinteger关联的 response.output 数组索引。content_indexinteger关联的 item.content 数组索引。item_idstring关联的输出项ID。
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":"你好","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":"!我是通","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":"义千问","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":"(Qwen","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":"),由阿里云研发的","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":"超大规模语言模型。我","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":"能够回答问题、创作文字","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":",比如写故事、写","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":"公文、写邮件、","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":"写剧本、逻辑推理、","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":"编程等等,还能表达观点","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":",玩游戏等。我支持","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":"多种语言,包括但不限于","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":"中文、英文、德语","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":"、法语、西班牙语","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":"等,满足国际","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":"化的使用需求。我","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":"擅长处理各种任务","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":",无论是专业领域的","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":"知识问答,还是","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":"日常生活中的问题咨询","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":",我都会尽力提供帮助","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":"。我的目标是成为","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":"你最可靠的智能","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":"伙伴!","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":"你好!我是通义千问(Qwen),由阿里云研发的超大规模语言模型。我能够回答问题、创作文字,比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等,还能表达观点,玩游戏等。我支持多种语言,包括但不限于中文、英文、德语、法语、西班牙语等,满足国际化的使用需求。我擅长处理各种任务,无论是专业领域的知识问答,还是日常生活中的问题咨询,我都会尽力提供帮助。我的目标是成为你最可靠的智能伙伴!","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":"你好!我是通义千问(Qwen),由阿里云研发的超大规模语言模型。我能够回答问题、创作文字,比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等,还能表达观点,玩游戏等。我支持多种语言,包括但不限于中文、英文、德语、法语、西班牙语等,满足国际化的使用需求。我擅长处理各种任务,无论是专业领域的知识问答,还是日常生活中的问题咨询,我都会尽力提供帮助。我的目标是成为你最可靠的智能伙伴!"}}
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":"你好!我是通义千问(Qwen),由阿里云研发的超大规模语言模型。我能够回答问题、创作文字,比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等,还能表达观点,玩游戏等。我支持多种语言,包括但不限于中文、英文、德语、法语、西班牙语等,满足国际化的使用需求。我擅长处理各种任务,无论是专业领域的知识问答,还是日常生活中的问题咨询,我都会尽力提供帮助。我的目标是成为你最可靠的智能伙伴!"}],"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":"你好!我是通义千问(Qwen),由阿里云研发的超大规模语言模型。我能够回答问题、创作文字,比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等,还能表达观点,玩游戏等。我支持多种语言,包括但不限于中文、英文、德语、法语、西班牙语等,满足国际化的使用需求。我擅长处理各种任务,无论是专业领域的知识问答,还是日常生活中的问题咨询,我都会尽力提供帮助。我的目标是成为你最可靠的智能伙伴!"}],"status":"completed"}],"parallel_tool_calls":false,"created_at":1760076609,"tool_choice":"auto","model":"","id":"508f1306-3760-49da-9e43-380fd952c297","tools":[],"object":"response","status":"completed"}}

常见问题

  1. 如何传递多轮对话的上下文? 需要在客户端维护完整的对话历史,并在每次请求时将所有历史消息完整地放在input数组中传递给API。 基于pre_response_idconversation_id的上下文功能将在后续支持
  2. 为什么响应示例中的某些字段未在本文说明? 如果使用OpenAI的官方SDK,它可能会根据其自身的模型结构打印出一些额外的字段(通常为null)。这些字段是OpenAI协议本身定义的,我们的服务当前不支持,所以它们为空值。只需关注本文档中描述的字段即可。

错误码

如果应用调用失败并返回报错信息,请参阅错误码解决。