msword/docx: malformed hyperlink address aborts the whole conversion
Bug
A single malformed hyperlink aborts the entire DOCX conversion.
If a document contains a hyperlink whose address is slightly malformed, most commonly a URL with a space in it, which happens constantly in real-world documents, the whole conversion shatters. One bad link takes down the entire document, not just that link, and no DoclingDocument is ever returned.
The conversion dies with:
pydantic_core._pydantic_core.ValidationError: 1 validation error
Input should be a valid URL, invalid international domain name
[type=url_parsing, input_value='http://exa mple.com/bad url', input_type=str]
Under the normal pipeline this surfaces as RuntimeError: Pipeline SimplePipeline failed.
Expected result one malformed hyperlink should degrade gracefully: keep the link's visible text and drop/leave-unresolved the broken target, and finish converting the rest of the document. Expected output for the repro below:
Before link. click me
After link.
Steps to reproduce
Self-contained repro. It builds a .docx with one hyperlink whose address contains a space, then converts it. No external file needed:
from pathlib import Path
from docx import Document
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from docx.oxml.ns import qn
from lxml import etree
from docling.backend.msword_backend import MsWordDocumentBackend
from docling.datamodel.base_models import InputFormat
from docling.datamodel.document import InputDocument
OUT = Path("bad_hyperlink.docx")
doc = Document()
p = doc.add_paragraph("Before link. ")
# A perfectly ordinary real-world URL that happens to contain a space:
r_id = doc.part.relate_to("http://exa mple.com/bad url", RT.HYPERLINK, is_external=True)
hyperlink = etree.SubElement(p._p, qn("w:hyperlink"))
hyperlink.set(qn("r:id"), r_id)
run = etree.SubElement(hyperlink, qn("w:r"))
etree.SubElement(run, qn("w:t")).text = "click me"
doc.add_paragraph("After link.")
doc.save(OUT)
in_doc = InputDocument(
path_or_stream=OUT, format=InputFormat.DOCX, backend=MsWordDocumentBackend
)
result = in_doc._backend.convert() # <-- crashes here on `main`
print(result.export_to_markdown())
Docling version
Docling version: 2.107.0
Docling Core version: 2.85.0
Docling Parse version: 7.0.0
Python: cpython-313 (3.13.6)
Python version
Python 3.13.6
This is a confirmed bug. The issue is in the _get_hyperlink_target() method in the msword backend [1]:
def _get_hyperlink_target(self, hyperlink: Hyperlink) -> AnyUrl | Path | None:
if hyperlink.address:
return (
AnyUrl(hyperlink.address)
if urlparse(hyperlink.address).scheme
else Path(hyperlink.address)
)
return None
The call to AnyUrl(hyperlink.address) has no error handling for ValidationError. When a URL with spaces (or other invalid characters) is encountered, pydantic raises an unhandled exception that crashes the entire conversion.
Notably, the HTML backend already handles this correctly with a try-except [2]:
try:
new_hyperlink = AnyUrl(this_href)
except ValidationError:
new_hyperlink = Path(this_href)
A fix would be to add the same pattern to _get_hyperlink_target() — catch ValidationError and either fall back to Path, attempt URL-encoding whitespace, or return None to drop the broken target while preserving the link text. Something like:
def _get_hyperlink_target(self, hyperlink: Hyperlink) -> AnyUrl | Path | None:
if hyperlink.address:
if urlparse(hyperlink.address).scheme:
try:
return AnyUrl(hyperlink.address)
except ValidationError:
return None # or attempt urllib.parse.quote(hyperlink.address, safe=':/?#[]@!$&\'()*+,;=')
else:
return Path(hyperlink.address)
return None
This would achieve the expected graceful degradation — the link text ("click me") is preserved while the broken URL target is dropped.
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.