run-llama/llama_index · error · Exception

Error loading file

Error message

Error loading file

What it means

Raised by SimpleDirectoryReader._load_data when reader.load_data(input_file, ...) throws a non-ImportError exception and raise_on_error=True. The loader converts any per-file failure (corrupt file, bad encoding, missing parser output) into a generic Exception chained from the original via `raise Exception("Error loading file") from e`; use __cause__ to see the real error. With raise_on_error=False the file is instead skipped with a printed message.

Source

Thrown at llama-index-core/llama_index/core/readers/file/base.py:620

            if file_suffix not in file_extractor:
                # instantiate file reader if not already
                reader_cls = default_file_reader_cls[file_suffix]
                file_extractor[file_suffix] = reader_cls()
            reader = file_extractor[file_suffix]

            # load data -- catch all errors except for ImportError
            try:
                kwargs: dict[str, Any] = {"extra_info": metadata}
                if fs and not is_default_fs(fs):
                    kwargs["fs"] = fs
                docs = reader.load_data(input_file, **kwargs)
            except ImportError as e:
                # ensure that ImportError is raised so user knows
                # about missing dependencies
                raise ImportError(str(e))
            except Exception as e:
                if raise_on_error:
                    raise Exception("Error loading file") from e
                # otherwise, just skip the file and report the error
                print(
                    f"Failed to load file {input_file} with error: {e}. Skipping...",
                    flush=True,
                )
                return []

            # iterate over docs if needed
            if filename_as_id:
                for i, doc in enumerate(docs):
                    doc.id_ = f"{input_file!s}_part_{i}"

            documents.extend(docs)
        else:
            # do standard read
            fs = fs or get_default_fs()
            with fs.open(input_file, errors=errors, encoding=encoding) as f:
                data = cast(bytes, f.read()).decode(encoding, errors=errors)

View on GitHub (pinned to afd0fef371)

Solutions

  1. Inspect the chained cause: catch the exception and print exc.__cause__ to find the real per-file error.
  2. Test the failing file in isolation: SimpleDirectoryReader(input_files=[bad_file]).load_data() to reproduce without the rest of the corpus.
  3. Re-save or repair the offending document (re-export the PDF/DOCX) or remove it from the directory.
  4. For tolerant bulk ingestion, construct SimpleDirectoryReader(..., raise_on_error=False) so bad files are skipped and reported instead of aborting.

Example fix

# before
reader = SimpleDirectoryReader(input_dir="./data", raise_on_error=True)
docs = reader.load_data()  # aborts on first bad file

# after
try:
    docs = SimpleDirectoryReader(input_dir="./data").load_data()
except Exception as e:
    print("real cause:", e.__cause__)
# or skip bad files wholesale:
reader = SimpleDirectoryReader(input_dir="./data", raise_on_error=False)
Defensive patterns

Strategy: try-catch

Try / catch

try:
    docs = reader.load_data()
except Exception as e:
    raise RuntimeError(f"load failed, cause: {e.__cause__}") from e

Prevention

When it happens

Trigger: Ingesting a corrupt or truncated PDF/DOCX; a file whose bytes do not match its extension; a parser dependency (e.g. pypdf, docx2txt) throwing at runtime rather than at import; raise_on_error=True passed to the constructor while the corpus contains a few bad files.

Common situations: Bulk ingestion of user-uploaded documents where some are malformed; partially downloaded files; files with wrong extensions; mixed-quality corpora scraped from the web.

Related errors


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/7410e9b84e322bef. Report an issue: GitHub.