> ## 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.

# 更新文档

> 通过上传新文件更新现有文档。这是基于文件更新文档的规范端点。将重新触发索引，使用返回的 `batch` ID 配合 [获取文档嵌入状态（进度）](/zh/api-reference/documents/get-document-indexing-status) 跟踪进度。



## OpenAPI

````yaml /zh/api-reference/openapi_service.json patch /datasets/{dataset_id}/documents/{document_id}
openapi: 3.0.1
info:
  title: Dify 服务 API
  description: 用于 Dify 应用与知识库的 REST API。应用类接口使用应用 API 密钥认证，知识库类接口使用知识库 API 密钥认证。
  version: 1.0.0
servers:
  - url: https://{api_base_url}
    description: Dify 服务 API 的基础 URL。自部署时，替换为你的 API 基础 URL。
    variables:
      api_base_url:
        default: api.dify.ai/v1
        description: API 基础 URL 的主机与路径，不含 `https://` 前缀。
security:
  - ApiKeyAuth: []
tags:
  - 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: 用于管理和运行知识流水线的操作，包括数据源插件和流水线执行。
paths:
  /datasets/{dataset_id}/documents/{document_id}:
    patch:
      tags:
        - 文档
      summary: 更新文档
      description: >-
        通过上传新文件更新现有文档。这是基于文件更新文档的规范端点。将重新触发索引，使用返回的 `batch` ID 配合
        [获取文档嵌入状态（进度）](/zh/api-reference/documents/get-document-indexing-status)
        跟踪进度。
      operationId: updateDocument
      parameters:
        - name: dataset_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
          description: 知识库 ID。
        - name: document_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
          description: 文档 ID.
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
                  description: 要上传的文件。
                data:
                  type: string
                  description: >-
                    包含配置信息的 JSON 字符串。接受与
                    [从文本创建文档](/zh/api-reference/documents/create-document-by-text)
                    相同的字段（`indexing_technique`、`doc_form`、`doc_language`、`process_rule`、`retrieval_model`、`embedding_model`、`embedding_model_provider`），但不包括
                    `name` 和 `text`。
                  example: >-
                    {"indexing_technique":"high_quality","doc_form":"text_model","doc_language":"English","process_rule":{"mode":"automatic"}}
      responses:
        '200':
          description: 文档更新成功。
          content:
            application/json:
              schema:
                type: object
                properties:
                  document:
                    $ref: '#/components/schemas/Document'
                  batch:
                    type: string
                    description: 用于跟踪索引进度的批次 ID。
              examples:
                success:
                  summary: 响应示例
                  value:
                    document:
                      id: a8e0e5b5-78c6-4130-a5ce-25feb0e0b4ac
                      position: 1
                      data_source_type: upload_file
                      data_source_info:
                        upload_file_id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
                      data_source_detail_dict:
                        upload_file:
                          id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
                          name: guide.txt
                          size: 2048
                          extension: txt
                          mime_type: text/plain
                          created_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                          created_at: 1741267200
                      dataset_process_rule_id: e1f2a3b4-c5d6-7890-ef12-345678901234
                      name: guide.txt
                      created_from: api
                      created_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                      created_at: 1741267200
                      tokens: 512
                      indexing_status: completed
                      error: null
                      enabled: true
                      disabled_at: null
                      disabled_by: null
                      archived: false
                      display_status: available
                      word_count: 350
                      hit_count: 0
                      doc_form: text_model
                      doc_metadata: []
                      summary_index_status: null
                      need_summary: false
                    batch: '20250306150245647595'
        '400':
          description: >-
            - `provider_not_initialize` : 未找到有效的模型供应商凭据。前往 Settings -> Model
            Provider 补全供应商凭据。

            - `invalid_param` : 文档不可用，无法更新（仅可更新处于可用状态的文档）。
          content:
            application/json:
              examples:
                provider_not_initialize:
                  summary: provider_not_initialize
                  value:
                    status: 400
                    code: provider_not_initialize
                    message: >-
                      No valid model provider credentials found. Please go to
                      Settings -> Model Provider to complete your provider
                      credentials.
                invalid_param_not_available:
                  summary: invalid_param (not available)
                  value:
                    status: 400
                    code: invalid_param
                    message: Document is not available
        '403':
          description: |-
            - `forbidden` : 该知识库未启用 API 访问。
            - `forbidden` : 向量空间容量已达到您订阅套餐的上限。
            - `forbidden` : 已达到您订阅套餐的知识库请求速率限制。
          content:
            application/json:
              examples:
                forbidden_1:
                  summary: forbidden (api access)
                  value:
                    status: 403
                    code: forbidden
                    message: Dataset api access is not enabled.
                forbidden_2:
                  summary: forbidden (vector space)
                  value:
                    status: 403
                    code: forbidden
                    message: >-
                      The capacity of the vector space has reached the limit of
                      your subscription.
                forbidden_3:
                  summary: forbidden (rate limit)
                  value:
                    status: 403
                    code: forbidden
                    message: >-
                      Sorry, you have reached the knowledge base request rate
                      limit of your subscription.
        '404':
          description: |-
            - `not_found` : 未找到知识库。
            - `not_found` : 未找到文档。
          content:
            application/json:
              examples:
                not_found_1:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Dataset not found.
                not_found_2:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Document not found.
        '413':
          description: '`file_too_large` : 文件大小超出限制。'
          content:
            application/json:
              examples:
                file_too_large:
                  summary: file_too_large
                  value:
                    status: 413
                    code: file_too_large
                    message: File size exceeded.
        '415':
          description: '`unsupported_file_type` : 不支持的文件类型。'
          content:
            application/json:
              examples:
                unsupported_file_type:
                  summary: unsupported_file_type
                  value:
                    status: 415
                    code: unsupported_file_type
                    message: File type not allowed.
components:
  schemas:
    Document:
      type: object
      properties:
        id:
          type: string
          description: 文档的唯一标识符。
        position:
          type: integer
          description: 文档在列表中的显示位置。
        data_source_type:
          type: string
          description: 文档的创建方式。文件上传为 `upload_file`，Notion 导入为 `notion_import`。
        data_source_info:
          type: object
          description: 原始数据源信息，随 `data_source_type` 而异。
        data_source_detail_dict:
          type: object
          description: 详细的数据源信息，包括文件详情。
        dataset_process_rule_id:
          type: string
          description: 应用于该文档的处理规则 ID。
        name:
          type: string
          description: 文档名称。
        created_from:
          type: string
          description: 文档来源。通过 API 创建时为 `api`，通过 UI 创建时为 `web`。
        created_by:
          type: string
          description: 创建该文档的用户 ID。
        created_at:
          type: number
          description: 创建时间戳（Unix 纪元，单位为秒）。
        tokens:
          type: integer
          description: 文档中的令牌总数。
        indexing_status:
          type: string
          description: >-
            当前索引状态。`waiting` 表示排队中，`parsing` 表示正在提取内容，`cleaning`
            表示正在去噪，`splitting` 表示正在分块，`indexing` 表示正在构建向量，`completed`
            表示就绪，`error` 表示失败，`paused` 表示手动暂停。
        error:
          type: string
          nullable: true
          description: 索引失败时的错误消息。无错误时为 `null`。
        enabled:
          type: boolean
          description: 该文档是否启用检索。
        disabled_at:
          type: number
          nullable: true
          description: 文档被禁用的时间戳。启用时为 `null`。
        disabled_by:
          type: string
          nullable: true
          description: 禁用该文档的用户 ID。启用时为 `null`。
        archived:
          type: boolean
          description: 文档是否已归档。
        display_status:
          type: string
          description: 基于 `indexing_status` 和 `enabled` 状态派生的面向用户的显示状态。
        word_count:
          type: integer
          description: 文档的总字数。
        hit_count:
          type: integer
          description: 该文档在检索查询中被匹配的次数。
        doc_form:
          type: string
          description: >-
            文档分块模式。`text_model` 表示标准文本分块，`hierarchical_model` 表示父子结构，`qa_model`
            表示问答对提取。
        doc_metadata:
          type: array
          description: 分配给该文档的元数据值。
          items:
            type: object
            properties:
              id:
                type: string
                description: 元数据字段标识符。
              name:
                type: string
                description: 元数据字段名称。
              type:
                type: string
                description: 元数据字段值类型。
              value:
                type: string
                description: 此文档的元数据值。
        summary_index_status:
          type: string
          nullable: true
          description: 该文档的摘要索引状态。未配置摘要索引时为 `null`。
        need_summary:
          type: boolean
          description: 是否需要为该文档生成摘要。
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API_KEY
      description: >-
        API Key 认证。对于所有 API 请求，请在 `Authorization` HTTP 头中包含您的 API Key，并加上
        `Bearer ` 前缀。示例：`Authorization: Bearer {API_KEY}`。**强烈建议将 API Key
        存储在服务端，不要在客户端共享或存储，以避免 API Key 泄漏导致严重后果。**缺少或无效的 API Key 会返回 HTTP
        `401`，错误码为 `unauthorized`。

````