Skip to content

fix: fall back to Markdown for text files with a generic XML declaration - #5713

Open
zlg-codes wants to merge 1 commit into
docling-project:mainfrom
zlg-codes:fix/txt-generic-xml-declaration
Open

zlg-codes wants to merge 1 commit into
docling-project:mainfrom
zlg-codes:fix/txt-generic-xml-declaration

Conversation

@zlg-codes

@zlg-codes zlg-codes commented Dec 2, 2026 •

Copy link
Copy Markdown

Description

Content probing maps any content starting with <?xml to application/xml. When none of the specialized XML formats matches (no XBRL marker, no recognized DOCTYPE, no doclang root element), _guess_from_content returned None, so a plain-text .txt / .text file whose content merely starts with a generic XML declaration failed conversion with ConversionError: File format not allowed (format None) instead of falling back to InputFormat.MD like other .txt files (per the fallback added in #3311).

The fix falls back to InputFormat.MD in that case, but only for known plain-text extensions (md/markdown/txt/text/qmd/rmd). Unknown extensions keep failing instead of silently becoming Markdown, and documents with a recognized specialized XML format (JATS, USPTO, XBRL, doclang) are unaffected — the fallback only runs when the XML branch matched nothing.

Known limitation: the Markdown export flattens XML structure — <root><item>Hello</item></root> exports as Hello without the element tags. Together with #5689 the text content is preserved, but structural markup is not retained. Mentioned per the review discussion.

Issue resolved by this Pull Request:
Resolves #5709

New Package?

  • Yes
  • No

Version Bump?

  • No (llama-index-core-style core package; docling version not bumped)

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • I added new unit tests to cover this change

Added test_guess_format_txt_with_generic_xml_declaration covering both Path and DocumentStream inputs: .txt/.text with a generic declaration → InputFormat.MD; .txt/.xml with a JATS DOCTYPE → InputFormat.XML_JATS; unknown extension → still None. The new test fails before the change (assert None is InputFormat.MD) and passes after. End-to-end check with the issue's reproducer (DocumentConverter().convert(DocumentStream(name="sample.txt", ...))) now succeeds and exports the text.

Review evidence:

Before: converter.convert(DocumentStream(name="sample.txt", stream=BytesIO(b"<?xml version='1.0' encoding='UTF-8'?>..."))) → ConversionError: File format not allowed (format None), reproduced on main via _guess_format returning None for both path and stream.

After: conversion succeeds (ConversionStatus.SUCCESS), markdown export contains the plain text. tests/test_input_doc.py: 84 passed (full file). make check passes (ruff format/check, dprint, uv lock, skill routes).

Checklist:

Content probing maps any content starting with '<?xml' to
'application/xml'. When none of the specialized XML formats matches
(no XBRL marker, no recognized DOCTYPE, no doclang root element),
_guess_from_content returned None, so a plain-text .txt/.text file
whose content merely starts with a generic XML declaration failed
conversion with "File format not allowed (format None)" instead of
falling back to InputFormat.MD like other .txt files.

Fall back to InputFormat.MD in that case, but only for known
plain-text extensions; unknown extensions keep failing, and
documents with a recognized specialized XML format are unaffected.

Fixes docling-project#5709

Signed-off-by: zlg-codes <light131455@gmail.com>
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @zlg-codes, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Dec 2, 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) bug Something isn't working markdown issue related to markdown backend plain text issue related to plain text input (read by the Markdown backend) priority:low labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

The PR makes _guess_from_content fall back to InputFormat.MD for known plain-text extensions. This applies when the content starts with a generic XML declaration and no specialized XML format matches. It adds a parametrized test.

Topic labels added: bug, priority:low, markdown, plain text.

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

3 related pull request(s) in the same code
PR State Signal Assessment
#5708 open changes the same code related: This PR changes how DocumentStream names with a leading dot give an extension. It touches the same function, but it fixes a different problem.
#5698 open changes the same code related: This PR also changes how DocumentStream names with a leading dot give an extension. It is a different problem from the XML declaration fallback.
#3311 merged referenced as #3311 related: This merged PR added the .txt fallback to InputFormat.MD for text/plain content. The PR under review extends the fallback to the application/xml branch, so it does not repeat that change.

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

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Dec 2, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI first review (advisory): no blocker found

This review covers the full diff at b56f75b4d9. It read the code and did not run it.

I found no defect. The new fallback in docling/datamodel/document.py runs only after the XBRL, USPTO, JATS and doclang checks have all failed. It also runs only for the known Markdown extensions. For a .txt file that starts with <?xml, the old code returned None at that point.

A maintainer makes the merge decision. This review never approves the pull request.

@MohammadHijjawi97 MohammadHijjawi97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I ran this against main with three .txt streams and one .txt path:

input main this PR
sample.txt = <?xml version='1.0' encoding='UTF-8'?><root><item>Hello</item></root> (repro from #5709) skipped, format None success, MD, text 'Hello'
notes.txt = <?xml version='1.0'?>\nThe config below sets the timeout.\n<timeout>30</timeout>\nDone.\n skipped success, text 'The config below sets the timeout. 30 Done.'
same sample.txt as a Path skipped success, 'Hello'

So the detection part works, and tests/test_input_doc.py passes (84 passed) with ruff clean. The JATS-in-.txt and .xyz cases in the new test also look right to me.

One thing to be aware of: the fallback sends the file to the Markdown backend, which drops every HTML/XML tag except <br>. So the conversion now reports success, but the XML declaration and every element name are gone from the text (<timeout>30</timeout> becomes 30). That is the same problem as #5688. I merged this branch with #5689 locally, and then the text is kept literally (<?xml version='1.0'?> The config below sets the timeout. <timeout>30</timeout> Done.).

Two suggestions:

  • Note in the PR (or in a comment) that the output only keeps the markup once #5689 is in, so the two land together or in that order.
  • The new test only checks _guess_format. One end-to-end assertion on the converted text (for example that Hello and root both appear) would catch the tag loss and would start to pass when #5689 merges.

Comment thread docling/datamodel/document.py Resolved
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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) ai:review-lgtm AI first review found no blocker or major issue (advisory) bug Something isn't working markdown issue related to markdown backend plain text issue related to plain text input (read by the Markdown backend) priority:low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.txt files starting with generic <?xml fail with File format not allowed

2 participants