BerriAI/litellm · error · ValueError

File is empty or could not be read

Error message

File is empty or could not be read

What it means

Raised after reading the OCR input file when file_bytes ends up empty (0-byte file, unreadable stream, or read() returning nothing): an empty data URI would produce a useless provider request, so it is rejected.

Source

Thrown at litellm/ocr/main.py:543

        with open(file_path, "rb") as f:
            file_bytes = f.read()
    elif isinstance(file_input, bytes):
        file_bytes = file_input
    elif isinstance(file_input, IOBase) or hasattr(file_input, "read"):
        if hasattr(file_input, "name"):
            file_name = getattr(file_input, "name", None)
            if file_name:
                mime_type = get_mime_type(file_name)
        file_bytes = file_input.read()
        if isinstance(file_bytes, str):
            file_bytes = file_bytes.encode("utf-8")
    else:
        raise ValueError(
            f"Unsupported file input type: {type(file_input)}. Expected pathlib.Path, bytes, or a file-like object."
        )

    if not file_bytes:
        raise ValueError("File is empty or could not be read")

    if "mime_type" in document:
        mime_type = document["mime_type"]

    if not _MIME_PATTERN.match(mime_type):
        raise ValueError(f"Invalid MIME type: {mime_type}")

    base64_data: Final = base64.b64encode(file_bytes).decode("utf-8")
    data_uri: Final = f"data:{mime_type};base64,{base64_data}"

    if mime_type.startswith("image/"):
        verbose_logger.debug(
            "OCR file input: Converted file to image_url data URI (mime=%s, size=%s bytes, name=%s)",
            mime_type,
            len(file_bytes),
            file_name,
        )
        return {"type": "image_url", "image_url": data_uri}

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Check the file has non-zero size and read permissions.
  2. Re-export or re-download the file if it is truncated.

Example fix

os.path.getsize(path) > 0 before calling
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at litellm/ocr/main.py:543 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/86dc692eaa5ef296. Report an issue: GitHub.