From ffe649b2842ea0f9e81da48cb536ed3a96fab1f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=B0=E8=A8=80?= Date: Thu, 2 Jul 2026 17:06:51 +0800 Subject: [PATCH] fix(ipynb): catch decoding exceptions in accepts() to prevent pipeline crashes on non-decodable application/json files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 辰言 --- .../src/markitdown/converters/_ipynb_converter.py | 2 ++ packages/markitdown/tests/test_module_misc.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) 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()