跳转到主要内容
OpenAI Responses

创建响应

兼容 Responses API

POST
/compatible-mode/v1/responses
import os
from openai import OpenAI

client = OpenAI(
  # 若未设置环境变量,请替换为:api_key="sk-xxx"
  api_key=os.getenv("DASHSCOPE_API_KEY"),
  base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

response = client.responses.create(
  model="qwen3.7-plus",
  input="What can you do?"
)

# 获取模型回复
print(response.output_text)
{
  "created_at": 1771165900,
  "id": "f75c28fb-4064-48ed-90da-4d2cc4362xxx",
  "model": "qwen3.7-plus",
  "object": "response",
  "output": [
    {
      "content": [
        {
          "annotations": [],
          "text": "Hello! I am Qwen3.5, a large language model developed by Alibaba Cloud with knowledge up to 2026, designed to assist you with complex reasoning, creative tasks, and multilingual conversations.",
          "type": "output_text"
        }
      ],
      "id": "msg_89ad23e6-f128-4d4c-b7a1-a786e7880xxx",
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": false,
  "status": "completed",
  "tool_choice": "auto",
  "tools": [],
  "usage": {
    "input_tokens": 57,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens": 44,
    "output_tokens_details": {
      "reasoning_tokens": 0
    },
    "total_tokens": 101,
    "x_details": [
      {
        "input_tokens": 57,
        "output_tokens": 44,
        "total_tokens": 101,
        "x_billing_type": "response_api"
      }
    ]
  }
}
旧版 URL 路径 /api/v2/apps/protocols/compatible-mode/v1/responses 即将停止维护,请尽快迁移至新版路径 /compatible-mode/v1/responses

与 OpenAI 的兼容性

本 API 兼容 OpenAI,但在参数、功能和行为上存在差异。 请求仅处理本文档中列出的参数,未提及的 OpenAI 参数将被忽略。 主要差异:
  • 不支持的参数:部分参数不支持,例如 background(仅支持同步调用)。
  • 扩展参数:支持 OpenAI 规范之外的额外参数,例如 enable_thinking

鉴权

string
header
必填

千问 AI 平台 API Key。详见获取 API Key

Header 参数

enum<string>

控制多轮对话中的会话缓存(需配合 previous_response_id 使用)。启用后,服务器将自动缓存对话上下文,从而降低延迟和费用。

  • enable:启用会话缓存。缓存创建按标准输入价格的 125% 计费;缓存命中按 10% 计费。缓存有效期为 5 分钟(命中后重置)。创建缓存至少需要 1024 个 Token。
  • disable:禁用会话缓存。如模型支持,则回退到隐式缓存。

支持的模型:qwen3.8-max-preview(仅 Token Plan 可用)、qwen3.7-maxqwen3.7-max-2026-06-08qwen3.7-max-2026-05-20qwen3-maxqwen3.7-plusqwen3.7-plus-2026-05-26qwen3.6-plusqwen3.5-plusqwen3.5-flashqwen-plusqwen-flashqwen3-coder-plusqwen3-coder-flash

SDK 传参方式:Python 使用 default_headers,Node.js 使用 defaultHeaders

enable,disable

请求体

application/json
string
必填

模型名称。支持的模型包括 qwen3.8-max-preview(仅 Token Plan 可用)、qwen3.7-max、qwen3.7-max-2026-06-08、qwen3.7-max-2026-05-20、qwen3.7-max-preview、qwen3.7-max-2026-05-17、qwen3-max、qwen3-max-2026-01-23、qwen3.7-plus、qwen3.7-plus-2026-05-26、qwen3.6-plus、qwen3.6-plus-2026-04-02、qwen3.5-plus、qwen3.5-plus-2026-04-20、qwen3.5-plus-2026-02-15、qwen3.7-flash、qwen3.7-flash-2026-07-15、qwen3.6-flash、qwen3.6-flash-2026-04-16、qwen3.5-flash、qwen3.5-flash-2026-02-23、qwen3.6-35b-a3b、qwen3.5-397b-a17b、qwen3.5-122b-a10b、qwen3.5-27b、qwen3.5-35b-a3b、qwen-plus、qwen-flash、qwen3-coder-plus、qwen3-coder-flash、qwen3.5-ocr、qwen-plus-character、qwen-flash-character。

string
必填

模型的输入内容。支持纯文本字符串,或按对话顺序排列的消息数组。

string

插入到上下文开头的系统指令。使用 previous_response_id 时,上一轮中指定的 instructions 不会延续到当前上下文。

string

上一轮响应的唯一 ID,有效期为 7 天。通过该参数可实现多轮对话,服务器会自动检索并将上一轮的输入和输出作为上下文传入。若同时提供了消息数组和 previous_response_id,input 中的新消息将追加到历史上下文之后。不能与 conversation 同时使用。使用示例请参考多轮对话指南

string

当前响应所属的会话。会话中的历史记录将自动作为上下文传入当前请求,当前请求的输入和输出也会在响应完成后自动添加到会话中。不能与 previous_response_id 同时使用。

boolean
默认值false

是否启用流式输出。设置为 true 时,模型响应数据将实时以流的形式返回给客户端。

object[]

模型可使用的工具列表。支持的工具类型:web_searchcode_interpreterweb_extractorweb_search_imageimage_searchfile_searchmcpfunction

内置工具使用 {"type": "<tool_name>"} 格式。例如:{"type": "web_search"}

MCP 工具使用以下格式:

{
    "type": "mcp",
    "server_protocol": "sse",
    "server_label": "amap-maps",
    "server_description": "AMAP MCP Server...",
    "server_url": "https://dashscope.aliyuncs.com/api/v1/mcps/amap-maps/sse",
    "headers": {
        "Authorization": "Bearer $DASHSCOPE_API_KEY"
    }
}

Function 工具使用以下格式:

[{
  "type": "function",
  "name": "get_weather",
  "description": "Get weather information for a specified city",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "The name of the city"
      }
    },
    "required": ["city"]
  }
}]
``` 使用示例请参考[函数调用指南](/developer-guides/tool-calling/function-calling)和[联网搜索指南](/developer-guides/tool-calling/web-search)。
enum<string>

控制模型选择和调用工具的方式。支持字符串格式和对象格式。

字符串格式:

  • auto:模型自动决定是否调用工具。
  • none:阻止模型调用任何工具。
  • required:强制模型调用工具。仅当 tools 列表中只有一个工具时可用。

**对象格式:**指定模型可使用的工具范围,模型只能从预定义的工具列表中选择并调用。

number

控制生成文本多样性的采样温度。温度越高,生成的文本越多样;温度越低,生成的文本越确定。取值范围:[0, 2)。temperaturetop_p 都能控制生成文本的多样性,建议只设置其中一个。

number

控制生成文本多样性的核采样概率阈值。top_p 越高,生成文本越多样;top_p 越低,生成文本越确定。取值范围:(0, 1.0]。temperaturetop_p 都能控制生成文本的多样性,建议只设置其中一个。

boolean

是否启用思考模式。设置为 true 时,模型在回复前会先进行思考,思考内容通过 reasoning 类型的输出项返回。推理 Token 计入 output_tokens_details.reasoning_tokens,并按推理 Token 价格计费。启用思考模式时,建议同时启用内置工具,以在复杂任务上获得最佳模型性能。

该参数不是标准 OpenAI 参数。 Python SDK 需通过 extra_body={"enable_thinking": True} 传递;Node.js SDK 和 curl 可直接在顶层参数中使用 enable_thinking: true。建议使用 reasoning.effort 替代,enable_thinking 后续将不再支持。

object

思考模式相关配置。

响应

200-application/json
string

本次响应的唯一 ID,有效期为 7 天。可将此 ID 传入 previous_response_id 参数以实现多轮对话。

number

本次请求的 Unix 时间戳(秒)。

enum<string>

对象类型。值为 response

response
enum<string>

响应生成的状态。

completed,failed,in_progress,cancelled,queued,incomplete
string

生成本次响应所使用的模型 ID。

object[]

模型生成的输出项数组。数组中元素的类型和顺序取决于模型的响应。

boolean

是否启用了并行工具调用。

string

请求中 tool_choice 参数的回显值。有效值为 autononerequired

object[]

请求中 tools 参数的完整内容回显。结构与请求体中的 tools 参数相同。

object | null

模型生成响应失败时返回的错误对象。成功时此字段为 null

object

本次请求的 Token 消耗信息。