Skip to content

feat(backend): add opt-in metadata extraction for declarative backends - #5543

Open
LowmistLMT wants to merge 1 commit into
docling-project:mainfrom
LowmistLMT:feat/document-metadata-extraction
Open

LowmistLMT wants to merge 1 commit into
docling-project:mainfrom
LowmistLMT:feat/document-metadata-extraction

Conversation

@LowmistLMT

Copy link
Copy Markdown

Add an opt-in extract_metadata: bool = False option on BaseBackendOptions and a structured DocumentMetadata Pydantic model to extract document-level metadata across four pure-Python declarative backends (Markdown, EPUB, HTML/MHTML, and OpenDocument ODT/ODP/ODS) with zero new dependencies.

Related to #2273 and docling-project/docling-core#544.

Summary of changes

  • docling/datamodel/document_metadata.py: Added a typed DocumentMetadata Pydantic model (title, authors, date, language, description, keywords, publisher, identifier) with normalization, deduplication, and DocumentMetadata.from_mapping() / attach_document_metadata() helpers. Metadata is stored under doc.body.meta["docling__document_metadata"] (merging into any existing doc.body.meta keys rather than overwriting them, following the existing vlm_pipeline.py convention).
  • docling/datamodel/backend_options.py: Added extract_metadata: bool = False to BaseBackendOptions so existing outputs remain unchanged by default.
  • docling/backend/md_backend.py: Preserved parsed YAML frontmatter instead of discarding it and attached structured metadata when extract_metadata=True (including when delegating HTML-block Markdown).
  • docling/backend/epub_backend.py: Wired OPF Dublin Core metadata (dc:title, multiple dc:creator / dc:subject, dc:date, dc:language, dc:description, dc:publisher, dc:identifier) into doc.body.meta when extract_metadata=True, addressing the existing TODO in convert().
  • docling/backend/html_backend.py: Extracted <title>, <html lang|xml:lang>, <link rel="canonical">, and standard <meta> tags (OpenGraph, Dublin Core, Google Scholar citation_*, article:*, author, keywords, description), splitting comma-separated author/keywords values while preserving commas inside single-author fields such as citation_author.
  • docling/backend/opendocument_backend.py: Extracted odfdo document metadata (dc:title, dc:creator/meta:initial-creator, dc:date/meta:creation-date, dc:language, dc:description/dc:subject, meta:keyword including comma-separated values) in _OdfBaseBackend for ODT, ODP, and ODS.
  • tests/test_document_metadata.py: Added offline unit tests covering all four backend families, opt-in default behavior, existing doc.body.meta preservation, empty/malformed inputs, and model normalization.

Usage example

from docling.backend.md_backend import MarkdownDocumentBackend
from docling.datamodel.backend_options import DeclarativeBackendOptions
from docling.datamodel.base_models import InputFormat
from docling.document_converter import DocumentConverter, FormatOption
from docling.pipeline.simple_pipeline import SimplePipeline

converter = DocumentConverter(
    format_options={
        InputFormat.MD: FormatOption(
            pipeline_cls=SimplePipeline,
            backend=MarkdownDocumentBackend,
            backend_options=DeclarativeBackendOptions(extract_metadata=True),
        )
    }
)
result = converter.convert("example.md")
metadata = result.document.body.meta.model_dump(exclude_none=True)[
    "docling__document_metadata"
]

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

Review evidence:

  • Behavior before: Markdown stripped and discarded YAML frontmatter; EPUB parsed OPF metadata into self.metadata with a TODO comment but did not attach it to DoclingDocument; HTML and OpenDocument backends did not extract document-level metadata into doc.body.meta.
  • Behavior after: When extract_metadata=False (default), document trees and exports are completely unchanged. When extract_metadata=True, all four declarative backend families populate doc.body.meta["docling__document_metadata"] with a normalized DocumentMetadata payload while preserving any pre-existing doc.body.meta entries and existing document-tree structure.
  • Reference data: No reference test data was modified because extraction is strictly opt-in (extract_metadata=False by default). uv.lock is unchanged.
  • Tests run:
    • NO_PROXY=.localhost uv run pytest -v tests/test_document_metadata.py tests/test_backend_markdown.py tests/test_backend_epub.py tests/test_backend_html.py tests/test_backend_opendocument.py
    • Result: 398 passed, 0 failed, 0 skipped (test_document_metadata.py: 6/6, test_backend_markdown.py: 93/93, test_backend_epub.py: 25/25, test_backend_html.py: 173/173, test_backend_opendocument.py: 101/101).
  • Checks run:
    • make check — all 9 checks passed (ruff format --check, ruff check, ty check with 0 new warnings vs. HEAD, tach check, check_tach_module_coverage.py, check_max_lines.py, check_skill_routes.py, dprint check, uv lock --locked).
    • make validate and git diff --check — passed.
  • Checks not run: Full repository test suite requiring GPU/OCR/VLM model downloads was not run locally (changes are isolated to pure-Python declarative backends and BaseBackendOptions).

… declarative backends

Signed-off-by: lmtyy <3512819960@qq.com>
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @lmtyy, 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) enhancement New feature or request epub issue related to EPUB backend html issue related to html backend markdown issue related to markdown backend odf Issue related to the OpenDocument format (ODT, ODS, ODP) backend priority:medium labels Nov 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

The PR adds an opt-in extract_metadata option to BaseBackendOptions. It adds a DocumentMetadata model. The Markdown, EPUB, HTML, and OpenDocument backends use them to store document metadata in doc.body.meta. The default output does not change.

Topic labels added: enhancement, epub, html, markdown, odf, priority:medium.

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

2 related pull request(s) in the same code
PR State Signal Assessment
#5328 open changes the same code related: It adds a GitHub JSON backend and a preserve_code_whitespace option. It touches the same backend and options files, but the changes have a different purpose and do not conflict.
#2284 closed also references issue #2273 related: It is closed and not merged. It adds an extract_metadata() method for the USPTO backend. It refers to the same issue but uses a different design and different files.

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 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 6398672621. It read the code and did not run it.

Checked opt-in metadata extraction across Markdown, HTML, EPUB, and OpenDocument backends, verifying that default conversions remain unchanged, frontmatter parsing handles invalid YAML safely, and metadata merges into existing document metadata without data loss.

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

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

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

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) enhancement New feature or request epub issue related to EPUB backend html issue related to html backend markdown issue related to markdown backend odf Issue related to the OpenDocument format (ODT, ODS, ODP) backend priority:medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant