Skip to content
Merged
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
15 changes: 15 additions & 0 deletions packages/leann-core/src/leann/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ def suppress_cpp_output(suppress: bool = True):
def extract_pdf_text_with_pymupdf(file_path: str) -> str | None:
"""Extract text from PDF using PyMuPDF for better quality."""
try:
if os.path.getsize(file_path) == 0:
# Empty file: nothing to extract, skip instead of letting
# fitz.EmptyFileError abort the whole build.
return ""

import fitz # PyMuPDF

doc = fitz.open(file_path)
Expand All @@ -186,11 +191,18 @@ def extract_pdf_text_with_pymupdf(file_path: str) -> str | None:
except ImportError:
# Fallback to default reader
return None
except Exception:
# Skip corrupted PDFs instead of aborting the whole build
return ""


def extract_pdf_text_with_pdfplumber(file_path: str) -> str | None:
"""Extract text from PDF using pdfplumber for better quality."""
try:
if os.path.getsize(file_path) == 0:
# Empty file: nothing to extract, skip instead of aborting the build.
return ""

import pdfplumber

text = ""
Expand All @@ -201,6 +213,9 @@ def extract_pdf_text_with_pdfplumber(file_path: str) -> str | None:
except ImportError:
# Fallback to default reader
return None
except Exception:
# Skip corrupted PDFs instead of aborting the whole build
return ""


class LeannCLI:
Expand Down
7 changes: 6 additions & 1 deletion packages/leann-core/src/leann/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ def _leann_cmd() -> list[str]:
def _run_leann(*args, timeout=120):
"""Run a leann CLI command and return (returncode, stdout, stderr)."""
result = subprocess.run(
["leann", *args],
[*_leann_cmd(), *args],
capture_output=True,
text=True,
# The leann CLI prints UTF-8 (emoji, CJK, ...). Decode explicitly:
# text=True alone falls back to the locale encoding (e.g. GBK on
# Chinese Windows) and crashes the subprocess reader thread.
encoding="utf-8",
errors="replace",
timeout=timeout,
)
return result.returncode, result.stdout, result.stderr
Expand Down
Loading