> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 运行查询

> 运行已通过 [创建查询令牌](/zh/api-reference/queries/create-query-token) 获取令牌的查询，并以 Server-Sent Events 流式返回回答。此接口以查询令牌作为凭据，不使用知识库 API 密钥。服务端在受理请求时校验令牌。

两步调用流程及流的读取方式，详见 [查询知识库](/zh/api-reference/guides/knowledge-base#query-the-knowledge-base)。

哪些错误应当重试、错误详情各字段的含义，详见 [处理错误](/zh/api-reference/guides/knowledge-base#handle-errors)。

查询成功时，会先保存查询记录，再发送 `answer.done`。重复调用会再次运行查询。



## OpenAPI

````yaml /zh/api-reference/openapi_service.json post /knowledge-fs/query-stream
openapi: 3.0.1
info:
  description: 用于 Dify 应用与知识库的 REST API。应用类接口使用应用 API 密钥认证，知识库类接口使用知识库 API 密钥认证。
  title: Dify 服务 API
  version: 1.0.0
servers:
  - description: Dify 服务 API 的基础 URL。自部署时，替换为你的 API 基础 URL。
    url: https://{api_base_url}
    variables:
      api_base_url:
        default: api.dify.ai/v1
        description: API 基础 URL 的主机与路径，不含 `https://` 前缀。
security:
  - ApiKeyAuth: []
tags:
  - description: 与聊天消息和交互相关的操作。
    name: 对话消息
  - description: 文件上传和预览操作。
    name: 文件操作
  - description: 终端用户信息相关操作。
    name: 终端用户
  - description: 用户反馈操作。
    name: 消息反馈
  - description: 与管理会话相关的操作。
    name: 会话管理
  - description: 文字转语音和语音转文字操作。
    name: 语音与文字转换
  - description: 获取应用设置和信息的操作。
    name: 应用配置
  - description: 与管理标注直接回复相关的操作。
    name: 标注管理
  - description: 暂停等待人工输入的工作流恢复操作。
    name: 人工介入
  - description: 用于执行和管理工作流的操作。
    name: 工作流运行
  - description: 文本生成相关操作。
    name: 文本生成消息
  - description: 用于管理知识库的操作，包括创建、配置和检索。
    name: 知识库
  - description: 用于在知识库中创建、更新和管理文档的操作。
    name: 文档
  - description: 用于管理分段和子分段的操作。
    name: 分段
  - description: 用于管理知识库元数据字段和文档元数据值的操作。
    name: 元数据
  - description: 用于管理知识库标签和标签绑定的操作。
    name: 标签
  - description: 用于获取可用模型的操作。
    name: 模型
  - description: 用于管理和运行知识流水线的操作，包括数据源插件和流水线执行。
    name: 知识流水线
  - description: 向知识库提问，并流式返回回答。
    name: 查询
  - description: 查看一次查询检索到了什么，以及原因。
    name: 查询记录
  - description: 在后台运行研究问题，并获取其结果。
    name: 研究任务
  - description: 读取知识库中的文档及其文件、修订版本和分段，并用元数据标注文档。
    name: 文档与分段
  - description: 跟踪、取消或重试索引，并列出知识库的后台任务。
    name: 索引任务
  - description: 跟踪文档文件、一批文件或数据源的删除，直到完成。
    name: 删除作业
  - description: 管理为知识库提供内容的数据源，以及它们的定时同步设置和连接。
    name: 数据源
  - description: 立即同步数据源，导入网站页面或提供商条目，并跟踪、取消或重试运行。
    name: 数据源同步与导入
  - description: 读取和更改知识库的模型与检索设置，并跟踪更改触发的迁移。
    name: 设置
paths:
  /knowledge-fs/query-stream:
    post:
      tags:
        - Queries
      summary: 运行查询
      description: >-
        运行已通过 [创建查询令牌](/zh/api-reference/queries/create-query-token) 获取令牌的查询，并以
        Server-Sent Events 流式返回回答。此接口以查询令牌作为凭据，不使用知识库 API 密钥。服务端在受理请求时校验令牌。


        两步调用流程及流的读取方式，详见
        [查询知识库](/zh/api-reference/guides/knowledge-base#query-the-knowledge-base)。


        哪些错误应当重试、错误详情各字段的含义，详见
        [处理错误](/zh/api-reference/guides/knowledge-base#handle-errors)。


        查询成功时，会先保存查询记录，再发送 `answer.done`。重复调用会再次运行查询。
      operationId: runQuery
      parameters:
        - description: >-
            [创建查询令牌](/zh/api-reference/queries/create-query-token) 响应中的
            `trace_id`，与令牌中的 `trace_id` 声明值相同。与令牌不一致的值会被拒绝并返回 `403`。参见
            [查询知识库](/zh/api-reference/guides/knowledge-base#query-the-knowledge-base)。
          in: header
          name: X-Trace-ID
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            examples:
              request:
                summary: 请求示例 - 新会话
                value:
                  activeDocumentIds: []
                  activeEntityIds: []
                  knowledgeSpaceId: 8d2c6a1e-5b3f-4c7d-9e01-2a3b4c5d6e7f
                  mode: fast
                  query: What changed in the refund policy this quarter?
              request_session:
                summary: 请求示例 - 带会话 ID
                value:
                  activeDocumentIds: []
                  activeEntityIds: []
                  knowledgeSpaceId: 8d2c6a1e-5b3f-4c7d-9e01-2a3b4c5d6e7f
                  mode: fast
                  query: What is the refund window for monthly plans?
                  sessionId: a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d
            schema:
              additionalProperties: false
              description: 需提供非空白的 `query`。
              properties:
                activeDocumentIds:
                  description: 从返回的 `request` 对象复制。这些 ID 不会对检索到的内容进行筛选或排序。
                  items:
                    format: uuid
                    type: string
                  maxItems: 100
                  type: array
                activeEntityIds:
                  description: 从返回的 `request` 对象复制。这些 ID 不会对检索到的内容进行筛选或排序。
                  items:
                    maxLength: 200
                    minLength: 1
                    type: string
                  maxItems: 100
                  type: array
                knowledgeSpaceId:
                  description: >-
                    知识库的内部 ID，从
                    [创建查询令牌](/zh/api-reference/queries/create-query-token) 返回的
                    `request` 对象复制。必须与令牌一致，不一致会被拒绝并返回 `403`。
                  format: uuid
                  minLength: 1
                  type: string
                mode:
                  description: >-
                    检索深度，从
                    [创建查询令牌](/zh/api-reference/queries/create-query-token) 返回的
                    `request` 对象复制。可传 `null`，表示使用知识库的默认深度。
                  enum:
                    - fast
                    - deep
                    - research
                    - null
                  nullable: true
                  type: string
                query:
                  description: 要查询的问题。不能为空白，且不超过 16,384 个 UTF-8 字节。
                  maxLength: 16000
                  nullable: false
                  pattern: \S
                  type: string
                sessionId:
                  description: >-
                    会话 ID，从
                    [创建查询令牌](/zh/api-reference/queries/create-query-token) 返回的
                    `request` 对象复制。
                  format: uuid
                  nullable: true
                  type: string
              required:
                - knowledgeSpaceId
                - query
              type: object
        description: >-
          原样发送 [创建查询令牌](/zh/api-reference/queries/create-query-token) 返回的
          `request` 对象。包含未知字段时返回 `400`。


          标记为 nullable 的可选字段，传 `null` 等同于省略。


          规范化后的请求不得超过 65,536 个 UTF-8 字节，超出时返回 `413`。参见
          [请求大小限制](/zh/api-reference/guides/knowledge-base#request-size-limits)。
        required: true
      responses:
        '200':
          content:
            text/event-stream:
              schema:
                description: >-
                  每一帧包含一行 `event:`（事件名）和一行 `data:`（一个 JSON 对象）。按 `event:`
                  行分发事件，JSON 中没有 `event` 字段。


                  客户端的读取超时要足以覆盖你所用的最慢检索模式。API
                  本身没有读取超时，也不发送保活事件。不能因客户端超时而中断的工作，改用
                  [创建研究任务](/zh/api-reference/research-tasks/create-research-task)。


                  流式调用流程详见
                  [查询知识库](/zh/api-reference/guides/knowledge-base#query-the-knowledge-base)。


                  **事件**


                  - `answer.delta`：回答的一个片段。
                    - `delta`：要追加的文本。
                    - `traceId`：查询记录 ID。
                  - `answer.done`：回答已完成，查询记录已保存。
                    - `finishReason`：找到分段时为 `retrieval-evidence`，没有任何匹配时为 `no-retrieval-evidence`。
                    - `metadata`：键不固定的对象。在 `fast`、`deep`、`research` 深度下均包含 `mode` 和 `evidenceBundle`，结果为 `retrieval-evidence` 时包含 `citations`，还可能包含本次运行产生的其他键。`evidenceBundle.items` 包含检索到的分段及其文本和分数。

                      如需事后查看这些分段，调用 [获取查询分段列表](/zh/api-reference/query-records/list-query-chunks)。它会分页返回，并标出所属文档此后已被删除的分段。
                    - `traceId`：查询记录 ID。
                  - `answer.error`：流建立后查询失败。
                    - `code`：失败类型，取值之一：
                      - `RETRIEVAL_EXECUTION_LEASE_LOST`：知识库进入删除流程，或本次运行已过期。重新运行查询。
                      - `KNOWLEDGE_FS_UNAVAILABLE`：生成回答或保存查询记录时发生的其他任何失败。
                    - `error`：错误消息。
                    - `failure`：错误详情，包括 `category`、`code`、`message`、`retryPolicy`、`stage`、`traceId`，以及可选的 `action`。
                      - `stage`：始终为 `query_generation`。
                    - `traceId`：查询记录 ID。

                  **`citations`**


                  每个支撑回答的检索分段对应一条：


                  - `nodeId`：分段 ID。

                  - `label`：`node:<nodeId>`。

                  - `documentAssetId`：分段所属的文档文件。

                  - `sectionPath`：大纲路径。

                  - `pageNumber`：页码，从 1 开始，源文件提供页码信息时返回，否则省略。

                  - `projectionIds`：参与本次匹配的检索索引 ID。识别源文件请使用 `documentAssetId`。

                  - `sources`：找到该分段的检索方式：`dense`、`fts`、`pageindex` 或 `visual`。


                  图片引用可能包含
                  `multimodalCandidate.assetRef.analysisUnavailable.reason`。出现该字段表示图片分析因资源限制或图片处理失败而被跳过，相关文本仍可能计入结果。


                  `evidenceBundle.items[].citations[]` 还可能带有 `startOffset` 和
                  `endOffset`。它们是解析后文档文本中的 UTF-8 字节偏移量，从 0
                  开始计数，包含起点、不包含终点。展示引用时使用返回的分段文本，不要按这些偏移量截取原始文件。


                  **不同结果下的结束顺序**


                  - 成功：零个或多个 `answer.delta`，然后是一个 `answer.done`，随后流关闭。

                  - 失败：以 `answer.error` 代替
                  `answer.done`，随后流关闭。在此之前可能已发送部分片段，HTTP 状态码仍为
                  `200`。将其视为最终结果，如需重试，重新发起查询。

                  - 客户端断开连接：本次运行被取消，流结束。


                  **回答内容**：在 `fast` 和 `deep` 深度下，由一个 `answer.delta`
                  携带检索分段的汇总（`Retrieval evidence answer:` 后接带编号的章节路径及其文本）。在
                  `research` 深度下，综合生成的回答以一个或多个片段流式返回。没有任何匹配时，唯一的片段是 `I could not
                  find evidence for that query in the indexed retrieval
                  projections.`，且 `finishReason` 为 `no-retrieval-evidence`。


                  在所有深度下，回答最长 2,000 个字符。回答达到这个长度，并不能说明它是否被截断。


                  一次成功的运行：


                  ```text

                  event: answer.delta

                  data: {"delta":"Retrieval evidence answer:\n1. Policies /
                  Refunds: Refund window extended from 14 to 30 days for annual
                  plans.","traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"}


                  event: answer.done

                  data:
                  {"finishReason":"retrieval-evidence","metadata":{"mode":"fast","generator":"hybrid-query","topScore":0.83,"citations":[{"documentAssetId":"b3f4c8d2-6a1e-4f7b-9c0d-2e3f4a5b6c7d","label":"node:9f2e6b41-7c3d-4a58-b0e9-1d2c3f4a5b6e","nodeId":"9f2e6b41-7c3d-4a58-b0e9-1d2c3f4a5b6e","pageNumber":4,"projectionIds":["c7a0d5b2-3e14-4f89-a6b7-0c1d2e3f4a5b"],"sectionPath":["Policies","Refunds"],"sources":["dense","fts"]}],"evidenceBundle":{"createdAt":"2026-09-06T08:31:12Z","id":"e4b2f7a0-1c3d-4e58-9f6a-0b1c2d3e4f5a","items":[{"citations":[{"documentAssetId":"b3f4c8d2-6a1e-4f7b-9c0d-2e3f4a5b6c7d","documentVersion":1,"pageNumber":4,"sectionPath":["Policies","Refunds"]}],"conflicts":[],"freshness":{"status":"fresh"},"metadata":{},"nodeId":"9f2e6b41-7c3d-4a58-b0e9-1d2c3f4a5b6e","score":0.83,"scores":{"final":0.83,"retrieval":0.71},"text":"Refund
                  window extended from 14 to 30 days for annual
                  plans."}],"missingEvidence":[],"query":"What changed in the
                  refund policy this
                  quarter?","state":"answerable","traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"}},"traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"}


                  ```


                  流建立后失败：


                  ```text

                  event: answer.error

                  data: {"code":"KNOWLEDGE_FS_UNAVAILABLE","error":"Agent
                  Knowledge Base is temporarily unavailable. Try again
                  later.","failure":{"action":"retry","category":"dependency","code":"KNOWLEDGE_FS_UNAVAILABLE","message":"Agent
                  Knowledge Base is temporarily unavailable. Try again
                  later.","retryPolicy":"manual","stage":"query_generation","traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"},"traceId":"6c1f0b2e-9d3a-4f5b-8c7d-0e1f2a3b4c5d"}


                  ```
                type: string
          description: >-
            流已建立。无论请求发送的 `Accept` 是什么，后续事件都以 `text/event-stream` 返回。流建立之前，按 HTTP
            错误处理失败；流建立之后，即使 HTTP 状态码为 `200`，收到 `answer.error` 事件也应按失败处理。
          headers:
            x-query-run-id:
              description: >-
                查询记录 ID（UUID），可用于
                [获取查询记录](/zh/api-reference/query-records/get-query-record)。它与每个事件中的
                `traceId` 相同，也是
                [获取查询记录列表](/zh/api-reference/query-records/list-query-records)
                中该记录的 `id`。流建立之前就失败的请求不带运行 ID。
              schema:
                type: string
            x-session-id:
              description: >-
                查询会话 ID（UUID）：即你发送的 `sessionId`，或为本次查询新开的会话。之后调用
                [创建查询令牌](/zh/api-reference/queries/create-query-token) 时将其作为
                `sessionId` 发送，即可沿用该会话。
              schema:
                type: string
        '400':
          content:
            application/json:
              examples:
                KNOWLEDGE_FS_INVALID_REQUEST:
                  summary: KNOWLEDGE_FS_INVALID_REQUEST
                  value:
                    code: KNOWLEDGE_FS_INVALID_REQUEST
                    error: The Agent Knowledge Base request is invalid.
                    failure:
                      category: validation
                      code: KNOWLEDGE_FS_INVALID_REQUEST
                      message: The Agent Knowledge Base request is invalid.
                      retryPolicy: never
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
                RETRIEVAL_PROFILE_SCORE_THRESHOLD_REQUIRES_RERANK:
                  summary: RETRIEVAL_PROFILE_SCORE_THRESHOLD_REQUIRES_RERANK
                  value:
                    code: RETRIEVAL_PROFILE_SCORE_THRESHOLD_REQUIRES_RERANK
                    error: The Agent Knowledge Base request is invalid.
                    failure:
                      category: validation
                      code: KNOWLEDGE_FS_INVALID_REQUEST
                      message: The Agent Knowledge Base request is invalid.
                      retryPolicy: never
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
                    mode: fast
                knowledge_fs_invalid_request:
                  summary: knowledge_fs_invalid_request (X-Trace-ID)
                  value:
                    code: knowledge_fs_invalid_request
                    message: Agent Knowledge Base request is invalid.
                    status: 400
                knowledge_fs_invalid_request_body:
                  summary: knowledge_fs_invalid_request (body validation)
                  value:
                    code: knowledge_fs_invalid_request
                    message: Agent Knowledge Base request is invalid.
                    status: 400
                    violations:
                      - field:
                          - knowledgeSpaceId
                        type: missing
          description: >-
            Dify 返回的标准错误响应体：


            - `knowledge_fs_invalid_request`：`X-Trace-ID` 缺失、超过 255 字节，或包含
            NUL、CR、LF 字符。此响应体不含字段详情。

            - `knowledge_fs_invalid_request`：请求体包含未知键、缺少 `knowledgeSpaceId`，或违反了
            [创建查询令牌](/zh/api-reference/queries/create-query-token)
            的某条规则。`violations` 列出每个出错的字段。


            运行查询时返回（包含 `error`、`code`，错误详情位于 `failure`）：


            - `KNOWLEDGE_FS_INVALID_REQUEST`：请求体未通过服务自身的校验：`sessionId` 或某个
            `activeDocumentIds` 条目不是 UUID，或某个 `activeEntityIds` 条目的长度不在 1～200
            个字符之间。响应体还可能带有 `violations`，每项包含 `code` 和 `path`。

            - `RETRIEVAL_PROFILE_SCORE_THRESHOLD_REQUIRES_RERANK`：实际采用的深度为
            `fast` 或 `deep`，而知识库的检索设置启用了分数阈值，却设置了 `rerank.enabled: false`。在 Dify
            中的 **设置** > **检索** 下检查重排序模型配置。响应体还会带有 `mode`。
        '401':
          content:
            application/json:
              examples:
                KNOWLEDGE_FS_ACCESS_DENIED:
                  summary: KNOWLEDGE_FS_ACCESS_DENIED
                  value:
                    code: KNOWLEDGE_FS_ACCESS_DENIED
                    error: >-
                      You do not have permission to perform this Agent Knowledge
                      Base operation.
                    failure:
                      action: contact_admin
                      category: authorization
                      code: KNOWLEDGE_FS_ACCESS_DENIED
                      message: >-
                        You do not have permission to perform this Agent
                        Knowledge Base operation.
                      retryPolicy: never
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
                knowledge_fs_invalid_credential:
                  summary: knowledge_fs_invalid_credential
                  value:
                    code: knowledge_fs_invalid_credential
                    message: Invalid Agent Knowledge Base service credential.
                    status: 401
          description: >-
            Dify 返回的标准错误响应体：


            - `knowledge_fs_invalid_credential`：缺少 `Authorization` 请求头、请求头格式不是
            `Bearer <token>`，或令牌为空、超过 16 KiB、包含空白字符。


            运行查询时返回：


            - `KNOWLEDGE_FS_ACCESS_DENIED`：令牌已过期，或其签名、签发者、受众校验未通过。创建新令牌后再调用。
        '403':
          content:
            application/json:
              examples:
                KNOWLEDGE_FS_ACCESS_DENIED:
                  summary: KNOWLEDGE_FS_ACCESS_DENIED
                  value:
                    code: KNOWLEDGE_FS_ACCESS_DENIED
                    error: >-
                      You do not have permission to perform this Agent Knowledge
                      Base operation.
                    failure:
                      action: contact_admin
                      category: authorization
                      code: KNOWLEDGE_FS_ACCESS_DENIED
                      message: >-
                        You do not have permission to perform this Agent
                        Knowledge Base operation.
                      retryPolicy: never
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
          description: >-
            运行查询时返回：


            - `KNOWLEDGE_FS_ACCESS_DENIED`：`X-Trace-ID` 与令牌中的 `trace_id`
            声明不一致、请求体的 `knowledgeSpaceId` 与令牌不匹配，或令牌是为其他操作签发的。
        '404':
          content:
            application/json:
              examples:
                KNOWLEDGE_FS_NOT_FOUND:
                  summary: KNOWLEDGE_FS_NOT_FOUND
                  value:
                    code: KNOWLEDGE_FS_NOT_FOUND
                    error: The requested Agent Knowledge Base resource was not found.
                    failure:
                      category: not_found
                      code: KNOWLEDGE_FS_NOT_FOUND
                      message: >-
                        The requested Agent Knowledge Base resource was not
                        found.
                      retryPolicy: never
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
                QUERY_IMAGE_NOT_FOUND:
                  summary: QUERY_IMAGE_NOT_FOUND
                  value:
                    code: QUERY_IMAGE_NOT_FOUND
                    error: The requested Agent Knowledge Base resource was not found.
                    failure:
                      category: not_found
                      code: KNOWLEDGE_FS_NOT_FOUND
                      message: >-
                        The requested Agent Knowledge Base resource was not
                        found.
                      retryPolicy: never
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
                not_found:
                  summary: not_found (feature off)
                  value:
                    code: not_found
                    message: >-
                      The requested URL was not found on the server. If you
                      entered the URL manually please check your spelling and
                      try again.
                    status: 404
          description: >-
            Dify 返回的标准错误响应体：


            - `not_found`：当前部署已关闭 Agent 知识库功能。在此接口上，该检查在令牌、`X-Trace-ID`
            和请求体校验之后进行。


            运行查询时返回：


            - `KNOWLEDGE_FS_NOT_FOUND`：知识库已不存在，例如在签发令牌之后被删除。`knowledgeSpaceId`
            与令牌不匹配时返回 `403`。

            - `QUERY_IMAGE_NOT_FOUND`：引用的查询图片不存在，或调用方无权使用。
        '409':
          content:
            application/json:
              examples:
                RETRIEVAL_DELETION_IN_PROGRESS:
                  summary: RETRIEVAL_DELETION_IN_PROGRESS
                  value:
                    code: RETRIEVAL_DELETION_IN_PROGRESS
                    error: >-
                      This Agent Knowledge Base is being deleted and cannot be
                      searched.
                    failure:
                      category: conflict
                      code: RETRIEVAL_DELETION_IN_PROGRESS
                      message: >-
                        This Agent Knowledge Base is being deleted and cannot be
                        searched.
                      retryPolicy: never
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
                RETRIEVAL_EXECUTION_LEASE_LOST:
                  summary: RETRIEVAL_EXECUTION_LEASE_LOST
                  value:
                    code: RETRIEVAL_EXECUTION_LEASE_LOST
                    error: >-
                      The retrieval execution expired before it could finish.
                      Run the query again.
                    failure:
                      action: retry
                      category: conflict
                      code: RETRIEVAL_EXECUTION_LEASE_LOST
                      message: >-
                        The retrieval execution expired before it could finish.
                        Run the query again.
                      retryPolicy: manual
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
          description: >-
            运行查询时返回：


            - `RETRIEVAL_DELETION_IN_PROGRESS`：知识库正在删除，因此拒绝查询。

            -
            `RETRIEVAL_EXECUTION_LEASE_LOST`：流建立之前本次运行已被取消，原因是删除已经开始或运行已过期。重新运行查询。
        '413':
          content:
            application/json:
              examples:
                knowledge_fs_request_too_large:
                  summary: knowledge_fs_request_too_large
                  value:
                    code: knowledge_fs_request_too_large
                    message: Agent Knowledge Base request is too large.
                    status: 413
          description: >-
            Dify 返回的标准错误响应体：


            - `knowledge_fs_request_too_large`：Dify 补上默认值后，请求超过 65,536 个 UTF-8
            字节。参见
            [请求大小限制](/zh/api-reference/guides/knowledge-base#request-size-limits)。
        '500':
          content:
            application/json:
              examples:
                KNOWLEDGE_FS_INTERNAL_ERROR:
                  summary: KNOWLEDGE_FS_INTERNAL_ERROR
                  value:
                    code: KNOWLEDGE_FS_INTERNAL_ERROR
                    error: >-
                      Agent Knowledge Base could not complete the operation. Try
                      again, or contact an administrator with the error
                      reference.
                    failure:
                      action: contact_admin
                      category: internal
                      code: KNOWLEDGE_FS_INTERNAL_ERROR
                      message: >-
                        Agent Knowledge Base could not complete the operation.
                        Try again, or contact an administrator with the error
                        reference.
                      retryPolicy: manual
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
          description: |-
            运行查询时返回：

            - `KNOWLEDGE_FS_INTERNAL_ERROR`：流建立之前查询意外失败。稍候使用新的查询令牌重试。
        '502':
          content:
            application/json:
              examples:
                knowledge_fs_upstream_unavailable:
                  summary: knowledge_fs_upstream_unavailable
                  value:
                    code: knowledge_fs_upstream_unavailable
                    message: Agent Knowledge Base is unavailable.
                    status: 502
          description: >-
            Dify 返回的标准错误响应体：


            - `knowledge_fs_upstream_unavailable`：未能及时收到响应，或收到的响应 API
            无法转发（压缩的响应体、不是事件流的成功响应，或不是 JSON
            的错误响应）。稍候使用新的查询令牌及随之返回的请求体重新开始。这会发起一次新的运行，不会恢复中断的流。
        '503':
          content:
            application/json:
              examples:
                KNOWLEDGE_FS_UNAVAILABLE:
                  summary: KNOWLEDGE_FS_UNAVAILABLE
                  value:
                    code: KNOWLEDGE_FS_UNAVAILABLE
                    error: >-
                      Agent Knowledge Base is temporarily unavailable. Try again
                      later.
                    failure:
                      action: retry
                      category: dependency
                      code: KNOWLEDGE_FS_UNAVAILABLE
                      message: >-
                        Agent Knowledge Base is temporarily unavailable. Try
                        again later.
                      retryPolicy: manual
                      traceId: 3f1e9a7c-2b4d-4e6f-8a90-1b2c3d4e5f60
                knowledge_fs_operation_unavailable:
                  summary: knowledge_fs_operation_unavailable
                  value:
                    code: knowledge_fs_operation_unavailable
                    message: Agent Knowledge Base operation is not available.
                    status: 503
          description: >-
            Dify 返回的标准错误响应体：


            - `knowledge_fs_operation_unavailable`：当前部署不支持运行查询。


            运行查询时返回：


            -
            `KNOWLEDGE_FS_UNAVAILABLE`：知识库尚无法以实际采用的深度回答：还没有已发布的内容、没有生效的嵌入模型，或某个检索索引仍在构建中。检查
            [获取知识库设置](/zh/api-reference/settings/get-knowledge-base-settings) 中的
            `capabilities`，稍后重试。

            - `TIDB_FTS_POSTINGS_NOT_READY`：关键词检索索引仍在准备中。响应体还会带有
            `runState`。稍后重试。
      security:
        - QueryToken: []
components:
  securitySchemes:
    ApiKeyAuth:
      bearerFormat: API_KEY
      description: >-
        每个请求都通过 API Key 认证：`Authorization: Bearer {API_KEY}`。应用接口使用应用 API
        Key，知识库接口使用知识库 API Key（[快速开始](/zh/api-reference/guides/get-started)）。


        API Key 应保存在服务端，切勿嵌入客户端代码。缺失或无效的 Key 会返回 HTTP `401`（`unauthorized`）。
      scheme: bearer
      type: http
    QueryToken:
      bearerFormat: JWT
      description: >-
        [创建查询令牌](/zh/api-reference/queries/create-query-token) 返回的查询令牌，以
        `Authorization: Bearer {token}` 形式发送。须在返回的 `expires_at` 之前使用，该时间最晚为签发后
        60 秒。该令牌只能用于对该知识库调用「运行查询」。


        在同一请求中发送 `X-Trace-ID`，值为「创建查询令牌」返回的 `trace_id`。
      scheme: bearer
      type: http

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.