Implement Relational Parsing, TOC Retrieval, and Typed Answers for PDFs
A technical analysis outlines the evolution from a minimal RAG baseline to a production-ready enterprise pipeline by upgrading four core architectural components: document parsing, question parsing, retrieval, and generation. Using the arXiv paper Attention Is All You Need as a controlled test case, the research demonstrates how structural enhancements and typed contracts resolve common enterprise failure modes, including typo-induced retrieval misses, loss of document hierarchy, and unstructured model outputs. Document parsing has transitioned from extracting flat text to generating a relational set of tables. The upgraded module produces line_df for atomic line-level citations, page_df for page-level aggregation, and toc_df containing the native table of contents. This approach preserves the document's intrinsic structure, enabling downstream bricks to scope queries precisely. The parsing process also yields a summary with metadata such as layout type and language, supporting informed routing decisions in subsequent stages. Question parsing now handles noisy user input through a single LLM call that simultaneously corrects surface-level typos and extracts content keywords. This process eliminates the need for separate spell-checking passes. The system further enhances retrieval precision by expanding corrected keywords using an expert vocabulary database, ensuring the search targets relevant domain terms even when the user omits specific jargon. The output is a typed ParsedQuestion object that separates concerns, providing distinct briefs for retrieval and generation based on the inferred intent, scope, and expected answer shape. Retrieval logic has shifted from basic keyword matching to a structured filtering mechanism anchored by the document's table of contents. The pipeline employs keyword hits per section in conjunction with an LLM-based TOC router that reasons over the outline to identify the most relevant sections. To mitigate retrieval failures caused by arbitrary PDF line breaks, the system performs detection at the passage level rather than the line level. The retrieval component returns a typed RetrievalResult that unions candidates from multiple detectors, allowing context windows to be sized according to the granularity implied by the question. Generation has been upgraded to enforce strict typed schemas, replacing free-form prose with machine-readable contracts. When the question parser identifies a request for a list, the system outputs a ListAnswer where each item contains text, verbatim quotes, and precise start and end line ranges. The schema includes critical quality indicators such as confidence scores, completeness flags, and a context_structured metric that detects reading order violations. These fields allow downstream applications to validate results automatically, highlight evidence in annotated PDFs, or trigger fallback mechanisms when context is incomplete or scrambled. The analysis emphasizes that each brick upgrade is modular and independent, permitting incremental adoption. The resulting pipeline produces auditable, structured outputs suitable for integration with SQL databases and automated workflows. Future development will focus on composing these bricks into a single invocation with feedback loops, enabling the system to handle real-world stress tests involving broken tables of contents and complex multi-intent queries.
