Skip to content

Commit 149f8b2

Browse files
JSap0914afourney
andauthored
fix: ZipConverter renders '(unknown)' instead of literal 'None' when stream has no source info (#2134)
* fix: ZipConverter emits '(unknown)' instead of 'None' when stream has no source info When MarkItDown.convert_stream() is called with a ZIP stream that has no associated URL, local_path, or filename (e.g. a raw io.BytesIO), the ZipConverter header read: Content from the zip file `None`: because stream_info.url, stream_info.local_path, and stream_info.filename were all None and Python f-strings render None as the literal string 'None'. Fix: fall back to '(unknown)' when all three source-info fields are absent, producing the more descriptive: Content from the zip file `(unknown)`: Add a regression test in test_module_misc.py that verifies the output does not contain the literal string 'None' in this scenario. * Fixed formatting. --------- Co-authored-by: JSap0914 <JSap0914@users.noreply.github.com> Co-authored-by: afourney <adamfo@microsoft.com>
1 parent 3661853 commit 149f8b2

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

packages/markitdown/src/markitdown/converters/_zip_converter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ def convert(
9090
stream_info: StreamInfo,
9191
**kwargs: Any, # Options to pass to the converter
9292
) -> DocumentConverterResult:
93-
file_path = stream_info.url or stream_info.local_path or stream_info.filename
93+
file_path = (
94+
stream_info.url
95+
or stream_info.local_path
96+
or stream_info.filename
97+
or "(unknown)"
98+
)
9499
md_content = f"Content from the zip file `{file_path}`:\n\n"
95100

96101
with zipfile.ZipFile(file_stream, "r") as zipObj:

packages/markitdown/tests/test_module_misc.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import re
77
import shutil
8+
import zipfile
89
from types import SimpleNamespace
910
import pytest
1011
from unittest.mock import MagicMock
@@ -785,6 +786,24 @@ def test_markitdown_llm() -> None:
785786
validate_strings(result, PPTX_TEST_STRINGS)
786787

787788

789+
def test_zip_stream_no_filename_header() -> None:
790+
"""Regression test: ZipConverter must not render the literal string 'None'
791+
in the output header when the stream has no associated URL, local path, or
792+
filename (e.g. when called via convert_stream() without stream_info)."""
793+
markitdown = MarkItDown()
794+
795+
buf = io.BytesIO()
796+
with zipfile.ZipFile(buf, "w") as zf:
797+
zf.writestr("hello.txt", "Hello world")
798+
buf.seek(0)
799+
800+
result = markitdown.convert_stream(
801+
buf, stream_info=StreamInfo(mimetype="application/zip")
802+
)
803+
assert result.markdown.startswith("Content from the zip file `(unknown)`:\n\n")
804+
assert "Hello world" in result.markdown
805+
806+
788807
def test_ipynb_accepts_non_ascii() -> None:
789808
"""IpynbConverter.accepts() must not raise on non-ASCII binary content."""
790809
from markitdown.converters._ipynb_converter import IpynbConverter

0 commit comments

Comments
 (0)