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

# Upload a document into a knowledge base

> Register an uploaded document and dispatch it for indexing.

The HTTP connection covers only the upload phase: the request body
is streamed into the blob store, a ``pending`` document record is
persisted, the indexing task is dispatched, and the response is
returned.  Parsing / chunking / embedding happen asynchronously in
a worker; the client tracks progress via
:func:`list_knowledge_document_status`.

Args:
    knowledge_base_id (`str`):
        The knowledge base to receive the document.
    file (`UploadFile`):
        The uploaded file (multipart/form-data).
    content_type (`str | None`, optional):
        Override the IANA media type used to route the upload.
    user_id (`str`):
        Injected authenticated user ID.
    service (`KnowledgeBaseService`):
        Injected knowledge base service.

Returns:
    `UploadKnowledgeDocumentResponse`:
        The server-assigned document id, filename, and the
        initial lifecycle state (always ``"pending"``).



## OpenAPI

````yaml /versions/2.0.4dev/en/deploy/openapi.json post /knowledge_bases/{knowledge_base_id}/documents
openapi: 3.1.0
info:
  title: AgentScope
  version: 2.0.3
servers: []
security: []
paths:
  /knowledge_bases/{knowledge_base_id}/documents:
    post:
      tags:
        - knowledge_bases
      summary: Upload a document into a knowledge base
      description: |-
        Register an uploaded document and dispatch it for indexing.

        The HTTP connection covers only the upload phase: the request body
        is streamed into the blob store, a ``pending`` document record is
        persisted, the indexing task is dispatched, and the response is
        returned.  Parsing / chunking / embedding happen asynchronously in
        a worker; the client tracks progress via
        :func:`list_knowledge_document_status`.

        Args:
            knowledge_base_id (`str`):
                The knowledge base to receive the document.
            file (`UploadFile`):
                The uploaded file (multipart/form-data).
            content_type (`str | None`, optional):
                Override the IANA media type used to route the upload.
            user_id (`str`):
                Injected authenticated user ID.
            service (`KnowledgeBaseService`):
                Injected knowledge base service.

        Returns:
            `UploadKnowledgeDocumentResponse`:
                The server-assigned document id, filename, and the
                initial lifecycle state (always ``"pending"``).
      operationId: >-
        upload_knowledge_document_knowledge_bases__knowledge_base_id__documents_post
      parameters:
        - name: knowledge_base_id
          in: path
          required: true
          schema:
            type: string
            description: The knowledge base id.
            title: Knowledge Base Id
          description: The knowledge base id.
        - name: x-user-id
          in: header
          required: true
          schema:
            type: string
            description: >-
              Caller's user ID. Temporary header-based identity; will be
              replaced by JWT auth.
            title: X-User-Id
          description: >-
            Caller's user ID. Temporary header-based identity; will be replaced
            by JWT auth.
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              $ref: >-
                #/components/schemas/Body_upload_knowledge_document_knowledge_bases__knowledge_base_id__documents_post
      responses:
        '201':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UploadKnowledgeDocumentResponse'
        '404':
          description: Not found
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
components:
  schemas:
    Body_upload_knowledge_document_knowledge_bases__knowledge_base_id__documents_post:
      properties:
        file:
          type: string
          contentMediaType: application/octet-stream
          title: File
          description: The document to index (PDF, TXT, Markdown, …).
        content_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Content Type
          description: >-
            Override the IANA media type used to route the upload. Defaults to
            the type guessed from the filename.
      type: object
      required:
        - file
      title: >-
        Body_upload_knowledge_document_knowledge_bases__knowledge_base_id__documents_post
    UploadKnowledgeDocumentResponse:
      properties:
        document_id:
          type: string
          title: Document Id
          description: Server-assigned document identifier.
        filename:
          type: string
          title: Filename
          description: The original filename of the uploaded document.
        status:
          type: string
          enum:
            - pending
            - parsing
            - chunking
            - indexing
            - ready
            - error
          title: Status
          description: >-
            Lifecycle state immediately after upload — always ``'pending'`` in
            the happy path; surfaced so the client can seed its progress tracker
            without an extra round-trip.
      type: object
      required:
        - document_id
        - filename
        - status
      title: UploadKnowledgeDocumentResponse
      description: Response body after uploading a document into a knowledge base.
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
        input:
          title: Input
        ctx:
          type: object
          title: Context
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError

````