docling icon indicating copy to clipboard operation
docling copied to clipboard

ImportError: cannot import name 'LatexCharsNode' from 'pylatexenc.latexwalker'

Open sims0l0n opened this issue 5 months ago • 5 comments

Bug

Traceback:
C:\Program Files\Python312\Lib\importlib\__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
...\core.py:13: in <module>
    from docling.document_converter import (
.venv\Lib\site-packages\docling\document_converter.py:29: in <module>
    from docling.backend.latex_backend import LatexDocumentBackend
.venv\Lib\site-packages\docling\backend\latex_backend.py:1: in <module>
    from docling.backend.latex import LatexDocumentBackend
.venv\Lib\site-packages\docling\backend\latex\__init__.py:1: in <module>
    from docling.backend.latex.backend import LatexDocumentBackend
.venv\Lib\site-packages\docling\backend\latex\backend.py:10: in <module>
    from pylatexenc.latexwalker import (
E   ImportError: cannot import name 'LatexCharsNode' from 'pylatexenc.latexwalker' (unknown location)
 Check Python Logs for more details.

Steps to reproduce

The issue occurs when loading pytest eventhough the format-latex extension is not installed.

Let me know if you need any additional info.

Docling version

docling-slim[format-pdf,format-office,format-web,models-local,feat-ocr-tesserocr]==2.108.0

sims0l0n avatar Jul 03 '26 04:07 sims0l0n

Hi @sims0l0n! I'm Dosu and I'm helping the IBM Docling team.

This happens because document_converter.py unconditionally imports LatexDocumentBackend at the top level [1], which triggers the full pylatexenc import chain — even when format-latex is not installed. There's no try/except guard or lazy import around it.

A recent PR #3702 (merged June 26, 2026) introduced conditional import guards for the email and markdown backends using a deferred-error pattern (module-level try/except + flag check in __init__), but that PR explicitly notes that the LaTeX, DOCX, PPTX, XLSX, and XML backends still use eager imports and need the same treatment in a follow-up.

So this is a confirmed gap — the LaTeX backend should be getting the same conditional import pattern applied to it. In the meantime, installing pylatexenc (e.g. pip install pylatexenc) should unblock you, even without the full format-latex extra.

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about docling with Dosu Add Dosu to your team

dosubot[bot] avatar Jul 03 '26 04:07 dosubot[bot]

Hi maintainers and @sims0l0n , I dug into this one. The problem is that DocumentConverter imports LatexDocumentBackend eagerly at the top of document_converter.py, and that pulls in pylatexenc at import time across the whole latex subpackage (backend + handlers + utils, 6 files) with no guard around it. So just doing import docling breaks the moment pylatexenc is missing or the wrong version — even for someone who never installed the format-latex extra and only wants PDF/office/web, like the reporter here.

What's happening in this specific case: some other package pulled in pylatexenc 3.x, where the node classes were moved out of pylatexenc.latexwalker, hence the cannot import name 'LatexCharsNode'. Our pylatexenc>=2.10,<3.0 pin lives on the format-latex extra, so since that extra wasn't installed the pin never kicked in, and docling just eagerly imports whatever pylatexenc is lying around. Good news is this is basically the follow-up already called out in #3702 (like @dosu said), that PR added the deferred-import guard to the email + markdown backends and explicitly left latex and docx/pptx/xlsx + the bs4 XML backends for a later pass. So the fix is just applying that same pattern here: guard the import and only raise a friendly error if someone actually tries to parse a .tex file.

In the meantime, a quick workaround is pip install 'pylatexenc>=2.10,<3.0'

Happy to put up a PR applying the guard to the latex backend, and I can fold in the other remaining eager backends as well. Thanks alot!

DanielNg0729 avatar Jul 03 '26 11:07 DanielNg0729

Thanks @sims0l0n for spotting this issue, which indeed was partially solved in #3702 but had to be extended to other backends that import optional libraries. @DanielNg0729 I have created this PR #3751 and I took advantage to tidy up some imports and ensure the deferred-error pattern is consistent across all the backends. Please, check it out and feel free to add any comment!

ceberam avatar Jul 03 '26 12:07 ceberam

@ceberam There is a similar issue with docling-core (https://github.com/docling-project/docling-core/issues/459) and an already proposed fix (https://github.com/docling-project/docling-core/pull/468). Could you please take a look? Thanks!

jspast avatar Jul 03 '26 15:07 jspast

Thank you so much Dr. @ceberam! I really appreciate this. I'll check out the PR carefully and learn from the changes you've made.

DanielNg0729 avatar Jul 03 '26 15:07 DanielNg0729