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

# Reprocess batch documents

> Re-extract documents in a batch.

Re-extracts selected (or all non-terminal) documents, optionally against another published version. Each completed file is a **new batch page charge**.


## OpenAPI

````yaml POST /api/v2/batches/{batch_id}/reprocess/
openapi: 3.1.0
info:
  title: Abacus Docs API
  version: 1.0.0
  description: |
    REST + SSE surface for Abacus Docs on the same host as the web app
    (`https://abacusdocs.com`). Paths are `/api/v2/…`.

    Authenticate with an organisation API key (`sk-abacus-*`) as a Bearer
    token, or an Auth0 access token. Cross-tenant reads return **404**, not
    403. Page-based lists use `?page=` and `?page_size=`.

    Extraction, reprocess, schema tests, and chat turns spend credits.
    See the help centre Credits pages for rates.
  contact:
    name: Abacus Docs
    url: https://abacusdocs.com/contact
servers:
  - url: https://abacusdocs.com
    description: Production (Extract)
security: []
paths:
  /api/v2/batches/{batch_id}/reprocess/:
    post:
      tags:
        - v2/batches
      summary: Reprocess BatchDocuments against a different StructVersion
      description: >-
        Resets the extraction state of selected (or all) ``BatchDocument`` rows
        in a batch and requeues them for processing against a different
        published ``StructVersion``. Each affected doc is treated as a brand-new
        extraction:


        - ``BatchDocument.struct_version`` is repointed at the target   version.

        - ``BatchDocument.status`` flips to PENDING.

        - ``BatchDocument.error_message`` is cleared.

        - The backing ``ChatAttachment.extraction_result`` and  
        ``ChatAttachment.extraction_struct_version`` are cleared so   the next
        Lynx call starts from a clean slate.

        - ``Batch.status`` flips back to PROCESSING and Celery is   requeued.


        Reprocessing is a fresh extraction → fresh credit charge. The response
        includes an estimated cost so the SPA can render a confirmation modal
        before the user clicks confirm.


        Validation:

        - Batch must already have a ``default_struct_version``   (no-schema
        batches return 400).

        - Target version must belong to the batch's parent struct   (404
        otherwise).

        - Deprecated target versions are rejected (400) — deprecation   marks
        the version as decommissioned.

        - 409 when *any* selected doc is currently PROCESSING (the   reset would
        race with the worker thread).
      operationId: v2_batches_reprocess_create
      parameters:
        - in: path
          name: batch_id
          schema:
            type: integer
          required: true
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/BatchReprocessRequestRequest'
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BatchReprocessResponse'
          description: ''
        '400':
          content:
            application/json:
              schema:
                type: object
                additionalProperties: {}
          description: ''
        '404':
          content:
            application/json:
              schema:
                type: object
                additionalProperties: {}
          description: ''
        '409':
          content:
            application/json:
              schema:
                type: object
                additionalProperties: {}
          description: ''
      security:
        - extractApiKey: []
        - bearerAuth: []
        - cookieAuth: []
components:
  schemas:
    BatchReprocessRequestRequest:
      type: object
      description: |-
        Body for POST /api/v2/batches/{batch_id}/reprocess/.

        Resets the extraction state of one or more BatchDocuments and requeues
        them for processing against a different (newer or older) StructVersion.
        Each affected doc is treated as a brand-new extraction — its
        ``ChatAttachment.extraction_result`` and
        ``ChatAttachment.extraction_struct_version`` are cleared so the next
        Lynx call starts from a clean slate.

        Fields:
            version_number: Target ``StructVersion.version_number`` (must
                belong to the batch's parent struct, must NOT be deprecated).
            document_ids: Optional list of ``BatchDocument`` PKs to reprocess.
                When omitted (or empty), every doc in the batch is reprocessed.
                Subset semantics let the user re-extract a single misbehaving
                doc without re-running the whole batch (saves credits).
            failed_only: When ``True``, additionally restrict the reprocess
                scope to ``BatchDocument`` rows in the FAILED state. Combines
                with ``document_ids`` (intersection) when both are supplied.
                Added in Task 7b (extraction-failure-surfacing) so users can
                recover the ~234 historical broken docs that the Task 6
                backfill flips from COMPLETED to FAILED — per-row reprocess at
                that scale is impractical.
      properties:
        version_number:
          type: integer
          minimum: 1
        document_ids:
          type: array
          items:
            type: integer
            minimum: 1
          description: >-
            Optional list of BatchDocument PKs to reprocess. Omit (or send an
            empty list) to reprocess every doc in the batch.
          maxItems: 1024
        failed_only:
          type: boolean
          default: false
          description: >-
            When true, restrict the reprocess scope to FAILED docs only.
            Intersects with ``document_ids`` when both are supplied — useful for
            batch-scale recovery after the extraction-status backfill flips
            historically broken docs to FAILED.
      required:
        - version_number
    BatchReprocessResponse:
      type: object
      description: |-
        Response shape for POST /api/v2/batches/{batch_id}/reprocess/.

        Fields:
            batch: The updated parent batch (status flipped to PROCESSING when
                any doc was reset).
            reprocessed_doc_count: Number of BatchDocuments whose extraction
                state was actually cleared. Zero when ``document_ids`` matched
                no rows (still 200 — the request was valid, it just had no
                effect).
            estimated_credit_cost: Best-effort millicredit estimate for the
                reprocess job. Computed up-front from per-doc page counts so
                the SPA can render a "this will cost ~X credits" confirmation
                *before* the user clicks confirm. The actual deduction happens
                inside ``process_batch`` when each doc finishes.
      properties:
        batch:
          $ref: '#/components/schemas/BatchSummary'
        reprocessed_doc_count:
          type: integer
        estimated_credit_cost:
          type: integer
          description: >-
            Estimated millicredit cost across all reprocessed docs (page_count *
            EXTRACT_MILLICREDITS_PER_PAGE). Informational.
      required:
        - batch
        - estimated_credit_cost
        - reprocessed_doc_count
    BatchSummary:
      type: object
      description: >-
        Compact projection of a Batch row for responses that need to echo

        the parent batch state alongside per-row payloads.


        Currently used by ``BatchReprocessResponse`` so the SPA can update its

        batch header (status pill, default version chip) without a follow-up
        GET.
      properties:
        id:
          type: integer
        name:
          type: string
        status:
          type: string
        total_files:
          type: integer
        succeeded_count:
          type: integer
        failed_count:
          type: integer
        default_version_number:
          type:
            - integer
            - 'null'
          description: >-
            Version number of Batch.default_struct_version, or null for plain
            batches.
      required:
        - default_version_number
        - failed_count
        - id
        - name
        - status
        - succeeded_count
        - total_files
  securitySchemes:
    extractApiKey:
      type: http
      scheme: bearer
      bearerFormat: API Key
      description: |
        Organisation API key from Settings → API / `/api/keys/`. The secret is
        shown once. Send `Authorization: Bearer sk-abacus-…`. One active key
        per organisation. Ingest (`/api/v2/ingest/*`) refuses API keys — that
        surface is Auth0-only with `ingest:read` / `ingest:write` scopes.
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: >
        Auth0 access token for a signed-in user. Send `Authorization: Bearer
        <token>`.

        The SPA and desktop agent use this. Prefer an organisation API key for

        server-to-server integrations.
    cookieAuth:
      type: apiKey
      in: cookie
      name: sessionid
      description: |
        Django session cookie from a browser login. Present so the web app can
        call `/api/v2` without a header. Do not rely on this for integrations.

````