AutoLoader
On this page
Module: auto_loader · Does not subclass BaseDocumentLoader — a pure dispatcher class with classmethods
Overview
The recommended entry point for most use cases. AutoLoader inspects a source string (file path, URL, or directory path) and routes it to the correct concrete loader, so callers don't need to know or care which loader class handles .pdf vs .csv vs a URL.
from zerda.document_loaders import AutoLoader
docs = AutoLoader.load("report.pdf")
docs = AutoLoader.load("data.csv")
docs = AutoLoader.load("https://example.com/page")
docs = AutoLoader.load("./my_documents/")
Signature
class AutoLoader:
@classmethod
def load(cls, source: str, **kwargs) -> List[LoadedDocument]: ...
@classmethod
def get_loader(cls, source: str, **kwargs) -> BaseDocumentLoader: ...
@classmethod
def detect_type(cls, source: str) -> str: ...
All three are classmethods — AutoLoader is never instantiated; it's used as a namespace, similar to a static utility class.
load(source, **kwargs) -> List[LoadedDocument]
Parameters
Returns
List[LoadedDocument] — equivalent to calling cls.get_loader(source, **kwargs).load().
Raises
ValueError— ifsourceis a file with an extension not present inEXTENSION_MAP(message lists all supported extensions)FileNotFoundError— propagated from the underlying loader's construction if a file/directory doesn't exist- Any other exception the selected concrete loader's constructor or
load()raises
Algorithm — get_loader(source, **kwargs)
source = str(source).strip()
if source.startswith(("http://", "https://", "ftp://")):
return WebLoader(url=source, **kwargs)
path = Path(source)
if path.is_dir():
return DirectoryLoader(path=source, **kwargs)
return cls._create_file_loader(path, **kwargs)
Detection order, each check short-circuiting the next:
- URL scheme prefix match —
startswith(("http://", "https://", "ftp://")). Note: this is a simple prefix check, not a full URL validation; a malformed string that happens to start with"http://"will still be routed toWebLoaderand fail at fetch time, not at dispatch time. - Directory check —
Path(source).is_dir(). This performs an actual filesystem check, so it requires the path to exist; a non-existent path falls through to file-extension dispatch. - Extension lookup in
EXTENSION_MAP, delegating to_create_file_loader, which imports and constructs the matching loader class (TextLoader,MarkdownLoader,PDFLoader,DocxLoader,CSVLoader,ExcelLoader,JSONLoader,JSONLinesLoader, orHTMLLoader) — raisingValueErrorif the extension is unrecognized.
Examples
Basic — any supported type:
docs = AutoLoader.load("article.md")
docs = AutoLoader.load("dataset.csv")
docs = AutoLoader.load("page.html")
Passing loader-specific options through:
docs = AutoLoader.load("big_report.pdf", per_page=True, password="secret")
# kwargs forwarded to PDFLoader(file_path, per_page=True, password="secret")
URL:
docs = AutoLoader.load("https://docs.python.org/3/")
Directory:
docs = AutoLoader.load("./my_docs/", recursive=False, silent_errors=True)
# kwargs forwarded to DirectoryLoader
Error handling — unsupported extension:
try:
docs = AutoLoader.load("archive.zip")
except ValueError as e:
print(e) # "Unsupported file type: '.zip' for file archive.zip\nSupported extensions: [...]"
Production usage — getting the loader without loading immediately, to configure further:
loader = AutoLoader.get_loader("report.pdf")
print(type(loader).__name__) # "PDFLoader"
loader.config.per_page = False
docs = loader.load()
get_loader(source, **kwargs) -> BaseDocumentLoader
Same dispatch logic as load(), but returns the constructed, unexecuted loader instance instead of calling .load() on it. Useful when you want to inspect or mutate the loader's config before running it, as shown in the "Production usage" example above.
detect_type(source) -> str
Returns
A short type string, without constructing any loader or touching the loader registry — purely string/path inspection:
"web"if the source starts with a recognized URL scheme"directory"ifPath(source).is_dir()- the
LoaderType.value(e.g."pdf","csv","json") if the extension is inEXTENSION_MAP "unknown"otherwise
Examples
AutoLoader.detect_type("report.pdf") # "pdf"
AutoLoader.detect_type("https://x.com") # "web"
AutoLoader.detect_type("./data/") # "directory" (only if it exists and is a dir)
AutoLoader.detect_type("file.xyz") # "unknown"
This is useful for pre-validating a batch of sources before committing to loading them, e.g. filtering out "unknown" sources up front.
Side effects
None directly — AutoLoader itself performs no I/O. All I/O happens inside whichever concrete loader it delegates to.
Thread safety
Stateless classmethods with no shared mutable state — safe to call concurrently from multiple threads. Thread safety of the delegated loader's load() call depends on that loader's own guarantees (see each loader's page).
Complexity
O(1) dispatch overhead (a few string checks and a dict lookup); total complexity is dominated by whichever concrete loader is selected.
Best practices
- Prefer
AutoLoader.load()for general-purpose ingestion pipelines where the input format varies and you don't want format-specific branching logic scattered through your code. - Use
AutoLoader.get_loader()instead of.load()when you need to set aconfigobject with non-default values before loading —**kwargsonly covers each loader's direct constructor parameters, not nested config object construction. - Use
detect_type()for fast pre-flight filtering of large source lists before any actual loading begins.
Common mistakes
- Passing a
config=kwarg throughAutoLoader.load(source, config=my_config)expecting it to merge with other**kwargs— it works (since**kwargsis forwarded directly to the constructor, andconfigis a valid named parameter on every concrete loader), but mixingconfig=with other format-specific kwargs in the same call can be confusing; prefer usingget_loader()and configuring the returned instance directly for complex cases. - Forgetting that
AutoLoaderdoes not know aboutGraphLoader,TableLoader, or themultimodal_loaderclasses — these are not wired intoEXTENSION_MAP/_create_file_loaderand must be imported and used explicitly. SeeMultimodalAutoLoaderfor a separate, parallel auto-dispatcher covering OCR/multimodal formats. - Relying on
detect_type()to validate that a file actually exists — for file extensions it only inspects the suffix string, not the filesystem, so a non-existent.pdfpath still returns"pdf".
Related APIs
EXTENSION_MAP,LoaderTypeDirectoryLoader,WebLoader— the two non-extension-based dispatch targetsMultimodalAutoLoader— the parallel dispatcher for OCR/image/multimodal formatsLoadedDocument