TellWorldAI World Archive

Home›Practice›Practice

Practice

Converting PDFs and images to structured documents with Granite Docling V2

Granite Docling V2 500M converts a page image into DocLang, a markup with element types, positions and reading order. This guide covers running it with Transformers or vLLM, choosing task prompts, and exporting to Markdown, HTML or JSON.

Who this is for

This guide suits developers who need to turn PDFs or scanned pages into structured documents, for example to build a knowledge base or extract tables and formulas in bulk. By the end you should be able to convert one page locally with Transformers or vLLM, choose a task prompt, and export the result to Markdown, HTML or JSON.

What Granite Docling V2 500M is

Granite Docling V2 500M is the small, fast member of the Granite Docling V2 family. It converts a page image into DocLang, a markup language that records each element's type, position and reading order[1].

Each DocLang element starts with its type, followed by four location tokens, x0 y0 x1 y1, on a 512 × 512 grid that starts at the top-left corner[1]. A single generation can produce headings, paragraphs, lists, tables with merged and header cells, LaTeX formulas, code, pictures with their captions and text, and page headers and footers[1].

The model comes from the Docling team at IBM, and it succeeds SmolDocling and Granite-Docling 258M[1].

Setting up

To run with Transformers, install the dependencies first[1]:

pip install "transformers>=5.23.0" torch torchvision pillow docling-core

The model has 0.54B parameters in total. The vision encoder has 157M, the language decoder has 387M, and the density router has 0.1M[1].

Converting one page with Transformers

Load the model and processor, then build a message that pairs the page image with the <doclang> prompt[1]:

import torch
from PIL import Image
from transformers import AutoModelForImageTextToText, AutoProcessor

model_id = "ibm-granite/granite-docling-v2-500m"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForImageTextToText.from_pretrained(model_id, dtype=torch.bfloat16, device_map="cuda")

image = Image.open("page.png").convert("RGB")
messages = [{"role": "user", "content": [
    {"type": "image", "image": image},
    {"type": "text", "text": "<doclang>"},
]}]

Decode the output with skip_special_tokens=False. DocLang tags and location tokens are special tokens, so skipping them leaves bare text[1]:

output = model.generate(**inputs, max_new_tokens=16384, do_sample=False)
doclang = processor.decode(output[0, inputs["input_ids"].shape[1]:], skip_special_tokens=False)
doclang = doclang.removesuffix(processor.tokenizer.eos_token)
print(doclang)

The inputs must first go through the density router. The router decision costs one pass of the vision encoder. To force the dense-page route, call prepare(fine_route=True)[1].

Speeding up with vLLM

The vLLM install command uses --pre and the nightly wheel index, and it pins transformers==5.23.0[1]:

pip install -U vllm --pre "transformers==5.23.0" \
  --extra-index-url https://wheels.vllm.ai/nightly

The server command sets --model-impl transformers. It turns on MTP speculative decoding with 3 speculative tokens, and it passes --trust-request-mm-kwargs so the client can send each page's route with its request[1]:

vllm serve ibm-granite/granite-docling-v2-500m \
  --model-impl transformers \
  --max-model-len 32768 \
  --max-num-batched-tokens 16384 \
  --limit-mm-per-prompt '{"image": 1}' \
  --speculative-config '{"method": "mtp", "num_speculative_tokens": 3}' \
  --api-server-count 8 \
  --gpu-memory-utilization 0.8 \
  --trust-request-mm-kwargs

The model card reports about 18.5 pages per second on one H100 with vLLM and MTP speculative decoding, and about 14 pages per second without MTP[1]. With MTP, the model drafts 3 tokens per step, and the output matches the output without MTP[1].

Send one page per request, and keep 64 or more requests in flight. Without mm_processor_kwargs, every page takes the 64-token route[1].

Choosing a task prompt

The default prompt, <doclang>, converts the whole page. To get only part of the content, add tasks in square brackets after <doclang>. Put each task in angle brackets and separate tasks with a comma and a space[1]. The supported tasks are <ocr>, <table>, <formula>, <picture>, <code>, <layout> and <detailed>[1].

A few common examples:

  • <doclang> [<table>] returns only the tables[1].
  • <doclang> [<table>, <formula>] returns only the tables and formulas[1].
  • <doclang> [<ocr>, <layout>] returns text with location tokens. Without <layout>, the output has content only[1].
  • <doclang> [<detailed>] returns the full page plus a generated description of each picture[1].

The format is strict. Keep exactly one space after <doclang>, because other spacing or punctuation lowers quality. The model does not follow free-form instructions or answer questions. When the requested content is absent, it returns an empty <doclang></doclang>[1].

Exporting to Markdown, HTML or JSON

After you have the DocLang string, use docling-core to deserialize it into a document object and export it[1]:

from docling_core.transforms.deserializer.doclang import DocLangDocDeserializer

document = DocLangDocDeserializer().deserialize_str(doclang)
print(document.export_to_markdown())
document.save_as_markdown("page.md")
document.save_as_html("page.html")
document.save_as_json("page.json")

Common problems

  • Handwriting is read much better than with Granite-Docling 258M, but printed text is still more accurate[1].
  • Chinese, Japanese, Korean and mixed Chinese–English pages are much stronger than with the 258M model[1].
  • The model card's scores use ground truth that the authors converted to DocLang and revised by hand. The numbers are not comparable with scores on published benchmarks or their leaderboards[1].

Next steps

The model is released under the Apache 2.0 license. Read the license text before any commercial use[1].

Adapted from the Granite Docling V2 500M model card on Hugging Face[1], released under the Apache 2.0 License.

Sources

  1. 1

Cite this page: Converting PDFs and images to structured documents with Granite Docling V2. TellWorld, https://www.tellworld.net/practice/practice-granite-docling-v2. Updated 2026-12-04

Related news

View all