LlamaParse Tutorial: Parsing Complex Documents for RAG Pipelines

# Tutorial LlamaParse: Parsing Dokumen Kompleks untuk RAG Pipeline Membangun aplikasi Retrieval-Augmented Generation (RAG) yang akurat sangat bergantung pada kualitas data yang diekstrak dari dokumen...

By Ruby Abdullah · · tutorial
LlamaParseRAGLlamaIndexDocument ParsingPython

LlamaParse Tutorial: Parsing Complex Documents for RAG Pipelines

Building accurate Retrieval-Augmented Generation (RAG) applications heavily depends on the quality of data extracted from source documents. Unfortunately, real-world documents such as PDFs, financial reports, academic papers, and presentation slides have complex structures — tables, images, multi-level headers, and multi-column layouts. Traditional parsing tools often fail to capture these structures correctly. LlamaParse provides a document parsing solution specifically designed to produce high-quality structured output ready for use in RAG pipelines.

In this tutorial, we will learn how to use LlamaParse to extract content from various document types, integrate it with LlamaIndex, and build robust RAG pipelines.

What is LlamaParse?

LlamaParse is a document parsing service developed by the LlamaIndex team. Unlike conventional PDF parsers that only extract raw text, LlamaParse uses AI models to understand document structure and produces output in structured Markdown format. Its key advantages include:

  • High-accuracy table parsing, including complex tables with merged cells
  • Content extraction from images and diagrams using vision models
  • Support for more than 10 file formats (PDF, DOCX, PPTX, XLSX, HTML, and more)
  • Custom parsing instructions to guide the extraction process
  • Direct integration with the LlamaIndex ecosystem

Installation and Setup

Package Installation

LlamaParse is available as a Python package that can be installed via pip:

pip install llama-parse llama-index

If you only need the parser without the full LlamaIndex framework, simply install:

pip install llama-parse

Getting an API Key

LlamaParse uses a cloud API for the parsing process. You need to obtain an API key from LlamaCloud:

  • Visit cloud.llamaindex.ai
  • Create an account or log in
  • Navigate to the API Keys menu
  • Generate a new API key
  • Save the API key as an environment variable:

    export LLAMACLOUDAPIKEY="llx-your-api-key-here"
    

    Or create a .env file in your project root:

    LLAMACLOUDAPIKEY=llx-your-api-key-here
    

    Verify Installation

    import llamaparse
    

    print(f"LlamaParse version: {llamaparse.version}")

    Basic Usage

    Simple PDF Parsing

    The most basic way to use LlamaParse is to parse a single PDF file:

    from llamaparse import LlamaParse
    
    

    parser = LlamaParse(

    resulttype="markdown"

    )

    documents = parser.loaddata("financialreport.pdf")

    for doc in documents:

    print(doc.text[:500])

    The resulttype parameter determines the output format. Available options:

    • "markdown" — output in Markdown format (recommended for RAG)
    • "text" — plain text output without formatting

    Parsing Multiple Files

    LlamaParse supports parsing multiple files at once:

    parser = LlamaParse(resulttype="markdown")
    
    

    files = [

    "reportq1.pdf",

    "reportq2.pdf",

    "businesspresentation.pptx",

    "employeedata.xlsx"

    ]

    alldocuments = []

    for filepath in files:

    docs = parser.loaddata(filepath)

    alldocuments.extend(docs)

    print(f"Total documents parsed: {len(alldocuments)}")

    Async Parsing

    For better performance when parsing many files, use the async API:

    import asyncio
    

    from llamaparse import LlamaParse

    async def parsedocuments():

    parser = LlamaParse(resulttype="markdown")

    files = [

    "document1.pdf",

    "document2.pdf",

    "document3.pdf",

    ]

    tasks = [parser.aloaddata(f) for f in files]

    results = await asyncio.gather(*tasks)

    alldocs = []

    for docs in results:

    alldocs.extend(docs)

    return alldocs

    documents = asyncio.run(parsedocuments())

    print(f"Total pages: {len(documents)}")

    Advanced Usage

    Custom Parsing Instructions

    One of the most powerful features of LlamaParse is the ability to provide custom instructions to guide the parsing process. This is especially useful when you have documents with specific formats:

    parser = LlamaParse(
    

    resulttype="markdown",

    parsinginstruction="""

    This document is a corporate financial report.

    Please ensure:

  • Extract all financial tables with correct formatting
  • Maintain heading hierarchy (chapter titles, sub-sections)
  • Ignore page headers and footers
  • Extract footnotes as separate sections
  • Format financial numbers with thousand separators
  • """

    )

    documents = parser.loaddata("annualreport.pdf")

    Complex Table Parsing

    LlamaParse has specialized capabilities for handling intricate tables:

    parser = LlamaParse(
    

    resulttype="markdown",

    parsinginstruction="""

    Focus on accurate table extraction.

    Preserve all columns and rows, including merged cells.

    Use standard Markdown table format.

    If a table spans multiple pages, merge them into a single table.

    """

    )

    documents = parser.loaddata("statisticaldata.pdf")

    for doc in documents:

    lines = doc.text.split('\n')

    tablelines = [l for l in lines if '|' in l]

    if tablelines:

    print("Table found:")

    for line in tablelines[:10]:

    print(line)

    Multimodal Parsing with Images

    LlamaParse can extract information from images and diagrams within documents:

    parser = LlamaParse(
    

    resulttype="markdown",

    usevendormultimodalmodel=True,

    vendormultimodalmodelname="openai-gpt4o",

    parsinginstruction="""

    For each image or diagram in the document:

  • Describe the visual content in detail
  • Extract any text present in the image
  • If there are charts/graphs, explain the data being displayed
  • """

    )

    documents = parser.loaddata("presentationwithcharts.pdf")

    Language Configuration

    For documents in languages other than English:

    parser = LlamaParse(
    

    resulttype="markdown",

    language="id",

    parsinginstruction="""

    This document is in Bahasa Indonesia.

    Maintain the original language of the document.

    Do not translate the content to another language.

    """

    )

    documents = parser.loaddata("indonesiandocument.pdf")

    Advanced Configuration

    LlamaParse provides various parameters for fine-tuning the parsing process:

    parser = LlamaParse(
    

    resulttype="markdown",

    numworkers=4,

    verbose=True,

    showprogress=True,

    splitbypage=True,

    skipdiagonaltext=True,

    donotunrollcolumns=False,

    pageseparator="\n\n---PAGE BREAK---\n\n",

    )

    documents = parser.loaddata("longdocument.pdf")

    for i, doc in enumerate(documents):

    print(f"Page {i+1}: {len(doc.text)} characters")

    Key parameters to know:

    • numworkers: Number of parallel workers for parsing (default: 4)
    • splitbypage: Split output per page (useful for chunking)
    • skipdiagonaltext: Ignore diagonal text (watermarks)
    • pageseparator: Separator string between pages

    Integration with LlamaIndex for RAG

    Basic RAG Pipeline

    Here is how to build a complete RAG pipeline using LlamaParse and LlamaIndex:

    from llamaparse import LlamaParse
    

    from llamaindex.core import VectorStoreIndex, Settings

    from llamaindex.core.nodeparser import MarkdownNodeParser

    from llamaindex.llms.openai import OpenAI

    from llamaindex.embeddings.openai import OpenAIEmbedding

    Settings.llm = OpenAI(model="gpt-4o-mini")

    Settings.embedmodel = OpenAIEmbedding(model="text-embedding-3-small")

    parser = LlamaParse(

    resulttype="markdown",

    parsinginstruction="Extract content with clear heading structure."

    )

    documents = parser.loaddata("knowledgebase.pdf")

    nodeparser = MarkdownNodeParser()

    nodes = nodeparser.getnodesfromdocuments(documents)

    index = VectorStoreIndex(nodes)

    queryengine = index.asqueryengine(

    similaritytopk=5

    )

    response = queryengine.query("What are the main conclusions of this report?")

    print(response)

    Using MarkdownNodeParser

    Since LlamaParse produces Markdown output, MarkdownNodeParser is the best choice for chunking:

    from llamaindex.core.nodeparser import MarkdownNodeParser
    
    

    nodeparser = MarkdownNodeParser()

    nodes = nodeparser.getnodesfromdocuments(documents)

    for node in nodes[:5]:

    print(f"Node ID: {node.nodeid}")

    print(f"Metadata: {node.metadata}")

    print(f"Content preview: {node.text[:200]}")

    print("---")

    MarkdownNodeParser automatically splits documents based on Markdown headings, ensuring each chunk has coherent context.

    RAG Pipeline with Metadata Filtering

    For large document collections, metadata filtering helps improve retrieval accuracy:

    from llamaparse import LlamaParse
    

    from llamaindex.core import VectorStoreIndex, Document

    from llamaindex.core.nodeparser import MarkdownNodeParser

    parser = LlamaParse(resulttype="markdown")

    fileconfigs = [

    {"path": "report2024.pdf", "category": "finance", "year": "2024"},

    {"path": "report2025.pdf", "category": "finance", "year": "2025"},

    {"path": "hrpolicy.pdf", "category": "hr", "year": "2025"},

    ]

    allnodes = []

    nodeparser = MarkdownNodeParser()

    for config in fileconfigs:

    docs = parser.loaddata(config["path"])

    for doc in docs:

    doc.metadata.update({

    "category": config["category"],

    "year": config["year"],

    "source": config["path"]

    })

    nodes = nodeparser.getnodesfromdocuments(docs)

    allnodes.extend(nodes)

    index = VectorStoreIndex(allnodes)

    from llamaindex.core.vectorstores import MetadataFilter, FilterOperator

    queryengine = index.asqueryengine(

    similaritytopk=5,

    filters=[

    MetadataFilter(key="category", value="finance"),

    MetadataFilter(key="year", value="2025"),

    ]

    )

    response = queryengine.query("What is the total revenue for 2025?")

    print(response)

    Chat Engine with Document Context

    To build a chatbot that can answer questions based on documents:

    from llamaparse import LlamaParse
    

    from llamaindex.core import VectorStoreIndex

    from llamaindex.core.nodeparser import MarkdownNodeParser

    from llamaindex.core.memory import ChatMemoryBuffer

    parser = LlamaParse(resulttype="markdown")

    documents = parser.loaddata("productmanual.pdf")

    nodeparser = MarkdownNodeParser()

    nodes = nodeparser.getnodesfromdocuments(documents)

    index = VectorStoreIndex(nodes)

    memory = ChatMemoryBuffer.fromdefaults(tokenlimit=3000)

    chatengine = index.aschatengine(

    chatmode="condensepluscontext",

    memory=memory,

    systemprompt="""

    You are a helpful assistant that answers questions

    based on the product manual document. Answer accurately

    and concisely. If information is not in the document,

    say so honestly.

    """

    )

    response = chatengine.chat("How do I install this product?")

    print(response)

    response = chatengine.chat("What are the system requirements?")

    print(response)

    Handling Special File Formats

    Parsing Spreadsheets (XLSX/CSV)

    parser = LlamaParse(
    

    resulttype="markdown",

    parsinginstruction="""

    This is a spreadsheet file. Convert each sheet to a Markdown table.

    Use sheet names as headings.

    Format numbers according to context (currency, percentage, etc.).

    """

    )

    documents = parser.loaddata("salesdata.xlsx")

    print(documents[0].text)

    Parsing PowerPoint (PPTX)

    parser = LlamaParse(
    

    resulttype="markdown",

    usevendormultimodalmodel=True,

    vendormultimodalmodelname="openai-gpt4o",

    parsinginstruction="""

    This is a PowerPoint presentation. For each slide:

  • Use the slide title as a heading
  • Extract all bullet points
  • Describe any images and diagrams present
  • Maintain slide order
  • """

    )

    documents = parser.loaddata("quarterlypresentation.pptx")

    Parsing Word Documents (DOCX)

    parser = LlamaParse(
    

    resulttype="markdown",

    parsinginstruction="""

    Preserve the heading structure of the Word document.

    Convert tables to Markdown format.

    Maintain bold and italic formatting.

    """

    )

    documents = parser.loaddata("projectproposal.docx")

    Best Practices

    1. Choose the Right Parsing Instructions

    Parsing instructions are key to getting high-quality output. Some tips:

    # For financial reports
    

    financialinstruction = """

    This document is a financial report. Prioritize:

    • Accuracy of numbers and currency formatting
    • Balance sheet and income statement table structure
    • Notes to financial statements
    """

    For academic papers

    academicinstruction = """

    This document is an academic paper. Prioritize:

    • Section structure (Abstract, Introduction, Methods, Results, Discussion)
    • Mathematical formulas (use LaTeX notation)
    • References and citations
    • Table and figure captions
    """

    For legal documents

    legalinstruction = """

    This is a legal document. Prioritize:

    • Article and section numbering
    • Term definitions
    • Clauses and sub-clauses
    • Preserve original numbering format
    """

    2. Optimize Chunking Strategy

    Choose a chunking strategy appropriate for the document type:

    from llamaindex.core.nodeparser import (
    

    MarkdownNodeParser,

    SentenceSplitter,

    )

    For documents with clear headings

    markdownparser = MarkdownNodeParser()

    For documents without consistent heading structure

    sentenceparser = SentenceSplitter(

    chunksize=1024,

    chunkoverlap=200

    )

    Use based on document type

    nodes = markdownparser.getnodesfromdocuments(documents)

    3. Robust Error Handling

    from llamaparse import LlamaParse
    

    import os

    def safeparse(filepath, maxretries=3):

    if not os.path.exists(filepath):

    raise FileNotFoundError(f"File not found: {filepath}")

    parser = LlamaParse(

    resulttype="markdown",

    verbose=True

    )

    for attempt in range(maxretries):

    try:

    documents = parser.loaddata(filepath)

    if not documents:

    print(f"Warning: No content from {filepath}")

    return []

    return documents

    except Exception as e:

    print(f"Attempt {attempt + 1}/{maxretries} failed: {e}")

    if attempt == maxretries - 1:

    raise

    return []

    try:

    docs = safeparse("report.pdf")

    print(f"Successfully parsed {len(docs)} documents")

    except Exception as e:

    print(f"Parsing failed: {e}")

    4. Cache Parsing Results

    To avoid re-parsing the same documents:

    import json
    

    import hashlib

    import os

    from llamaparse import LlamaParse

    CACHEDIR = ".parsecache"

    def getfilehash(filepath):

    with open(filepath, "rb") as f:

    return hashlib.md5(f.read()).hexdigest()

    def parsewithcache(filepath):

    os.makedirs(CACHEDIR, existok=True)

    filehash = getfilehash(filepath)

    cachepath = os.path.join(CACHEDIR, f"{filehash}.json")

    if os.path.exists(cachepath):

    print(f"Using cache for {filepath}")

    with open(cachepath, "r") as f:

    cached = json.load(f)

    from llamaindex.core import Document

    return [Document(text=d["text"], metadata=d["metadata"]) for d in cached]

    parser = LlamaParse(resulttype="markdown")

    documents = parser.loaddata(filepath)

    cachedata = [{"text": d.text, "metadata": d.metadata} for d in documents]

    with open(cachepath, "w") as f:

    json.dump(cachedata, f)

    print(f"Parsing results cached to {cachepath}")

    return documents

    5. Monitor API Usage

    LlamaParse has a free tier with daily page limits. Monitor your usage:

    def parsewithpagecount(filepath, dailylimit=1000):
    

    parser = LlamaParse(

    resulttype="markdown",

    splitbypage=True,

    verbose=True

    )

    documents = parser.loaddata(filepath)

    pagecount = len(documents)

    print(f"File: {filepath}")

    print(f"Pages processed: {pagecount}")

    print(f"Estimated remaining daily quota: {dailylimit - pagecount}")

    return documents

    Comparison with Other Parsing Tools

    | Feature | LlamaParse | PyPDF2 | Unstructured | Docling |

    |---------|-----------|--------|-------------|---------|

    | Table accuracy | High | Low | Medium | High |

    | Multimodal | Yes | No | Limited | Yes |

    | Output format | Markdown/Text | Text | JSON/Text | Markdown/JSON |

    | Custom instructions | Yes | No | No | Limited |

    | Pricing | Freemium | Free | Freemium | Free |

    | Self-hosted | No | Yes | Yes | Yes |

    | Speed | Medium | Fast | Medium | Medium |

    Conclusion

    LlamaParse is an invaluable tool for building RAG pipelines that require high-quality document extraction. Its main strengths lie in its ability to handle complex tables, multimodal support, and seamless integration with the LlamaIndex ecosystem.

    Key takeaways to remember:

    • Use resulttype="markdown" for the best output in RAG pipelines
    • Leverage parsing instructions to guide extraction according to document type
    • Use MarkdownNodeParser for optimal chunking
    • Implement caching to save API quota
    • Consider async parsing for batch processing large numbers of documents

    By combining LlamaParse with LlamaIndex, you can build RAG applications capable of accurately answering questions based on complex business documents — from financial reports to technical documentation.

    Related Articles

    Docling: Smart Document Parsing for AI and RAG Pipelines

    Docling: Document Parsing Cerdas untuk Pipeline AI dan RAG Dalam era AI generatif, kemampuan untuk mengekstrak informasi...

    Complete LlamaIndex Tutorial: Building RAG Applications with LLMs

    Tutorial Lengkap LlamaIndex: Membangun Aplikasi RAG dengan LLM LlamaIndex adalah framework data yang powerful untuk memb...

    Pathway Tutorial: Real-Time Data Processing Framework for AI and Machine Learning

    Tutorial Pathway: Framework Real-Time Data Processing untuk AI dan Machine Learning Pathway adalah framework open-source...

    Complete Unstructured Tutorial: Document Preprocessing for RAG and LLM Applications

    Tutorial Lengkap Unstructured: Preprocessing Dokumen untuk RAG dan Aplikasi LLM Dalam membangun aplikasi Retrieval-Augme...