windlass.interfaces.loader¶
loader
¶
The document-loader interface.
A loader turns a source — a path, a glob, a URL, a byte stream — into
:class:~windlass.core.types.Document objects. Format detection is automatic:
:func:~windlass.rag.loading.load inspects the extension and MIME type and picks
the right registered loader, so .ingest("./docs") handles a folder of mixed
PDFs, spreadsheets and Markdown without any configuration.
Implementers override one coroutine, :meth:Loader.aload_source.
Example
import tempfile, pathlib p = pathlib.Path(tempfile.mkdtemp()) / "note.txt" _ = p.write_text("hello world") from windlass.providers.loaders.text import TextLoader TextLoader().load(p)[0].content 'hello world'
Loader
¶
Loader(
*,
encoding: str = "utf-8",
metadata: dict[str, Any] | None = None,
recursive: bool = True,
on_error: str = "skip",
name: str | None = None,
**config: Any
)
Bases: Component
Abstract document loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding
|
str
|
Text encoding used for character-based formats. |
'utf-8'
|
metadata
|
dict[str, Any] | None
|
Extra metadata merged into every produced document — useful for tagging a whole corpus with a tenant or collection id. |
None
|
recursive
|
bool
|
Whether directory sources are walked recursively. |
True
|
on_error
|
str
|
|
'skip'
|
name
|
str | None
|
Component name for traces. |
None
|
**config
|
Any
|
Loader-specific options. |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
extensions |
tuple[str, ...]
|
File suffixes this loader claims, used for auto-detection. |
mimetypes |
tuple[str, ...]
|
MIME types this loader claims. |
Example
Implementing a loader takes one method::
class MyLoader(Loader):
provider_name = "mine"
extensions = (".mine",)
async def aload_source(self, source):
return [Document(content=read(source), source=str(source))]
Source code in src\windlass\interfaces\loader.py
aload_source
abstractmethod
async
¶
Load one concrete source.
Called once per file. Directory expansion and error handling are done
for you by :meth:aload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
SourceLike
|
A single path, URL or byte payload. |
required |
Returns:
| Type | Description |
|---|---|
list[Document]
|
The documents extracted from it. A PDF may return one document per |
list[Document]
|
page or one for the whole file — that is the loader's choice. |
Raises:
| Type | Description |
|---|---|
IngestionError
|
When the source cannot be parsed. |
Source code in src\windlass\interfaces\loader.py
can_handle
classmethod
¶
Return whether this loader claims source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
SourceLike
|
Path, URL or payload to test. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the extension or scheme matches this loader. |
Example
from windlass.providers.loaders.text import TextLoader TextLoader.can_handle("notes.txt") True TextLoader.can_handle("scan.pdf") False
Source code in src\windlass\interfaces\loader.py
aload
async
¶
aload(
source: SourceLike | Sequence[SourceLike], *, concurrency: int | None = None
) -> list[Document]
Load one or many sources.
Directories are expanded (respecting :attr:recursive and this loader's
:attr:extensions), and files are read concurrently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
SourceLike | Sequence[SourceLike]
|
A path, URL, byte payload, or a sequence of them. A directory path expands to its matching files. |
required |
concurrency
|
int | None
|
Maximum simultaneous reads. Defaults to the global
|
None
|
Returns:
| Type | Description |
|---|---|
list[Document]
|
Every extracted document, with :attr: |
Raises:
| Type | Description |
|---|---|
IngestionError
|
When |
Performance
Blocking parsers run on the thread pool, so a folder of 500 PDFs is parsed in parallel rather than serially.
Source code in src\windlass\interfaces\loader.py
load
¶
astream_load
async
¶
Yield documents one at a time instead of building a list.
Use this for corpora too large to hold in memory: combined with
Pipeline.aingest_stream it keeps peak memory flat regardless of
corpus size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
SourceLike | Sequence[SourceLike]
|
A path, URL, payload, or sequence of them. |
required |
Yields:
| Type | Description |
|---|---|
AsyncIterator[Document]
|
Documents as each source finishes parsing. |