HtmlRAG: HTML is Better Than Plain Text for Modeling Retrieved Knowledge in RAG Systems

Jiejun Tan, Zhicheng Dou, Wen Wang, Mang Wang, Weipeng Chen, Ji-Rong Wen

Abstract

Retrieval-Augmented Generation (RAG) systems typically strip HTML from retrieved web pages to fit context windows, inadvertently destroying structural and semantic metadata. We present HtmlRAG, a framework that retains HTML's rich context through a multi-stage pruning strategy. By organizing HTML into a "Block Tree" and employing both embedding-based and generative fine-grained pruning, HtmlRAG enables Large Language Models (LLMs) to leverage structural cues. Experimental results across six benchmarks demonstrate that HtmlRAG consistently outperforms plain-text baselines, offering a more robust approach to modeling retrieved knowledge.

Introduction

Most Retrieval-Augmented Generation (RAG) pipelines follow a predictable path: find a web page, strip it down to plain text (or Markdown), chunk it, and feed it to an LLM. While this approach was necessary when context windows were small, it is increasingly becoming a bottleneck for modern models.

When a complex web page is converted to plain text, the "connective tissue" of the document is lost. Tables become jumbled strings, headings lose their hierarchy, and semantic markers like <code> or <a> tags—which often highlight the most important entities—are discarded. However, raw HTML is notoriously "noisy," often exceeding 80,000 tokens per page due to JavaScript, CSS, and deeply nested tags. This noise can overwhelm an LLM's context window and distract it with irrelevant data.

The HtmlRAG Framework

HtmlRAG is designed to keep the structural benefits of HTML while making it concise enough for efficient LLM processing. The workflow consists of four primary stages:

  1. HTML Cleaning: A rule-based stage removes non-content elements (JavaScript, CSS, comments) and performs "lossless structural compression" by merging redundant nested tags.
  2. Granularity-Adjustable Block Tree: Instead of working with a hyper-detailed DOM tree, nodes are grouped into "blocks" based on a word-count threshold. This allows the system to treat related content as a single unit, preventing the "broken context" problem common in fixed-size text chunking.
  3. Embedding-based Pruning: A fast, coarse filter uses a standard embedding model to rank blocks against the user's query and discard the most irrelevant sections.
  4. Generative Fine-grained Pruning: A lightweight LLM (Phi-3.5-mini) is fine-tuned to act as a "path-aware" pruner. It analyzes the remaining HTML and predicts the "block path" leading to the most relevant information. To maintain efficiency, this stage utilizes a Token Tree with dynamic skipping, allowing the model to bypass nearly half of the required computation.

Evaluation and Key Insights

The authors evaluated HtmlRAG on six diverse Question Answering (QA) benchmarks, including ASQA, Hotpot-QA, and Trivia-QA. The results yielded several critical insights:

  • HTML as a Native Language: LLMs are pre-trained on massive web crawls. They do not merely tolerate HTML; they are inherently proficient at understanding it. The structure itself (e.g., being in a table or under a specific <h1>) is often as important as the text for accurate retrieval.
  • Superior Accuracy: HtmlRAG consistently outperformed plain-text and Markdown baselines. On the MuSiQue multi-hop dataset, it showed significant gains in Exact Match (EM) scores compared to traditional reranking methods.
  • Massive Compression: The cleaning process alone reduces raw HTML token counts by over 94%. Subsequent pruning stages further compress the content to fit within 4k or 8k context windows without losing answer-bearing content.

Conclusion

HtmlRAG demonstrates that the industry-standard practice of stripping HTML is counterproductive for high-performance RAG systems. By treating HTML structure as a first-class citizen and applying intelligent, generative pruning, systems can achieve higher accuracy on complex reasoning tasks. While the two-stage pruning process introduces some latency and depends on high-quality SFT data for the pruner, it represents a significant step toward more sophisticated web-scale knowledge modeling. Future work will explore end-to-end architectures and extending these techniques to other rich-text formats like PDFs.

Summary updated

BibTeX
@inproceedings{tan2024htmlrag,
  author = {Jiejun Tan and Zhicheng Dou and Wen Wang and Mang Wang and Weipeng Chen and Ji-Rong Wen},
  title = {{HtmlRAG: HTML is Better Than Plain Text for Modeling Retrieved Knowledge in RAG Systems}},
  booktitle = {The Web Conference},
  year = {2024},
}
Figure