Module 2 · Parsing & chunking

LlamaParse for complex PDFs, tables and images

6 min read

PDF is the most widespread format in companies, and the most treacherous for a RAG system. It was designed for printing, not for machine reading: a table is just a set of text fragments positioned on the page, two columns may be read in a zigzag, a diagram is a mute image. LlamaParse, LlamaIndex's parsing service, uses vision and language models to rebuild a faithful representation — in Markdown, tables included. This lesson shows when you need it and how to integrate it.

What a classic PDF reader sees

The default reader (based on pypdf) extracts text in the order it appears in the file. For a simple document, that's enough. For a real document, problems pile up:

ElementResult of raw text extraction
TableFlattened cells, mixed columns, lost headers
Two-column layoutLines of both columns interleaved
Headers and footersRepeated on every page, in the middle of the text
Diagram, photo, screenshotNothing: the information is lost
Scanned PDFNothing at all, without character recognition
Formulas, footnotesMoved, cut

The danger is that these errors are silent: ingestion "succeeds", but the indexed passages are wrong. The Lift desk assembly manual, in Lumen's corpus, contains a parts table: read by pypdf, it stays readable because it's simple and in a monospace font. On a real table with merged cells, that would no longer be the case.

Get into the habit of looking at what your reader produces, document by document, before indexing.

LlamaParse: model-based parsing

LlamaParse rebuilds the document's structure: headings, lists, tables in Markdown, descriptions of image content, correct column reading. The result is clean Markdown, well suited to section-based chunking (next lesson).

LlamaParse is an online service (LlamaCloud), with an API key and per-page billing. Several processing tiers trade cost against quality:

TierProcessingFor
fastFast extraction, no AISimple documents, large volumes
cost_effectiveModel-based parsing, economicalEveryday documents
agenticAgent-based parsing, with verificationComplex tables, rich layouts
agentic_plusThe most thoroughCritical and difficult documents

Beware: the SDK has changed

The old llama-parse and llama-cloud-services packages, found in many tutorials, are deprecated: their maintenance ended on May 1, 2026. The current SDK is llama-cloud (version 1.0 and above):

pip install "llama-cloud>=1.0"
export LLAMA_CLOUD_API_KEY="..."

Here is Lumen's integration:

from llama_cloud import LlamaCloud


def parse_pdf(path: Path, tier: str = "agentic") -> list[Document]:
    """Un Document par page, en Markdown (tableaux compris), avec le numéro de page en métadonnée."""
    client = LlamaCloud()  # lit LLAMA_CLOUD_API_KEY
    with open(path, "rb") as f:
        result = client.parsing.parse(tier=tier, version="latest", upload_file=f, expand=["markdown"])
    documents = []
    for page in result.markdown.pages:
        if not getattr(page, "success", False):
            continue  # page en échec : on la signale dans les journaux plutôt que d'indexer du vide
        documents.append(Document(
            text=page.markdown,
            metadata={"source": path.name, "page": page.page_number, "parser": f"llamaparse:{tier}"},
            id_=f"{path.name}#p{page.page_number}",
        ))
    return documents

A few design points:

  • parsing.parse combines uploading the file, waiting for processing to finish and fetching the result. For large volumes, prefer creating jobs (parsing.create), then fetching results later, or via a webhook.
  • expand=["markdown"] asks for the result in Markdown, page by page. Other formats exist (plain text, structured JSON items, extracted images).
  • A page can fail without the whole document failing: the result reports it page by page (success). Lumen skips failed pages; in production, log them to reprocess them.
  • The processing tier is recorded as metadata (parser). The day you change tier or version, you'll know which documents were processed how.
  • version pins the tier configuration: use a dated version in production so that two ingestions give the same result, latest to experiment.

The tests/test_parsing.py test checks this integration without network: it replaces the service call with a response built from the SDK's real types, including a failed page.

Sensitive data and cost

Sending a document to a parsing service means taking it out of your information system. Before adopting LlamaParse (or any equivalent service), check data location (LlamaCloud offers a European region), retention, and contractual terms. For very sensitive documents, local alternatives exist — open-source parsing libraries, or a vision model run in-house — usually at the cost of lower quality on hard cases.

On cost, parsing is paid once per document version: combined with incremental ingestion (lesson 2.4), an unchanged document is never re-parsed. One more reason to take care of identifiers and the cache.

When to do without it?

SituationReasonable choice
Markdown, HTML, textNo specialized parsing: the structure is already there
PDFs generated from a word processor, without complex tablesLocal reader, after checking the result
PDFs with tables, columns, diagramsLlamaParse (agentic tier)
Scanned PDFsParsing with character recognition (LlamaParse or equivalent)
Very sensitive documentsLocal solution, accepting lower quality on hard cases

Lumen uses LlamaParse when a key is available, and the local reader otherwise: the rest of the pipeline doesn't change, since both produce Documents.

Key takeaways

  • A PDF is made for printing: tables, columns and images are badly read by raw text extraction, and the error is silent.
  • LlamaParse rebuilds the structure in Markdown with models; several tiers trade cost against quality.
  • The current SDK is llama-cloud (client.parsing.parse); llama-parse and llama-cloud-services are deprecated.
  • Handle failures page by page, record the parser used, pin the version in production.
  • Check data location and retention before sending documents to an external service.

Practice

  1. Read data/manuals/desk-2-montage.pdf with SimpleDirectoryReader and compare the text obtained with the PDF: what is lost?
  2. If you have a LlamaCloud key, parse the same manual with the fast then agentic tiers and compare the parts table.
  3. Pick a real PDF from your company with a table, and evaluate both approaches on three questions about that table.

Resources