Chunker Pipeline#
The RAGFlow chunker pipeline is a stage in the document-processing flow (rag/flow/) that takes parser output and splits it into indexable chunks. Two chunker implementations exist under rag/flow/chunker/:
TokenChunker— token-size/delimiter-based splitting with optional overlap and media context.TitleChunker— heading-aware chunking, dispatching toHierarchyTitleChunkerorGroupTitleChunkerbased on configuredmethod.
Both extend ProcessBase / ProcessParamBase from rag/flow/base.py , which wraps _invoke() with timing, error handling, and canvas callback integration.
Upstream Data Contract#
Chunkers receive a single validated Pydantic model, TokenChunkerFromUpstream, containing:
| Field | Alias | Type | Purpose |
|---|---|---|---|
output_format | — | Literal["json","markdown","text","html","chunks"] | Tells the chunker which result field is populated |
json_result | json | list[dict] | Structured parser output (PDF, tables, images) |
markdown_result | markdown | str | Markdown flat text |
text_result | text | str | Plain text |
html_result | html | str | HTML content |
chunks | — | list[dict] | Pre-chunked output from a previous chunker stage |
file | — | dict | File metadata (blob/path) needed for PDF outline extraction |
The schema uses Pydantic field aliases so upstream components can write json, markdown, etc. directly as output keys.
Format Branching in TokenChunker#
TokenChunker._invoke() reads from_upstream.output_format and branches on three paths :
-
Flat text (
markdown/text/html): splits via delimiter regex or falls back tonaive_mergefor token-size merging. Optionalchildren_delimitersapply a secondary split for parent–child chunk relationships (momfield) . -
Structured JSON (
json): normalized through_build_json_chunks(), which mapsdoc_type_kwdtock_type(text/table/image) and extracts PDF position metadata into an internalPDF_POSITIONS_KEYfield. Media chunks then receive surrounding text context via_attach_context_to_media_chunks(). -
One-shot mode (
delimiter_mode = "one"): emits a single merged chunk regardless of size .
The chunker always sets output_format = "chunks" before returning , so the next stage always receives the chunks format.
Format Branching in TitleChunker#
BaseTitleChunker.extract_line_records() normalizes all upstream formats into a uniform list of dicts:
{"text": str, "doc_type_kwd": str, "img_id": str|None, "layout": str, PDF_POSITIONS_KEY: list}
- Flat text formats (
markdown/text/html): split on\n, filtered, and converted to text-only records with empty positions . - Structured formats (
chunks/json): each item's positions are extracted viaextract_pdf_positions()for coordinate-aware downstream processing.
build_chunks_from_record_groups() materializes each group into an output chunk: for flat-text input it concatenates text; for structured input it calls RAGFlowPdfParser.remove_tag() and merge_pdf_positions() to union bounding boxes across records.
PDF Metadata Lifecycle#
PDF coordinate metadata (PDF_POSITIONS_KEY) travels through the pipeline as an internal field and is finalized only at the last step:
- Extraction:
extract_pdf_positions()normalizes any of four raw formats (position tag string,positionslist,_pdf_positions, or individual fields) into a canonical list. - Accumulation: positions are carried on every internal chunk dict and merged when records are grouped.
- Preview generation:
restore_pdf_text_previews()generates thumbnail crops for structured-input chunks and caches them by position key. - Finalization:
finalize_pdf_chunk()converts accumulated positions into the indexedposition_int,page_num_int, andtop_intfields consumed downstream.
This deferred finalization ensures coordinates are only serialized once, at the chunker's output boundary.
Level Resolution in TitleChunker#
BaseTitleChunker.resolve_title_levels() attempts outline-based level assignment first (matches text against PDF bookmark outlines) and falls back to frequency-based regex matching when outline coverage is below 3%. The hierarchy method selects a target level from the resolved set via resolve_target_level(); the group method groups records into sections by title boundaries and token constraints.
Key Files#
| File | Role |
|---|---|
rag/flow/chunker/schema.py | TokenChunkerFromUpstream — the upstream data contract |
rag/flow/chunker/token_chunker.py | TokenChunker implementation |
rag/flow/chunker/title_chunker/common.py | BaseTitleChunker, TitleChunkerParam, shared utilities |
rag/flow/chunker/title_chunker/title_chunker.py | Dispatcher: routes to HierarchyTitleChunker or GroupTitleChunker |
rag/flow/chunker/title_chunker/hierarchy_chunker.py | Tree-based hierarchy chunking |
rag/flow/chunker/title_chunker/group_chunker.py | Section-group chunking with token limits |
rag/flow/base.py | ProcessBase / ProcessParamBase — execution framework |
rag/flow/parser/pdf_chunk_metadata.py | extract_pdf_positions, finalize_pdf_chunk, restore_pdf_text_previews |