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

# ドキュメントにチャンクを追加

> ドキュメント内に 1 つ以上のチャンクを作成します。各チャンクには `content` が必須です。Q&A モード（`qa_model`）ドキュメントの場合は `answer` も必須です。`keywords` は任意です。



## OpenAPI

````yaml /ja/api-reference/openapi_service.json post /datasets/{dataset_id}/documents/{document_id}/segments
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}/segments:
    post:
      tags:
        - チャンク
      summary: ドキュメントにチャンクを追加
      description: >-
        ドキュメント内に 1 つ以上のチャンクを作成します。各チャンクには `content` が必須です。Q&A
        モード（`qa_model`）ドキュメントの場合は `answer` も必須です。`keywords` は任意です。
      operationId: createSegments
      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: Document ID.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - segments
              properties:
                segments:
                  type: array
                  items:
                    type: object
                    properties:
                      content:
                        type: string
                        description: チャンクのテキスト内容です。
                        minLength: 1
                      answer:
                        type: string
                        description: QA モードの回答内容です。
                      keywords:
                        type: array
                        items:
                          type: string
                        description: チャンクのキーワードです。
                      attachment_ids:
                        type: array
                        items:
                          type: string
                        description: 添付ファイル ID です。
                    required:
                      - content
                  description: 作成するチャンクオブジェクトの配列です。
                  minItems: 1
      responses:
        '200':
          description: チャンクが正常に作成されました。
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Segment'
                    description: 作成されたチャンクのリスト。
                  doc_form:
                    type: string
                    description: このドキュメントが使用するドキュメントチャンキングモードです。
              examples:
                success:
                  summary: レスポンス例
                  value:
                    data:
                      - id: f3d1c7be-9f3a-40d8-8eb8-3a1ef9c3f2c1
                        position: 1
                        document_id: a8e0e5b5-78c6-4130-a5ce-25feb0e0b4ac
                        content: Dify is an open-source LLM app development platform.
                        sign_content: ''
                        answer: ''
                        word_count: 9
                        tokens: 12
                        keywords:
                          - dify
                          - platform
                          - llm
                        index_node_id: a1b2c3d4-e5f6-7890-abcd-000000000001
                        index_node_hash: abc123def456
                        hit_count: 0
                        enabled: true
                        disabled_at: null
                        disabled_by: null
                        status: completed
                        created_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                        created_at: 1741267200
                        updated_at: 1741267200
                        updated_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                        indexing_at: 1741267200
                        completed_at: 1741267200
                        error: null
                        stopped_at: null
                        child_chunks: []
                        attachments: []
                        summary: null
                    doc_form: text_model
        '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_limit:
                  summary: invalid_param (segments limit)
                  value:
                    status: 400
                    code: invalid_param
                    message: Exceeded maximum segments limit of 1000.
        '403':
          description: >-
            - `forbidden` : このナレッジベースでは API アクセスが有効になっていません。

            - `forbidden` : ベクトル空間の容量がサブスクリプションの上限に達しました。

            - `forbidden` : サブスクリプションのナレッジベースリクエストのレート制限に達しました。

            - `forbidden` : To unlock this feature and elevate your Dify
            experience, please upgrade to a paid plan.
          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.
                forbidden_4:
                  summary: forbidden (upgrade plan)
                  value:
                    status: 403
                    code: forbidden
                    message: >-
                      To unlock this feature and elevate your Dify experience,
                      please upgrade to a paid plan.
        '404':
          description: |-
            - `not_found` : ナレッジベースが見つかりません。
            - `not_found` : ドキュメントが見つかりません。
            - `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.
                not_found_3:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Document is not completed.
                not_found_4:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Document is disabled.
components:
  schemas:
    Segment:
      type: object
      properties:
        id:
          type: string
          description: チャンクの一意識別子です。
        position:
          type: integer
          description: ドキュメント内のチャンクの位置。
        document_id:
          type: string
          description: このチャンクが属するドキュメントの ID です。
        content:
          type: string
          description: チャンクのテキスト内容です。
        sign_content:
          type: string
          description: 整合性検証用の署名付きコンテンツハッシュです。
        answer:
          type: string
          description: 回答コンテンツです。Q&A モードのドキュメントで使用されます。
        word_count:
          type: integer
          description: チャンク内容の単語数です。
        tokens:
          type: integer
          description: チャンク内容のトークン数です。
        keywords:
          type: array
          description: キーワードベースの検索のためにこのチャンクに関連付けられたキーワードです。
          items:
            type: string
        index_node_id:
          type: string
          description: ベクトルストア内のインデックスノードの ID です。
        index_node_hash:
          type: string
          description: インデックスされたコンテンツのハッシュです。変更の検出に使用されます。
        hit_count:
          type: integer
          description: このチャンクが検索クエリでマッチした回数です。
        enabled:
          type: boolean
          description: このチャンクが検索に対して有効かどうかです。
        disabled_at:
          type: number
          nullable: true
          description: チャンクが無効化されたタイムスタンプです。有効な場合は `null` です。
        disabled_by:
          type: string
          nullable: true
          description: チャンクを無効化したユーザーの ID です。有効な場合は `null` です。
        status:
          type: string
          description: チャンクの現在のインデックスステータスです（例：`completed`、`indexing`、`error`）。
        created_by:
          type: string
          description: チャンクを作成したユーザーの ID です。
        created_at:
          type: number
          description: 作成タイムスタンプ（Unix エポック、秒単位）です。
        updated_at:
          type: number
          description: 最終更新タイムスタンプ（Unix エポック、秒単位）です。
        updated_by:
          type: string
          description: このチャンクを最後に更新したユーザーの ID です。
        indexing_at:
          type: number
          nullable: true
          description: インデックス作成が開始されたタイムスタンプです。まだ開始されていない場合は `null` です。
        completed_at:
          type: number
          nullable: true
          description: インデックス作成が完了したタイムスタンプです。まだ完了していない場合は `null` です。
        error:
          type: string
          nullable: true
          description: インデックス作成が失敗した場合のエラーメッセージです。エラーなしの場合は `null` です。
        stopped_at:
          type: number
          nullable: true
          description: インデックス作成が停止されたタイムスタンプです。停止されていない場合は `null` です。
        child_chunks:
          type: array
          description: このチャンクに属する子チャンクです。階層モードのドキュメントにのみ存在します。
          items:
            $ref: '#/components/schemas/ChildChunk'
        attachments:
          type: array
          description: このチャンクに添付されたファイルです。
          items:
            type: object
            properties:
              id:
                type: string
                description: 添付ファイルの識別子です。
              name:
                type: string
                description: 元のファイル名です。
              size:
                type: integer
                description: ファイルサイズ（バイト）。
              extension:
                type: string
                description: ファイル拡張子。
              mime_type:
                type: string
                description: ファイルの MIME タイプ。
              source_url:
                type: string
                description: 添付ファイルにアクセスする URL です。
        summary:
          type: string
          nullable: true
          description: AI が生成したチャンクコンテンツの要約です。要約インデックスが有効でない場合は `null` です。
    ChildChunk:
      type: object
      properties:
        id:
          type: string
          description: 子チャンクの一意識別子です。
        segment_id:
          type: string
          description: この子チャンクが属する親チャンクの ID です。
        content:
          type: string
          description: 子チャンクのテキスト内容です。
        position:
          type: integer
          description: 親チャンク内の子チャンクの位置です。
        word_count:
          type: integer
          description: 子チャンク内容の単語数です。
        type:
          type: string
          description: >-
            子チャンクの作成方法です。API から作成または更新された子チャンクは常に `customized` です。システム生成の子チャンクは
            `automatic` です。
        created_at:
          type: number
          description: 作成タイムスタンプ（Unix エポック、秒単位）です。
        updated_at:
          type: number
          description: 最終更新タイムスタンプ（Unix エポック、秒単位）です。
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API_KEY
      description: >-
        API Key 認証です。すべての API リクエストにおいて、`Authorization` HTTP ヘッダーに `Bearer `
        プレフィックスを付けた API Key を含めてください。例：`Authorization: Bearer {API_KEY}`。**API
        Key はサーバーサイドに保存し、クライアントサイドで共有・保存しないことを強く推奨します。API Key
        の漏洩は深刻な結果につながる可能性があります。**API Key が欠落または無効な場合、HTTP `401` とエラーコード
        `unauthorized` が返されます。

````