refactor(core): deduplicate SimpleDirectoryReader load_file/aload_file - #22371
Open
gaurav0107 wants to merge 1 commit into
Open
gaurav0107 wants to merge 1 commit into
gaurav0107 wants to merge 1 commit into
Conversation
load_file and aload_file duplicated the same branching flow (metadata
computation, reader resolution/instantiation, error handling,
filename_as_id id assignment, and the plain-text fallback read), each
marked "# TODO: make this less redundant".
Extract the shared logic into four private static helpers
(_resolve_file_reader, _reader_load_kwargs, _build_reader_documents,
_read_file_as_document) and rewrite both public methods as thin
wrappers. The helpers stay static so load_file remains picklable for
the multiprocessing num_workers path.
Behavior is preserved, including the deliberate divergence between the
two entry points: load_file wraps reader failures as
Exception("Error loading file") from e, while aload_file re-raises the
original exception. Add an async error-handling regression test that
locks in this divergence (previously only the sync path was covered).
gaurav0107
marked this pull request as ready for review
July 15, 2026 22:31
Contributor
|
This PR is stale because it has been open 50 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SimpleDirectoryReader.load_file(sync) andSimpleDirectoryReader.aload_file(async) in
llama-index-core/llama_index/core/readers/file/base.pyduplicatedthe same ~55-line branching flow — metadata computation, file-reader
resolution/instantiation, error handling,
filename_as_iddoc-id assignment,and the plain-text fallback read. Both carried a
# TODO: make this less redundantmarker.This PR extracts the shared logic into four private static helpers and
rewrites the two public methods as thin wrappers:
_resolve_file_reader— pick (and cache) theBaseReaderfor a suffix, orNonewhen the file should be read as plain text_reader_load_kwargs— build theextra_info/fskwargs for the reader_build_reader_documents— applyfilename_as_idids to reader output_read_file_as_document— the plain-text fallback readThe helpers are kept static so
load_filestays picklable for themultiprocessing
num_workerspath (load_datausespartial(SimpleDirectoryReader.load_file, ...)+pool.imap).Behavior is preserved, including the deliberate divergence between the two
entry points that the issue calls out:
load_filewraps reader failures asException("Error loading file") from e, whileaload_filere-raises theoriginal exception. No public signature or default behavior changes.
Fixes #21428
Type of Change
(Refactor / code-quality cleanup — non-breaking, behavior-preserving.)
How Has This Been Tested?
Ran
pytest tests/readers/file/test_base.pyinllama-index-core— 24/24 pass,including the existing behavior-preservation tests (
load_file,load_file_error— which asserts the syncException("Error loading file")wrap,
load_file_unknownplain-text path,load_data,aload_data,iter_data) and a newtest_SimpleDirectoryReader_aload_file_errorthatlocks in the async path's divergent behavior (re-raises the original
exception, skips on
raise_on_error=False, always re-raisesImportError).Also verified the multiprocessing
load_data(num_workers=2)andfilename_as_idpaths, and ranruff check,ruff format --check, andmypyon the changed file — all clean.
Suggested Checklist:
uv run make format; uv run make lintto appease the lint godsVersion Bump?
llama-index-coreis exempt from the per-package version-bump rule.New Package?