# BUG: pymupdf4llm list index out of range in document\_layout.py

**URL:** <https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207>\
**Category:** PyMuPDF\
**Created:** [November 27, 2025, 10:51am UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207 "2025-11-27T10:51:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![robvd](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/robvd/32/270_2.png) [@robvd](https://forum.mupdf.com/u/robvd)\
**Post date:** [November 27, 2025, 10:51am UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/1 "2025-11-27T10:51:13Z")

</div>

When parsing the attached file using `pymupdf.layout+pymupdf4llm` the following traceback is encountered:

```auto
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/pymupdf4llm/ __init__.py", line 97, in to_markdown
    return parsed_doc.to_markdown(
  File "/usr/local/lib/python3.10/site-packages/pymupdf4llm/helpers/document_layout.py", line 672, in to_markdown
    output += list_item_to_md(box.textlines, list_item_levels[i])
  File "/usr/local/lib/python3.10/site-packages/pymupdf4llm/helpers/document_layout.py", line 391, in list_item_to_md
    line = textlines[0]
IndexError: list index out of range

```

Versions:

`pymupdf4llm`: 0.2.4

`pymupdf-layout`: 1.26.6

p.s. could not attach file because it was too large. Please tell me how to send it.

---

<div class="post-metadata">

**Author:** ![Jamie\_Lemon](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/jamie_lemon/32/5_2.png) [@Jamie\_Lemon](https://forum.mupdf.com/u/Jamie_Lemon)\
**Post date:** [November 28, 2025, 1:15pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/2 "2025-11-28T13:15:38Z")

</div>

What is the file size of the PDF?

---

<div class="post-metadata">

**Author:** ![robvd](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/robvd/32/270_2.png) [@robvd](https://forum.mupdf.com/u/robvd)\
**Post date:** [November 28, 2025, 4:24pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/3 "2025-11-28T16:24:20Z")

</div>

It is 27.9MB

---

<div class="post-metadata">

**Author:** ![Jamie\_Lemon](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/jamie_lemon/32/5_2.png) [@Jamie\_Lemon](https://forum.mupdf.com/u/Jamie_Lemon)\
**Post date:** [November 28, 2025, 5:09pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/4 "2025-11-28T17:09:48Z")

</div>

Thanks - have updated the site to allow up to 30MB of file size for attachment - so please try again!

---

<div class="post-metadata">

**Author:** ![robvd](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/robvd/32/270_2.png) [@robvd](https://forum.mupdf.com/u/robvd)\
**Post date:** [November 28, 2025, 7:08pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/5 "2025-11-28T19:08:04Z")

</div>

Here it is!

The commands used were:

```auto
doc=pymupdf.open(pdf_name)
md_chunks = pymupdf4llm.to_markdown(doc)

```

[file.pdf](https://forum.mupdf.com/uploads/short-url/DaHnJEglkIH73cs6z7p1yrRz8N.pdf) (26.6 MB)

---

<div class="post-metadata">

**Author:** ![Jamie\_Lemon](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/jamie_lemon/32/5_2.png) [@Jamie\_Lemon](https://forum.mupdf.com/u/Jamie_Lemon)\
**Post date:** [November 28, 2025, 11:37pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/6 "2025-11-28T23:37:46Z")

</div>

@robvd Thanks. The good news is that I can replicate this bug on my machine! Will investigate further next week to see what we can do here …

---

<div class="post-metadata">

**Author:** ![HaraldLieder](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/haraldlieder/32/14_2.png) [@HaraldLieder](https://forum.mupdf.com/u/HaraldLieder)\
**Post date:** [December 2, 2025, 12:23pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/7 "2025-12-02T12:23:12Z")

</div>

If you upgrade to the latest PyMuPDF4LLM version 0.2.5, everything should work fine. This script:  
import sys  
from pathlib import Path  
import pymupdf.layout  
import pymupdf4llm

print(f"{pymupdf.version=}, {pymupdf4llm.version=}“)  
doc = pymupdf.open(sys.argv[1])  
md = pymupdf4llm.to\_markdown(  
doc,  
write\_images=False, # do not write image files  
embed\_images=False, # embed images as base64 strings  
image\_format=“jpg”, # image format (embedded or written)  
header=True, # include/omit page headers  
footer=False, # include/omit page footers  
pages=None,  
show\_progress=True,  
)  
Path(doc.name).with\_suffix(”.md").write\_bytes(md.encode())

```auto
produces this console output:
![image|690x90](upload://56Ma9wbNKiussTQfDjhtKtDMxJm.png)
```

---

<div class="post-metadata">

**Author:** ![Jamie\_Lemon](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/jamie_lemon/32/5_2.png) [@Jamie\_Lemon](https://forum.mupdf.com/u/Jamie_Lemon)\
**Post date:** [December 2, 2025, 1:47pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/8 "2025-12-02T13:47:11Z")

</div>

Can confirm it works for me with the latest version of pymupdf4llm. My script was:

```python
import pymupdf.layout
import pymupdf4llm

print(f"{pymupdf.version=}, {pymupdf4llm.version=}")

doc=pymupdf.open(“file.pdf”)

try:
  md = pymupdf4llm.to_markdown(
    doc,
    show_progress=True,
  )
  print(md)
except Exception as e:
  print(f’{e=}')
  input('Deliberate hang, press return to continue… ? ')

```

---

<div class="post-metadata">

**Author:** ![robvd](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/robvd/32/270_2.png) [@robvd](https://forum.mupdf.com/u/robvd)\
**Post date:** [December 2, 2025, 3:42pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/9 "2025-12-02T15:42:11Z")

</div>

I can also confirm it works with version 0.2.5. Thanks!

---

<div class="post-metadata">

**Author:** ![robvd](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/robvd/32/270_2.png) [@robvd](https://forum.mupdf.com/u/robvd)\
**Post date:** [December 2, 2025, 4:06pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-list-index-out-of-range-in-document-layout-py/207/10 "2025-12-02T16:06:08Z")

</div>

Just one thing: if I convert multiple documents after each other, using  
`doc=pymupdf.open(pdf_name)`  
`md_chunks = pymupdf4llm.to_markdown(doc)`

then in the output I see the `Full-page OCR` message accumulate, so e.g. when converting first file output was

```auto
Full-page OCR on page.number=5/6.
Full-page OCR on page.number=14/15.
Full-page OCR on page.number=23/24.

```

After the second file the output was:

```auto
Full-page OCR on page.number=5/6.
Full-page OCR on page.number=14/15.
Full-page OCR on page.number=23/24.
Full-page OCR on page.number=0/1.
Full-page OCR on page.number=1/2.

```

so it includes the output lines from the first file. Did not dive deep into this yet, just wanted to mention it.
