fix: IpynbConverter.accepts() raises UnicodeDecodeError on non-ASCII files (e.g. French PDFs)#1895
Open
echavet wants to merge 2 commits into
Open
Conversation
Add ConversionProgress dataclass and ProgressCallback Protocol to enable real-time progress reporting during document conversion. Converters emit progress events for each logical unit processed: - PdfConverter: per page - PptxConverter: per slide - EpubConverter: per chapter - XlsxConverter / XlsConverter: per sheet The callback is optional and passed via kwargs (progress_callback). Converters that do not support progress simply ignore it. Fully backward-compatible — no changes to existing API signatures. Signed-off-by: Eric Chavet <echavet@gmail.com>
…I files accepts() must never raise — it is a predicate that returns True/False. When a file contains non-ASCII bytes (e.g. a French PDF with accented characters encoded as multi-byte UTF-8 sequences like 0xc3...), decoding with 'ascii' or even 'utf-8' can fail if the stream contains arbitrary binary content. The fix wraps the decode + check block in a try/except (UnicodeDecodeError, ValueError) and returns False on failure. A file that cannot be decoded is definitively not a Jupyter notebook. Fixes: microsoft#1894
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IpynbConverter.accepts()can raiseUnicodeDecodeErrorwhen processing files with non-ASCII content (e.g. PDFs with French or other accented text). This propagates uncaught through the conversion pipeline and crashes the entire conversion — even though the file has nothing to do with Jupyter notebooks.Problem
The engine in
_markitdown.pyonly protectsaccepts()calls againstNotImplementedError:Any other exception raised by
accepts()propagates up and crashes the caller.IpynbConverter.accepts()reads the file stream and decodes it to look fornbformatmarkers. If the file contains bytes that cannot be decoded (binary content, wrong charset), aUnicodeDecodeErroris raised.Traceback (production environment — Windows Server)
The affected file was a standard French-language PDF invoice. The byte
0xc3is the first byte of a multi-byte UTF-8 sequence representing accented characters (é,è,à, etc.).Root Cause
accepts()is a predicate — per the contract defined in_base_converter.py, it must returnbooland never raise. The decode block was not protected against encoding failures:Note:
stream_info.charsetcan be explicitly set to"ascii"by MIME/charset detection for certain file types, making the issue reproducible even when"utf-8"is the fallback.Fix
Wrap the decode + check block in
try/except (UnicodeDecodeError, ValueError)and returnFalseon failure. A file that cannot be decoded is definitively not a Jupyter notebook.The
finally: file_stream.seek(cur_pos)block is preserved, ensuring the stream position contract is always respected.Compatibility
.ipynbfiles or JSON streamsaccepts()contract: returnsbool, resets stream positionRelated
progress_callbackfeature)Type of Change