DOCXToDocument drops URL fragments stored separately in Word hyperlinks

Chiusa Adatta ai principianti
#13,199 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@anakin87 ci sta già lavorando.

Dal 11/10/2026.

  • #13207 di @amengnew — integrata

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
data

Direzione di ricerca

La gestione dei link si trova in _process_links_in_paragraph() nel convertitore DOCX, che legge Hyperlink.address; la issue segnala che Hyperlink.url mantiene il frammento w:anchor memorizzato separatamente. Inizia controllando lo script di riproduzione e i test del convertitore DOCX. È completato quando sia link_format="markdown" sia "plain" mantengono il frammento, link_format="none" resta invariato e un test di regressione copre un hyperlink con w:anchor.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

DOCXToDocument drops the section part of an external hyperlink when Word stores the URL fragment separately in w:anchor. This happens with both link_format="markdown" and "plain".

For example, a link to https://example.org/manual#refund-policy becomes https://example.org/manual. The link text is still there, but the extracted link no longer points to the intended section. The same URL works when the fragment is stored directly in the relationship address.

The python-docx documentation explains that Word uses both representations. I found this with a small generated DOCX, rather than a production document.

Error message

No exception; the URL fragment is silently omitted.

Expected behavior

The complete external link target should be preserved in both output formats:

markdown '[Refund policy](https://example.org/manual#refund-policy)'
plain 'Refund policy (https://example.org/manual#refund-policy)'

To reproduce

from io import BytesIO

from docx import Document as WordDocument
from docx.opc.constants import RELATIONSHIP_TYPE
from docx.oxml import OxmlElement
from docx.oxml.ns import qn

from haystack.components.converters import DOCXToDocument
from haystack.dataclasses import ByteStream

doc = WordDocument()
paragraph = doc.add_paragraph()
rid = paragraph.part.relate_to(
    "https://example.org/manual", RELATIONSHIP_TYPE.HYPERLINK, is_external=True
)
link = OxmlElement("w:hyperlink")
link.set(qn("r:id"), rid)
link.set(qn("w:anchor"), "refund-policy")
run = OxmlElement("w:r")
text = OxmlElement("w:t")
text.text = "Refund policy"
run.append(text)
link.append(run)
paragraph._p.append(link)

buffer = BytesIO()
doc.save(buffer)
source = ByteStream(data=buffer.getvalue())

for mode in ("markdown", "plain"):
    result = DOCXToDocument(link_format=mode).run(sources=[source])
    print(mode, repr(result["documents"][0].content))

Actual output:

markdown '[Refund policy](https://example.org/manual)'
plain 'Refund policy (https://example.org/manual)'

Additional context / proposed fix

_process_links_in_paragraph() uses Hyperlink.address, which excludes a separately stored fragment. Hyperlink.url includes it. I tried using url locally: all nine reproduction cases passed, including ordinary URLs, inline fragments, and link_format="none". The 40 existing DOCX converter tests also passed. These were focused local checks, rather than the full test suite or CI.

Would you be happy for me to pick this up and send a focused fix? I would add a regression test and release note. I'll wait for your confirmation before opening a PR.

I used Codex to help investigate the behavior and run the local checks.

FAQ check

  • Checked the FAQ.

System

  • OS: macOS, arm64
  • Python: 3.13.9
  • Haystack: 3.4.0-rc0, tested at 5fa8cff71d665ca9d179bd222965adaaefca0db7
  • python-docx: 1.2.0
  • Checks run through Hatch 1.18.1
Lingua principale
Python
Stelle
26.9k
Fork
3.4k
Merge medio
21h 54m
PR unite (30g)
211

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di deepset-ai/haystack

Tutte le issue di deepset-ai/haystack

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.