Skip to content

Bug: UnicodeDecodeError in JSON conversion when non-ASCII character appears after 4 KB #2359

Closed

Description

When converting long JSON files, a UnicodeDecodeError is raised by IpynbConverter.accepts() if a non-ASCII character appears after the first 4 KB of the file. I believe this occurs because only the first 4 KB are inspected by MarkItDown to guess the charset.

Here is a minimal reproducer:

import json
import tempfile
from pathlib import Path

from markitdown import MarkItDown

# JSON file with the first non-ASCII char after 4096 bytes.
payload = {
    "record": {
        "title": "Example record",
        "abstract": "This is sample test. " * 500,
    },
    "notes": "non-ASCII character: è",
}
text = json.dumps(payload, ensure_ascii=False, indent=2)

md = MarkItDown()

with tempfile.TemporaryDirectory() as temp_dir:
    path = Path(temp_dir) / "input.json"
    path.write_text(text, encoding="utf-8")

    result = md.convert(str(path))

Resulting traceback:

Traceback (most recent call last):
  File "/home/fcatalan/mid_issue/reproducer_4k.py", line 22, in <module>
    result = md.convert(str(path))
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/fcatalan/Desktop/Repos/markitdown/packages/markitdown/src/markitdown/_markitdown.py", line 306, in convert
    return self.convert_local(source, stream_info=stream_info, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fcatalan/Desktop/Repos/markitdown/packages/markitdown/src/markitdown/_markitdown.py", line 360, in convert_local
    return self._convert(file_stream=fh, stream_info_guesses=guesses, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fcatalan/Desktop/Repos/markitdown/packages/markitdown/src/markitdown/_markitdown.py", line 617, in _convert
    _accepts = converter.accepts(file_stream, stream_info, **_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fcatalan/Desktop/Repos/markitdown/packages/markitdown/src/markitdown/converters/_ipynb_converter.py", line 36, in accepts
    notebook_content = file_stream.read().decode(encoding)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10604: ordinal not in range(128)

My setup is

  • MarkItDown: from 'main' (commit 9dc0d6579b8739c9d0671ff205e071e3053c7df1)
  • Python: 3.12.3
  • OS: Ubuntu 24.04

This is similar to the issue reported for PDF conversion in #1894.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions