RFC: expose conversion diagnostics without changing Markdown output
Problem
MarkItDown is increasingly used in ingestion pipelines where a Markdown string alone is not enough to operate safely. Callers often need to know which converter handled an input, which charset was selected, whether a fallback was used, and whether optional/cloud processing was involved. Today they must infer this from filenames, logs, or by reimplementing internal detection.
This makes it difficult to:
- audit or reproduce a conversion;
- detect low-confidence charset or format decisions;
- measure converter coverage in a heterogeneous corpus; and
- decide when a result should be reviewed instead of indexed automatically.
Proposal
Add opt-in diagnostics to DocumentConverterResult, for example:
result = md.convert(path, include_diagnostics=True)
result.diagnostics.converter # PlainTextConverter
result.diagnostics.charset # utf-8 or cp1252
result.diagnostics.charset_confidence # optional numeric/confidence bucket
result.diagnostics.warnings # list[str]
The default result would remain unchanged, and diagnostics should avoid exposing input contents or credentials. A CLI representation such as --diagnostics json could make the same information available to ingestion tools.
Questions for maintainers and users
- Should diagnostics be part of
DocumentConverterResult, a separate callback, or a tracing/logging hook? - Which fields are stable enough to expose as public API (converter identity, MIME type, charset, fallback reason, dependency/cloud usage)?
- Should low-confidence decisions be warnings only, or should callers be able to configure a failure threshold?
- Would a machine-readable CLI mode be useful, and if so should it write diagnostics to stderr or a sidecar file?
I would be happy to help prototype this if the maintainers agree on the API shape.
I'd like to contribute a bounded first increment here. Would this API shape fit the project's direction?
result = md.convert(path, include_diagnostics=True)
result.diagnostics.converter # e.g. "HtmlConverter"
result.diagnostics.warnings # e.g. ("html_recursion_plain_text_fallback",)
The local prototype adds an optional, immutable ConversionDiagnostics value with the successful converter's class name and a tuple of stable warning codes. Diagnostics default to None. Existing converter result constructors still work, and Markdown, title, string conversion, exceptions, and existing Python warnings retain their current behavior.
The first warning covers the existing HTML RecursionError fallback to plain-text extraction. An empty warning tuple only means that no supported diagnostic was reported; it does not imply lossless conversion. Metadata does not include input contents, filenames, URLs, credentials, or exception text.
For this increment, the dispatcher reports the successful outer converter; nested diagnostics are not aggregated. Charset confidence, failed-attempt reporting, cloud involvement, CLI serialization, and failure thresholds would remain separate follow-ups.
Local validation on Python 3.12.14: the HTML and library infrastructure suites passed 197 tests, with 12 skipped under the existing GITHUB_ACTIONS=true skip conditions. New tests cover opt-in output parity, fallback reporting and strict behavior, legacy custom converters, preservation of converter-supplied warning codes, and concurrent opt-in isolation. Black 23.7.0 and git diff --check passed. The full multi-format suite has not been run for this prototype.
Before preparing a PR, I'd appreciate guidance on:
- An optional result field versus a callback for this initial scope.
- Converter class names and stable warning codes as the initial contract.
- Keeping nested results separate initially versus designing a conversion tree now.
Thanks for the bounded prototype and validation. Please proceed with a PR using the optional immutable result field for this first increment.
Recommended contract for the initial PR:
- Keep
DocumentConverterResult.diagnosticsasNoneby default so existing callers, constructors, Markdown output,str()behavior, and serialization remain unchanged. - Expose a public immutable
ConversionDiagnosticsvalue withconverter: strandwarnings: tuple[str, ...]. Use the converter class name and stable snake_case warning codes as the initial contract; do not expose exception text, URLs, filenames, or input content. - Preserve diagnostics supplied by converters and ensure the dispatcher does not overwrite them. The outer successful converter should be reported; do not aggregate nested conversions yet.
- Keep strict behavior and existing Python warnings unchanged. The HTML recursion fallback warning is an appropriate first warning code, provided it is emitted only when that fallback actually occurs.
- Add tests for opt-in output parity, default
None, fallback warning and strict mode, legacy custom converter compatibility, preservation of converter-provided warnings, and concurrent isolation as described.
Please include a short API/design note in the PR description explaining that charset confidence, failed attempts, cloud involvement, CLI JSON, thresholds, and nested conversion trees are intentionally deferred follow-ups. This scope is reviewable and gives us a stable foundation without prematurely committing to a tracing model.