Parsing Documents with LlamaIndex
Bashpip install llama-parse
Pythonfrom llama_parse import LlamaParse parser = LlamaParse( api_key="llx-...", # from cloud.llamaindex.ai result_type="markdown", # "markdown" or "text" parsing_instruction="Extract all tables with full row/column structure", premium_mode=True, # enables agentic/VLM parsing for complex docs ) documents = parser.load_data("./contract.pdf") print(documents[0].text)
For fully local/offline parsing with no LLM tokens:
Bashnpm install @llamaindex/liteparse
Progress:
- Step 1: Identify document complexity (simple text vs. scanned/handwritten/multi-modal)
- Step 2: Choose the right tool (LlamaParse cloud for complex/agentic parsing, LiteParse for fast local parsing)
- Step 3: Parse — get layout-aware markdown/text output with bounding boxes if needed
- Step 4: Extract — define a schema and run LLM-powered structured extraction
- Step 5: Split/Classify — segment long documents into logical sections, tag by natural-language rules
- Step 6: Index — chunk and embed for RAG retrieval
- Step 7: Validate — spot-check tables, charts, and handwritten sections for accuracy; re-run with
premium_modeor customparsing_instructionif quality is low
Decision guide:
- Scanned/handwritten/complex multi-column docs → LlamaParse with
premium_mode=True - Clean digital PDFs, Office docs, images, no cloud/tokens desired → LiteParse (local)
- Need structured JSON from unstructured content → LlamaParse Extract with a Pydantic schema
- Need to route document types differently → LlamaParse Classify + Split before extraction
Example 1: Input: A 40-page financial due-diligence PDF with dense multi-page tables and embedded charts. Output: Markdown output preserving table row/column relationships, chart data converted to structured JSON (labels, series, values), ready to chunk and embed for RAG.
Example 2: Input: Scanned insurance claim form with handwritten notes. Output: Clean text extraction of handwritten fields via agentic OCR auto-correction loop, structured into a defined schema (claimant name, date, claim amount, notes) with confidence flags on low-certainty fields.
Example 3: Input: Directory of 500 mixed invoices (different vendors/layouts). Output: Classified by vendor type using natural-language rules, then extracted into a unified schema (invoice_number, line_items, total) with no per-template custom code.
- Always set a
parsing_instructionfor domain-specific documents (e.g., "preserve table structure," "extract handwritten annotations separately") — improves accuracy significantly. - Use
premium_mode/agentic parsing for anything with charts, tables, handwriting, or irregular layout; reserve fast/basic mode for clean digital text to save cost. - Define extraction schemas explicitly (Pydantic/JSON schema) rather than relying on free-form prompts — yields consistent, machine-parseable output.
- Chunk after parsing, not before — parse the full document first to preserve layout context, then split logically for indexing.
- For production/enterprise workloads, use VPC deployment and enable access controls if handling PHI/PII (HIPAA/GDPR/SOC2 scope).
- Validate outputs on a sample set before running full-batch extraction on high-volume document sets.
- Don't run cheap/basic OCR on scanned or handwritten documents — it silently fails on messy layouts; use agentic/premium parsing instead.
- Don't skip schema definition for extraction — free-form extraction is inconsistent and hard to validate downstream.
- Don't chunk raw OCR text before parsing corrects layout — this destroys table/row relationships permanently.
- Don't assume one parsing config fits all document types — finance tables, medical handwriting, and manufacturing specs need different
parsing_instructions. - Don't ignore auto-correction/confidence signals — always surface low-confidence extractions for human review in high-stakes domains (finance, healthcare, insurance).