Building a Unified Preprocessing Pipeline for Multi-Source LLM Knowledge Bases
Preparing reliable knowledge is often more time-consuming than calling an LLM. This article explains how to build a unified data layer for heterogeneous sources such as web pages, code repositories, PDFs, wikis, and discussion records. It covers modular pipeline design, structured intermediate representations, metadata enrichment, incremental updates, PDF and OCR processing, and semantic chunking for knowledge packs and search indexes.
1. Background: Knowledge Preparation Takes More Time Than Model Invocation
In enterprise environments and personal projects, the information an LLM needs to answer questions is often distributed across many formats. Official technical documentation, team code repositories, product requirement PDFs, internal wikis, and historical discussion records all have different structures, update cycles, and access controls.
An LLM’s training data has a cutoff date, while framework versions, API specifications, and internal engineering guidelines continue to change. Relying solely on the model’s built-in knowledge therefore cannot guarantee accurate answers. The latest source material must be provided in a searchable and citable form.
A traditional implementation usually assembles the workflow manually: write crawlers to retrieve web pages, run OCR on scanned PDFs, split long documents by hand, call an embedding API to generate vectors, and finally maintain a prompt or instruction file that may contain hundreds of lines. Each stage depends on custom code. When any data source changes, the entire pipeline may require rework. In real-world projects, preprocessing can easily take several days.
2. Breaking Down the Core Challenges
Turning multi-source material into a usable knowledge foundation requires solving six categories of problems:
- Parsing heterogeneous sources: Web pages, code, office documents, spreadsheets, and image-based PDFs have very different structures. Each source type requires suitable parsing logic.
- Preserving structure: If heading hierarchies, lists, tables, or code blocks are flattened, their meaning can be distorted and the model may no longer understand the surrounding context correctly.
- Maintaining chunk quality: If a chunk boundary falls in the middle of a code block, the retrieved content may not be understandable on its own.
- Preserving complete metadata: Without information such as the source, version, update time, and section path, search results are difficult to verify and trace back to their origin.
- Supporting incremental updates: Rebuilding everything is expensive. The pipeline should identify which documents have changed and process only those items.
- Adapting outputs: The same processed data may need to support instruction-oriented knowledge packages, vector indexes, or keyword indexes, each with different format requirements.
3. Designing a Unified Data Layer
A practical approach is to divide data processing into independent stages. Each stage should depend only on the standardized output of the previous stage. This allows a data source or output format to be replaced without modifying the rest of the pipeline.
Source -> Connector -> Parser -> Normalizer -> Chunker -> Metadata Enrichment -> Output
The responsibilities of each stage are:
- Connectors handle authentication, pagination, rate limiting, and incremental retrieval.
- Parsers convert raw formats into a unified intermediate representation.
- Normalizers remove noise such as navigation bars and footers, merge duplicated content, and standardize encoding and line breaks.
- Chunkers divide content according to semantic boundaries.
- Metadata enrichment adds fields such as source, version, and access-control labels.
- Outputs write data to the target storage system or generate the required delivery format.
The unified intermediate representation is the key to this design. The following example is illustrative; the field names are provided for demonstration:
{"doc_id": "eng-wiki-000123","source_type": "wiki","title": "Release Process Guide","section_path": ["Release Process", "Rollback Steps"],"content_blocks": [{"type": "heading", "level": 2, "text": "Rollback Steps"},{"type": "paragraph", "text": "Before rolling back, verify database compatibility with the target version."},{"type": "code", "lang": "bash", "text": "deploy --rollback --version v2.3.1"}],"version": "2026-10-01","updated_at": "2026-11-20T09:00:00Z","acl": ["engineering"]}
Storing documents as content blocks allows the chunker to recognize code blocks, tables, and other atomic units, preventing them from being split incorrectly.
Configuration should also be managed consistently. The following is an example configuration:
sources:- name: engineering-wikitype: wikiscope: ENGINEERINGschedule: daily- name: service-repostype: code_repobranches: [main]include: ["**/*.md", "src/**"]- name: requirement-docstype: pdfocr: autooutput:- kind: knowledge_pack- kind: vector_index
A single configuration can describe multiple heterogeneous sources and distribute the processed results to different outputs.
4. Implementation Considerations for Key Stages
4.1 Retrieval and Incremental Updates
- Follow the target site’s access policies and rate limits to avoid placing unnecessary load on internal systems.
- Use content hashes or last-modified timestamps to determine whether a document has changed, and reprocess only changed documents.
- Deleted documents must also be removed from the index. Otherwise, search results may continue to return outdated content.
4.2 PDFs and Scanned Documents
- First determine whether a PDF contains a text layer or consists only of images. Text-layer PDFs can be extracted directly, avoiding OCR and reducing recognition errors.
- Image-based files require OCR. The recognition output should preserve paragraph and table structures instead of concatenating all text into a single block.
- After recognition, perform sample-based proofreading. Pay particular attention to numbers, version strings, model numbers, and proper nouns, because errors in these areas can have the greatest impact.
4.3 Structure-Aware Chunking
- Use heading levels as the primary boundaries and keep the content of the same subsection in one chunk whenever possible.
- Treat code blocks as atomic units. When a code block exceeds the length limit, split it according to syntax or blank lines rather than truncating it mechanically.
- When splitting tables, retain the header row. Each resulting chunk should include the header information so that it remains understandable outside the original document.
