Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import io
import re
from typing import BinaryIO, Any
from warnings import warn
from charset_normalizer import from_bytes
from .._base_converter import DocumentConverter, DocumentConverterResult
from .._stream_info import StreamInfo
Expand Down Expand Up @@ -105,6 +106,11 @@ def convert(
_trim_outer_blank_rows(rows)

if not rows:
warn(
"CSV input is empty or contains only blank rows; producing empty markdown.",
UserWarning,
stacklevel=2,
)
return DocumentConverterResult(markdown="")

# Pad all rows, including the header, to preserve the widest row.
Expand Down
28 changes: 21 additions & 7 deletions packages/markitdown/tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from markitdown import MarkItDown, StreamInfo


# Record separators


Expand Down Expand Up @@ -72,10 +71,20 @@ def test_csv_long_blank_runs(position: str) -> None:
"all": blank,
}[position]

result = MarkItDown(enable_plugins=False).convert_stream(
io.BytesIO(content),
stream_info=StreamInfo(extension=".csv", charset="utf-8"),
)
if position == "all":
with pytest.warns(
UserWarning,
match="CSV input is empty or contains only blank rows",
):
result = MarkItDown(enable_plugins=False).convert_stream(
io.BytesIO(content),
stream_info=StreamInfo(extension=".csv", charset="utf-8"),
)
else:
result = MarkItDown(enable_plugins=False).convert_stream(
io.BytesIO(content),
stream_info=StreamInfo(extension=".csv", charset="utf-8"),
)

expected = (
"| name | value | |\n"
Expand Down Expand Up @@ -138,8 +147,13 @@ def test_csv_blank_lines_between_rows_are_kept() -> None:
)


def test_csv_all_blank_input_returns_empty_markdown() -> None:
result = _convert_csv(b"\n\n\n")
@pytest.mark.parametrize("data", [b"", b"\n\n\n"])
def test_csv_empty_input_warns_and_returns_empty_markdown(data: bytes) -> None:
with pytest.warns(
UserWarning,
match="CSV input is empty or contains only blank rows",
):
result = _convert_csv(data)

assert result == ""

Expand Down