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

# Retrieve Chunks Grouped

> Retrieve relevant chunks with grouped response format.

Uses the same filter operators as `/retrieve/chunks` (equality, `$eq/$ne`, `$gt/$gte/$lt/$lte`, `$in/$nin`,
`$exists`, `$type`, `$regex`, `$contains`, and the logical `$and/$or/$nor/$not`), with arbitrary nesting
supported inside `request.filters`.

Returns both flat results (for backward compatibility) and grouped results (for UI).
When padding > 0, groups chunks by main matches and their padding chunks.



## OpenAPI

````yaml https://app.stainless.com/api/spec/documented/morphik/openapi.documented.yml post /retrieve/chunks/grouped
openapi: 3.1.0
info:
  title: FastAPI
  version: 0.1.0
servers: []
security: []
paths:
  /retrieve/chunks/grouped:
    post:
      summary: Retrieve Chunks Grouped
      description: >-
        Retrieve relevant chunks with grouped response format.


        Uses the same filter operators as `/retrieve/chunks` (equality,
        `$eq/$ne`, `$gt/$gte/$lt/$lte`, `$in/$nin`,

        `$exists`, `$type`, `$regex`, `$contains`, and the logical
        `$and/$or/$nor/$not`), with arbitrary nesting

        supported inside `request.filters`.


        Returns both flat results (for backward compatibility) and grouped
        results (for UI).

        When padding > 0, groups chunks by main matches and their padding
        chunks.
      operationId: retrieve_chunks_grouped_retrieve_chunks_grouped_post
      parameters:
        - name: authorization
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: Authorization
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RetrieveRequest'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GroupedChunkResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      x-codeSamples:
        - lang: JavaScript
          source: |-
            import Morphik from 'morphik';

            const client = new Morphik({
              apiKey: process.env['MORPHIK_API_KEY'], // This is the default and can be omitted
            });

            const response = await client.retrieve.chunks.createGrouped();

            console.log(response.chunks);
        - lang: cURL
          source: |-
            curl https://api.morphik.ai/retrieve/chunks/grouped \
                -H 'Content-Type: application/json' \
                -H "Authorization: Bearer $MORPHIK_API_KEY" \
                -d '{}'
components:
  schemas:
    RetrieveRequest:
      properties:
        query:
          anyOf:
            - type: string
              minLength: 1
            - type: 'null'
          title: Query
          description: >-
            Natural-language query used to retrieve relevant chunks or
            documents.
        query_image:
          anyOf:
            - type: string
            - type: 'null'
          title: Query Image
          description: >-
            Base64-encoded image to use as query for Morphik multimodal
            retrieval. Requires use_colpali=True. Mutually exclusive with
            'query'.
        filters:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Filters
          description: >-
            Metadata filters supporting logical operators ($and/$or/$not/$nor)
            and field predicates
            ($eq/$ne/$gt/$gte/$lt/$lte/$in/$nin/$exists/$type/$regex/$contains).
        k:
          type: integer
          exclusiveMinimum: 0
          title: K
          description: Maximum number of chunks or documents to return.
          default: 4
        min_score:
          type: number
          title: Min Score
          description: Minimum similarity score a result must meet before it is returned.
          default: 0
        use_reranking:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Use Reranking
          description: >-
            When provided, overrides the workspace reranking configuration for
            this request.
        use_colpali:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Use Colpali
          description: >-
            When provided, uses Morphik's finetuned ColPali style embeddings
            (recommended to be True for high quality retrieval).
        output_format:
          anyOf:
            - $ref: '#/components/schemas/OutputFormat'
            - type: 'null'
          description: >-
            How to return image chunks: base64 (default), url, or text (markdown
            format)
          default: base64
        padding:
          type: integer
          minimum: 0
          title: Padding
          description: >-
            Number of additional chunks/pages to retrieve before and after
            matched chunks (ColPali only)
          default: 0
        folder_name:
          anyOf:
            - type: string
            - items:
                type: string
              type: array
            - type: 'null'
          title: Folder Name
          description: >-
            Optional folder scope. Accepts a folder PATH (e.g.,
            '/Company/Reports') or list of paths.
        folder_depth:
          anyOf:
            - type: integer
            - type: 'null'
          title: Folder Depth
          description: >-
            Folder scope depth. 0 or None = exact folder only, -1 = include all
            descendants, n > 0 = include descendants up to n levels deeper.
        end_user_id:
          anyOf:
            - type: string
            - type: 'null'
          title: End User Id
          description: Optional end-user scope for the operation
      type: object
      title: RetrieveRequest
      description: Base retrieve request model
    GroupedChunkResponse:
      properties:
        chunks:
          items:
            $ref: '#/components/schemas/ChunkResult'
          type: array
          title: Chunks
          description: Flat list of all chunks (for backward compatibility)
        groups:
          items:
            $ref: '#/components/schemas/ChunkGroup'
          type: array
          title: Groups
          description: Grouped chunks for UI display
        total_results:
          type: integer
          title: Total Results
          description: Total number of unique chunks
        has_padding:
          type: boolean
          title: Has Padding
          description: Whether padding was applied to any results
      type: object
      required:
        - chunks
        - groups
        - total_results
        - has_padding
      title: GroupedChunkResponse
      description: Response that includes both flat results and grouped results for UI
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    OutputFormat:
      type: string
      enum:
        - base64
        - url
        - text
      title: OutputFormat
      description: Output format for image chunks in retrieval results.
    ChunkResult:
      properties:
        content:
          type: string
          title: Content
        score:
          type: number
          title: Score
        document_id:
          type: string
          title: Document Id
        chunk_number:
          type: integer
          title: Chunk Number
        metadata:
          additionalProperties: true
          type: object
          title: Metadata
        content_type:
          type: string
          title: Content Type
        filename:
          anyOf:
            - type: string
            - type: 'null'
          title: Filename
        download_url:
          anyOf:
            - type: string
            - type: 'null'
          title: Download Url
        is_padding:
          type: boolean
          title: Is Padding
          description: Whether this chunk was added as padding
          default: false
      type: object
      required:
        - content
        - score
        - document_id
        - chunk_number
        - metadata
        - content_type
      title: ChunkResult
      description: Query result at chunk level
    ChunkGroup:
      properties:
        main_chunk:
          $ref: '#/components/schemas/ChunkResult'
        padding_chunks:
          items:
            $ref: '#/components/schemas/ChunkResult'
          type: array
          title: Padding Chunks
        total_chunks:
          type: integer
          title: Total Chunks
          description: Total number of chunks in this group
      type: object
      required:
        - main_chunk
        - total_chunks
      title: ChunkGroup
      description: 'Represents a group of chunks: one main match + its padding chunks'
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError

````