diff --git a/packages/markitdown/src/markitdown/converters/_ipynb_converter.py b/packages/markitdown/src/markitdown/converters/_ipynb_converter.py index b15e77aa2..6e4aa5a71 100644 --- a/packages/markitdown/src/markitdown/converters/_ipynb_converter.py +++ b/packages/markitdown/src/markitdown/converters/_ipynb_converter.py @@ -38,6 +38,8 @@ def accepts( "nbformat" in notebook_content and "nbformat_minor" in notebook_content ) + except (UnicodeDecodeError, ValueError, LookupError): + return False finally: file_stream.seek(cur_pos) diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py index 4d62e4919..9f485b9f0 100644 --- a/packages/markitdown/tests/test_module_misc.py +++ b/packages/markitdown/tests/test_module_misc.py @@ -532,6 +532,19 @@ def test_markitdown_llm() -> None: validate_strings(result, PPTX_TEST_STRINGS) +def test_ipynb_converter_accepts_handling_decode_error() -> None: + from markitdown.converters._ipynb_converter import IpynbConverter + from io import BytesIO + converter = IpynbConverter() + # A stream of non-decodable bytes (binary data like French PDF bytes) + stream = BytesIO(b"\xc3\x28\x91\xff\x00") + # StreamInfo guessing MIME type application/json (to trigger decoding in accepts()) + stream_info = StreamInfo(mimetype="application/json") + + # This should return False gracefully and NOT raise UnicodeDecodeError/ValueError + assert not converter.accepts(stream, stream_info) + + if __name__ == "__main__": """Runs this file's tests from the command line.""" for test in [ @@ -547,6 +560,7 @@ def test_markitdown_llm() -> None: test_markitdown_exiftool, test_markitdown_llm_parameters, test_markitdown_llm, + test_ipynb_converter_accepts_handling_decode_error, ]: print(f"Running {test.__name__}...", end="") test()