Parsing complex documents with LlamaParse

LlamaIndex · Practical · 10 min

Bad parsing of tables and multi-column PDFs is a common hidden cause of wrong RAG answers.

Simple PDF readers extract text in reading order and often scramble tables, multi-column pages, charts and scanned pages. The RAG system then retrieves garbled chunks, and answers suffer.

LlamaParse is LlamaIndex's hosted document platform. Its docs present several products under one API key:

  • Parse: converts PDFs, scans and Office files into clean Markdown or JSON, including tables.
  • Extract: pulls fields matching a schema you define.
  • Classify and Split: sort documents by type and split combined files.
  • Index: managed ingestion, embedding and retrieval for RAG.

Older material calls the platform LlamaCloud and uses the llama-parse or llama-cloud-services packages; the current Python SDK is llama-cloud, imported as llama_cloud, and reads the LLAMA_CLOUD_API_KEY environment variable. Parse and Extract have moved to a v2 API, so check the docs for current parameters. It is a paid service with a free allowance; pricing changes, so check the website.

When to use it:

  • Many tables, forms, financial reports, slides or scanned pages.
  • When parsed text from SimpleDirectoryReader looks wrong on inspection.

When not to: plain text, Markdown or simple PDFs parse fine locally and for free. Also check data policies, because documents are uploaded to a third party.

Whatever parser you use, print a few parsed pages before indexing. Five minutes of looking at parser output can save days of prompt tuning.

The Markdown output works well with MarkdownNodeParser, which keeps table and section structure together.

Example

# pip install llama-cloud ; export LLAMA_CLOUD_API_KEY=llx-...
from llama_cloud import LlamaCloud
from llama_index.core import Document

client = LlamaCloud()                                   # reads LLAMA_CLOUD_API_KEY
file = client.files.create(file="annual_report.pdf", purpose="parse")
result = client.parsing.parse(
    file_id=file.id,
    tier="agentic",                                     # parsing quality tier
    version="latest",
    expand=["markdown"],
)
pages = [p.markdown for p in result.markdown.pages]
print(pages[0][:200])                                   # | Quarter | Revenue | ... (tables as Markdown)

docs = [Document(text=md, metadata={"file_name": "annual_report.pdf", "page": i + 1})
        for i, md in enumerate(pages)]
# then: MarkdownNodeParser / SentenceSplitter -> VectorStoreIndex

Remember

  • LlamaParse parses complex PDFs, tables and scans into Markdown/JSON
  • Current SDK: llama-cloud with LLAMA_CLOUD_API_KEY; older docs say LlamaCloud/llama-parse
  • Inspect parsed output before indexing, whatever parser you use

Watch out for

  • Uploading confidential documents to a hosted parser without checking your organisation's data policy.

Check yourself

  1. Answers about numbers in financial-report tables are often wrong, and the parsed text shows table cells jumbled into one line. What is the best first fix?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: developers.llamaindex.ai/llamaparse/

Builds on

Unlocks

Nothing builds on this yet.