# BUG: pymupdf4llm parse\_document() produces KeyError when document.use\_ocr is False

**URL:** <https://forum.mupdf.com/t/bug-pymupdf4llm-parse-document-produces-keyerror-when-document-use-ocr-is-false/204>\
**Category:** PyMuPDF\
**Created:** [November 24, 2025, 11:37pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-parse-document-produces-keyerror-when-document-use-ocr-is-false/204 "2025-11-24T23:37:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Callum\_Davidson](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/callum_davidson/32/272_2.png) [@Callum\_Davidson](https://forum.mupdf.com/u/Callum_Davidson)\
**Post date:** [November 24, 2025, 11:37pm UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-parse-document-produces-keyerror-when-document-use-ocr-is-false/204/1 "2025-11-24T23:37:29Z")

</div>

Hi,  
Raised this as an issue on the github page before I saw the pinned notice to post here. Apologies. Below is what I posted [here](https://github.com/pymupdf/pymupdf4llm/issues/335):

Parsing a document results in a key error when ocr is disabled. See below traceback from `helpers/document_layout.py` in `parse_document` function.

```auto
849 else:
     850 decision = {"should_ocr": False}
--> [852]if decision["has_ocr_text"]: # prevent MD styling if already OCR'd
     853 page_full_ocred = True
     855 if decision["should_ocr"]:
     856 # We should be OCR: check full-page vs. text-only

KeyError: 'has_ocr_text'

```

This is because when `document.use_ocr` is false it bypasses the `check_ocr.should_ocr_page()` method which would normally populate the `decision` dictionary with the field, and instead produces its own, sparse dictionary which is missing this field. See below for an annotated snippet.

```auto
def parse document (
        ...
        if document.use_ocr:
            decision = check_ocr.should_ocr_page(
                page,
                dpi=ocr_dpi,
                edge_thresh=0.015,
                blocks=blocks,
            )
        else:
            decision = {"should_ocr": False} <- dict created here has no "has_ocr_text" field

        if decision["has_ocr_text"]: <- which KeyErrors here
            page_full_ocred = True
        ...

```

Suggest either: 1). adding this field to the dict when bypassing this method (quick fix) or 2). refactor to use a dataclass or similar for `decision` such that required fields are always present with appropriate defaults.

Thanks!

C.

---

<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 25, 2025, 12:46am UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-parse-document-produces-keyerror-when-document-use-ocr-is-false/204/2 "2025-11-25T00:46:55Z")

</div>

Thanks @Callum_Davidson - I looked at the PR and I think it probably makes sense, will leave to @HaraldLieder as he understands the codebase in depth. Thanks for the contribution!

---

<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:** [November 30, 2025, 8:56am UTC](https://forum.mupdf.com/t/bug-pymupdf4llm-parse-document-produces-keyerror-when-document-use-ocr-is-false/204/3 "2025-11-30T08:56:01Z")

</div>

This has been fixed in the meantime: version 0.2.4.
