Skip to content

feat(xml): add wikitext xml backend - #5536

Open
PeterStaar-IBM wants to merge 12 commits into
mainfrom
feat/add-wikitext-xml-backend
Open

PeterStaar-IBM wants to merge 12 commits into
mainfrom
feat/add-wikitext-xml-backend

Conversation

@PeterStaar-IBM

@PeterStaar-IBM PeterStaar-IBM commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Add a backend for MediaWiki XML exports and standalone wikitext (.wiki and .wikitext), with optional gzip compression. Each source file produces one DoclingDocument. For a multi-page XML export, Docling imports the latest revision of each page in export order. The SDK handles a generic ZIP through the input layer and produces one document per supported member.

The parser runs locally with no new dependencies. It preserves headings, lists, tables, links, emphasis, formulas, code languages, and file captions. Captions belong to picture items. Conversion does not fetch images or execute template code or Lua modules.

docling export.xml.gz --from mediawiki --to md

In the Python SDK, use InputFormat.MEDIAWIKI to restrict conversion to MediaWiki inputs. MediaWikiBackendOptions provides independent preserve_bold and preserve_italic settings, citation modes, and size limits. Adjacent runs with the same emphasis merge by default. MediaWiki usage and limits

Review follow-up

  • Removed MediaWiki tar support. Format detection checks the allowed formats before probing for MediaWiki and reads a fixed gzip prefix. METS-GBS retains precedence.
  • Bounded tokenizer work and list depth. The XML reader checks revision length as it reads the text. Inline references count each emitted copy toward a per-file output limit.
  • Added regression tests for those limits, standalone and gzip inputs, generic ZIP batches, revision selection and page order, emphasis settings, and formula, code, and caption output.

Reply to cau-git · Reply to dolfim-ibm

Second review follow-up

  • The XML reader streams only the fields it needs and checks an 8,192-character limit for each retained metadata field. Tests cover oversized metadata and memory use in <revision> and <siteinfo>.
  • A page that reaches the tokenizer work limit is kept as plain text with a warning. List markers beyond level 32 remain visible, and incomplete tags skip parsing when there is no possible closing delimiter.
  • The HTML backend reads TeX from data-math-tex. HTML tests cover formula text and figure caption ownership. The MediaWiki input helper handles gzip probing, and METS-GBS detection retains its limit warnings.

Second reply to dolfim-ibm · Fix commit

Review evidence

On 14a3c28d, make validate and make check passed locally. The MediaWiki, METS-GBS, ZIP, DocLang, HTML, and CLI suites had 392 passes and one skip across the local test runs.

On 1321018e, make validate passed. The same focused Docling suites had 410 passes and one skip. All 10 Wikimedia dataset conversion tests passed against the local Docling checkout.

Checklist

  • Updated the supported formats and usage documentation.
  • Added CLI and SDK usage examples.
  • Added regression tests.

Signed-off-by: Peter Staar <taa@zurich.ibm.com>
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @PeterStaar-IBM, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) CLI issues related to the command line interface (CLI) enhancement New feature or request priority:medium xml issue related to supported schema-specific XML formats labels Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit ed4ec83e6a. The analysis read the diff only. It did not run code from this pull request.

The PR adds a MediaWiki backend. It reads MediaWiki XML exports and .wiki or .wikitext files, with optional gzip. It adds InputFormat.MEDIAWIKI, format detection, CLI and SDK options, and tests. It also adds formula support to html_backend.py and changes how it handles dt and dd elements.

CI safety: no concern found. A maintainer must still approve CI.

3 related pull request(s) in the same code
PR State Signal Assessment
#5673 open changes the same code related: It turns inline SVG into pictures in html_backend.py. This PR changes other parts of the same file, so the two PRs may conflict in merge but do different things.
#5614 open changes the same code related: It keeps lists, tables, and images inside dt in html_backend.py. This PR also changes the dt and dd list code there for nested dl, so the two PRs may conflict. The two fixes differ.
#5633 open changes the same code related: It changes the CSV sniffer in document.py. This PR changes format detection in the same file for a different format.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 25, 2026
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 25, 2026
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 27, 2026
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 27, 2026
@github-actions github-actions Bot added the ai:ci-needs-care AI triage: check CI-safety findings before you approve CI label Nov 27, 2026

@cau-git cau-git left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @PeterStaar-IBM, nice addition. I tested this on fb649e31 and found a few things that need to change before we can merge.

1. Tokenizer time grows quadratically with unclosed tags

An unclosed tag makes the tokenizer search to the end of the page, fail, and parse the text again (_really_parse_tag, same for external links at L408). Tokenizer time only:

Input 2,000× 4,000× 8,000×
<p>para\n 5.1 s (16 KB) 19 s (32 KB) 73 s (64 KB)
<small>x\n 4.9 s 18 s 73 s
[http://a.b 6.8 s 27 s >60 s
from docling.backend.xml.mediawiki._tokenizer import Tokenizer
Tokenizer().tokenize("<p>para\n" * 8000)  # ~73 s

Closed tags, refs and links are linear, so normal pages are fine. But unclosed <p>, <small> or <center> is valid wikitext, and the 4 Mi-char page limit does not help: 1 MB of this takes hours. Please make this linear, for example by remembering failed tag routes per position or by bounding the search for the closing tag, and add a test with a few thousand unclosed tags.

2. Format detection reads every gzip/tar input to the end

is_mediawiki_archive runs in _guess_format for every gzip and tar input, also when MEDIAWIKI is not in allowed_formats. For an archive without wiki content it decompresses up to 512 MiB before it gives up, and the conversion then decompresses the archive again. It also runs before the METS-GBS check: if I add one README.wiki member to the METS test archive, it is detected as MEDIAWIKI.

Please:

  • run this check only when MEDIAWIKI is allowed (the allowed formats need to reach _guess_format for this)
  • run the METS-GBS detection first
  • stop after the first few members instead of scanning the whole archive

3. Archive limits

The limits are hard-coded (_MAX_ARCHIVE_BYTES, _MAX_ARCHIVE_MEMBERS, _MAX_WIKITEXT_* in the backend). EPUB, METS-GBS, iWork and the Office backends expose max_total_bytes, max_file_bytes and max_member_count on their backend options. Please follow the same pattern in MediaWikiBackendOptions.

4. Lost semantics

Not blocking on its own, but worth fixing:

  • <math> becomes a code item instead of a formula (render.py#L624)
  • <syntaxhighlight lang="python"> loses the code language
  • the caption of [[File:...|thumb|caption]] ends up as a plain body paragraph

Thanks.

@dolfim-ibm dolfim-ibm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the review above: one request on scope, three more resource-exhaustion defects, and some smaller points. All numbers are from DocumentConverter on fb649e31.

Scope: remove the tar support

The backend should convert one XML export or one wikitext file. Archive handling belongs in the input layer. main already expands a generic ZIP there into one document per member (#4865), and with this PR a ZIP of .wiki files works through that path with no backend code. The tar reader here gives a second model: all members are joined into one document.

Without tar, the detection cost from point 2 of the review above goes away, and so do defect 1 below and the tar/tgz entries in FormatToExtensions.

Related question: an XML export with many pages also becomes one document. Is that intended?

Defects

  1. Tar extended headers (GHSA-ph55-ff4r-394x). mediawiki_input.py opens tar archives without the limits from tar_limits.py (#5606). A 389 KB .tar.gz gives 2,411 MB peak memory, against 396 MB on main. This runs in format detection, also with allowed_formats=[InputFormat.PDF].

  2. reference_mode="inline". Each <ref name=a/> renders the reference body again. A 152 KB file gives 400 MB of Markdown and 4.2 GB peak memory. The output needs a size budget.

  3. Late page size check. The reader builds the full <revision> before the 4 M character check. A 510 KB .xml.gz reaches 1,522 MB before it fails. The reader should count characters in data() and stop at the limit.

On the tokenizer: the quadratic growth is not limited to tags and external links. {{{a|, {|\n, {{a|[[ and <!-- show it too ("{{{a|" * 1600, 8 KB, takes 8.2 s), so the fix must cover all route types. Also, "*" * 100000 runs for 141 s and then fails with a recursion error, so the list depth needs a limit.

Each guard needs a test with a small hostile input.

Smaller points

  • The xml entry in FormatToExtensions is not needed, because detection is by content.
  • The file headers say "See LICENSE", but that file is no longer in the branch. A header line that names the MIT license and the upstream copyright is sufficient.
  • .github/max-lines-ignore: document.py has 1,480 lines on this head, below the limit. A helper in mediawiki_input.py for the detection blocks avoids the exemption.

Docs and skills

  • supported_formats.md and the skill cli.md are correct. Update them for the scope and limit changes.
  • Skill python-sdk.md: two lines and a link to supported_formats.md are sufficient.
  • Skill service-client.md: remove the line. It is true for each new format.
  • Skill slim-packaging.md: the sentence is inside the list of extras, but it is not an extra. Move it out of the list.

@github-actions github-actions Bot removed the ai:ci-needs-care AI triage: check CI-safety findings before you approve CI label Nov 27, 2026

Copy link
Copy Markdown
Member Author

@cau-git Thanks for the review. I addressed the four points in 14a3c28d.

  • The tokenizer indexes possible closing delimiters so it can skip searches when there is no closer. A work limit also bounds ambiguous cases. The regression tests compare work counts at 1,000 and 2,000 repetitions.
  • Format detection checks allowed_formats before probing for MediaWiki. The gzip probe reads a fixed prefix. MediaWiki tar support is gone, so a wiki member cannot take precedence over METS-GBS.
  • MediaWikiBackendOptions now exposes max_total_bytes, max_file_bytes, and max_page_chars for single-file inputs. The backend has no tar member limit because it no longer reads tar members.
  • <math> produces formula items, <syntaxhighlight> and <source> retain recognized language hints, and a file caption belongs to its picture item. The backend creates the picture item without fetching the image.

The same update merges adjacent bold or italic runs and adds independent preserve_bold and preserve_italic options. Both default to True.

Local validation passed with make validate, make check, and 392 targeted test passes with one skip.

Your review · Fix commit

Copy link
Copy Markdown
Member Author

@dolfim-ibm Thanks for the review. I addressed these points in 14a3c28d.

  • The backend now accepts one XML export or wikitext file, with optional gzip compression. Generic ZIP handling stays in the input layer and returns one document per supported member. A multi-page XML export intentionally returns one DoclingDocument with the latest revision of each page in export order.
  • I removed the MediaWiki tar reader and extension entries. The branch also includes the METS-GBS tar safeguards from main.
  • Inline references render each named body once and charge every output copy to a configurable budget for the whole source file. The XML reader checks the revision character limit in data() before buffering the text.
  • The tokenizer skips impossible routes for incomplete tags, links, templates, arguments, tables, and comments. A shared work limit covers ambiguous cases. List depth is checked before building nested lists. Regression tests cover each guard.
  • I removed the MediaWiki xml extension entry and the document.py line-limit exemption, corrected the upstream MIT attribution, and updated the supported formats page and the four usage references as requested.

Local validation passed with make validate, make check, and 392 targeted test passes with one skip.

Your review · Fix commit

@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 27, 2026

@dolfim-ibm dolfim-ibm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One memory defect remains in the XML reader. The other points from the previous review are resolved on 14a3c28d. All numbers are from DocumentConverter.

Blocker

The XML reader still builds trees without a limit. _ExportReader counts characters only inside <text>. All other content of <revision>, <siteinfo> and <title> goes into a TreeBuilder.

Input (.xml.gz) File size Peak memory
<revision> with 50 MiB of <a/> children 51 KB 1,504 MB
<revision> with 100 MiB of <a/> children 102 KB 2,608 MB
<siteinfo> with 50 MiB of <a/> children 51 KB 1,504 MB
<title> with 100 MiB of text 102 KB 1,323 MB, and a 105 MB heading in the output

The baseline is 398 MB, and max_total_bytes permits 512 MiB. The reader needs only a few fields (base, title, timestamp, id, model, role, text). It should keep those fields with a length limit and discard all other elements without building them. Each case needs a test.

Suggestions

  • Parser limits and multi-page exports. MAX_LIST_DEPTH and the work limit raise ParserError, and the backend reports a failure for the full input. An export with three pages, where one page has a line with 33 : markers, gives status=failure and no output. A 100 KB page with approximately 100 unclosed <small>, {{ or [[ reaches the limit of 64 work units per character and fails in the same way (with 50, the page converts). Both are valid wikitext, so one such page stops the conversion of a dump. It is better to apply the limit to the page only: keep the markers after level 32 as text, and convert a page that reaches the work limit as plain text with a warning.
  • html_backend.py. The changes apply to each HTML input, but only the MediaWiki tests cover them. <li>item <figure><img><figcaption>cap</figcaption></figure></li> now gives item and not item cap, which is a correct fix. <span data-math-tex> is a new contract: <span data-math-tex="E=mc^2">E = mc<sup>2</sup></span> gives $E = mc2$, because the code uses the element text and ignores the attribute value. Add tests to test_backend_html.py and define where the TeX is. A separate PR is also possible.
  • _detect_mets_gbs. The rewrite is not related to this PR and it removes the two warnings for the member count and size limits. document.py is now at 1,499 of 1,500 lines. Keep the warnings, and move the gzip and MediaWiki detection block into mediawiki_input.py to get the space.
  • Tokenizer worst case. A 4.2 MB .wiki file of <a runs for 97 s and reaches 1,441 MB before the work-limit error. The cost is linear and has a limit, but the memory is 250 times the input size.

Question

  • preserve_bold and preserve_italic are new in this push. No other backend has options of this type. What is the use case, and can it go into a separate PR?

Resolved

Tar support is gone, and the tar header input from the previous review stays at 397 MB. reference_mode="inline" stops at the budget (154 KB input, 0.13 s). The <text> size check stops a 510 KB .xml.gz at 404 MB. The tokenizer patterns from both reviews are linear up to 180 KB. The xml extension entry, the license headers, the line-limit exemption, and the docs and skills are as requested. A ZIP of .wiki and export files gives one document for each member.

…cornercases

Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 28, 2026

Copy link
Copy Markdown
Member Author

@dolfim-ibm Thanks for the follow-up. I addressed the XML reader blocker and the parser, HTML, and format detection points in 1321018e.

  • The XML reader now streams the fields it needs and discards other elements without building trees. It checks the 8,192-character metadata limit and the existing revision text limit while reading. The regression tests cover oversized metadata and memory use in <revision> and <siteinfo>.
  • The tokenizer keeps list markers beyond level 32 as literal text. A page that exceeds the work limit is kept as plain text with a warning, so the rest of the export still converts. The repeated incomplete-tag case now skips tag-body parsing and completed in 3.7 seconds through DocumentConverter locally.
  • The HTML backend reads TeX from data-math-tex. The HTML tests cover a formula with different preview text and a figure whose caption belongs to the picture inside a list item.
  • METS-GBS detection again warns when the member-count or XML-size limit is exceeded. The gzip and MediaWiki probe now lives in mediawiki_input.py.

The Wikimedia dataset converter sets preserve_bold and preserve_italic to False to keep text without those styles in its DocLang output. I kept the options here because that converter already uses them.

Locally, make validate passed. The focused Docling suites had 410 passes and one skip. All 10 Wikimedia dataset conversion tests passed.

Your review · Fix commit

Signed-off-by: Peter Staar <taa@zurich.ibm.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 30, 2026
Signed-off-by: Peter Staar <taa@zurich.ibm.com>

# Conflicts:
#	docling/cli/main.py
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 30, 2026

@dolfim-ibm dolfim-ibm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) CLI issues related to the command line interface (CLI) enhancement New feature or request priority:medium xml issue related to supported schema-specific XML formats

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants