1. PDFLoaderStage
Uses PyMuPDF (fitz) to load the raw PDF file, count pages, and analyze metadata to determine if it is text-based or scanned (image-only).
Production PDF Ingestion
Production-Grade Parser
Convert dense competitive exam PDFs into perfectly structured JSON data. Powered by a modular sequential processing architecture, featuring visual-aware LLM segmentation, self-correcting Pydantic validations, and automatic diagram extraction.
Uses PyMuPDF (fitz) to load the raw PDF file, count pages, and analyze metadata to determine if it is text-based or scanned (image-only).
Extracts or renders every page of the PDF into a high-quality 200 DPI PNG image. Visual context is crucial for LLMs to interpret dense 2-column layouts, mathematical symbols, and diagrams.
Extracts raw text natively via PyMuPDF. If the page is scanned (0 characters extracted), it falls back to Tesseract OCR to build the text layer.
Applies regex pattern matching to locate question numbers and split the page text into individual, manageable question chunks. Safely falls back to page-level LLM parsing if chunking fails.
The core AI stage. Sends the question chunk text and the high-res page image to the LLM (Gemini, DeepSeek, Qwen) to parse unstructured content into structured JSON.
Validates the raw LLM output against the strict Pydantic Question schema. If errors occur, it captures the error trace and prompts the LLM to self-correct (up to 3 retries).
Takes normalized [ymin, xmin, ymax, xmax] coordinates from the LLM, scales them to the high-res image, and crops diagrams. Uses a Union Merge algorithm to combine overlapping crops.
Determines correct answers and explanations. Integrates three routes: reading an official answer key, querying Tavily Web Search, or requesting LLM solver execution.
Assembles all validated questions and sections into the final Paper schema. Exports a complete JSON file and extracts metadata (title, year) visually from the cover page if needed.
Enforces an abstract contract (BaseLLM) defining `.generate` and `.generate_with_image`. The pipeline manager remains provider-agnostic, allowing seamless provider switching via environment variables.
If an LLM's response fails Pydantic schema validation, the system feeds the broken output and Python stack trace error back to the model, enabling quick self-correction of commas or formatting issues.
By supplying high-resolution PNGs along with OCR chunks, the LLM maintains visual awareness of subscripts, mathematical notation, chemical diagrams, and multi-column document structure.
Before LLM calls, the segmenter splits pages into individual question chunks. This mitigates LLM omissions on long, dense competitive exam papers by limiting the task size per invocation.