Implementation:Run llama Llama index OnDemandLoaderTool
Overview
The OnDemandLoaderTool class provides an ad-hoc data loading capability within the LlamaIndex tool framework. It wraps any data loader (reader) or function tool and, when called, loads documents on demand, indexes them into a vector store (or other index type), and queries the index with a natural language query string. This enables agents to dynamically ingest and query external data sources at runtime.
Source File: llama-index-core/llama_index/core/tools/ondemand_loader_tool.py
Module: llama_index.core.tools.ondemand_loader_tool
Lines of Code: 169
Dependencies
| Dependency | Type | Purpose |
|---|---|---|
llama_index.core.bridge.pydantic.BaseModel |
Internal | Base model for Pydantic schema generation |
llama_index.core.indices.base.BaseIndex |
Internal | Abstract base class for all index types |
llama_index.core.indices.vector_store.VectorStoreIndex |
Internal | Default index type used for document indexing |
llama_index.core.readers.base.BaseReader |
Internal | Base class for data readers/loaders |
llama_index.core.schema.Document |
Internal | Document data model |
llama_index.core.tools.function_tool.FunctionTool |
Internal | Function-based tool (used in from_tool factory)
|
llama_index.core.tools.types.AsyncBaseTool |
Internal | Async-capable base tool class |
llama_index.core.tools.types.ToolMetadata |
Internal | Tool metadata container |
llama_index.core.tools.types.ToolOutput |
Internal | Standard tool output wrapper |
llama_index.core.tools.utils.create_schema_from_function |
Internal | Automatic schema generation from function signatures |
Class: OnDemandLoaderTool
class OnDemandLoaderTool(AsyncBaseTool)
Inheritance
Extends AsyncBaseTool, making it compatible with both synchronous and asynchronous agent execution contexts.
Constructor
def __init__(
self,
loader: Callable[..., List[Document]],
index_cls: Type[BaseIndex],
index_kwargs: Dict,
metadata: ToolMetadata,
use_query_str_in_loader: bool = False,
query_str_kwargs_key: str = "query_str",
) -> None
| Parameter | Type | Description |
|---|---|---|
loader |
Callable[..., List[Document]] |
The callable that loads documents (e.g., reader.load_data)
|
index_cls |
Type[BaseIndex] |
The index class to use for indexing loaded documents |
index_kwargs |
Dict |
Additional keyword arguments passed to the index constructor |
metadata |
ToolMetadata |
Tool metadata (name, description, schema) |
use_query_str_in_loader |
bool |
Whether to pass the query string to the loader function (default: False)
|
query_str_kwargs_key |
str |
The keyword argument name used for the query string (default: "query_str")
|
Instance Attributes
| Attribute | Type | Description |
|---|---|---|
_loader |
Callable |
The document loading function |
_index_cls |
Type[BaseIndex] |
Index class for document indexing |
_index_kwargs |
Dict |
Keyword arguments for index construction |
_use_query_str_in_loader |
bool |
Flag controlling query string pass-through to the loader |
_metadata |
ToolMetadata |
Tool metadata |
_query_str_kwargs_key |
str |
Key name for the query string parameter |
Factory Methods
from_defaults
@classmethod
def from_defaults(
cls,
reader: BaseReader,
index_cls: Optional[Type[BaseIndex]] = None,
index_kwargs: Optional[Dict] = None,
use_query_str_in_loader: bool = False,
query_str_kwargs_key: str = "query_str",
name: Optional[str] = None,
description: Optional[str] = None,
fn_schema: Optional[Type[BaseModel]] = None,
) -> "OnDemandLoaderTool"
Creates an OnDemandLoaderTool from a BaseReader instance:
- Defaults
index_clstoVectorStoreIndex. - If no description is provided, generates one from the reader class name.
- If no
fn_schemais provided, generates one automatically from the reader'sload_datamethod signature usingcreate_schema_from_function, adding the query string parameter as an additional field. - Wraps
reader.load_dataas the loader callable.
from_tool
@classmethod
def from_tool(
cls,
tool: FunctionTool,
index_cls: Optional[Type[BaseIndex]] = None,
index_kwargs: Optional[Dict] = None,
use_query_str_in_loader: bool = False,
query_str_kwargs_key: str = "query_str",
name: Optional[str] = None,
description: Optional[str] = None,
return_direct: bool = False,
fn_schema: Optional[Type[BaseModel]] = None,
) -> "OnDemandLoaderTool"
Creates an OnDemandLoaderTool from an existing FunctionTool:
- Similar logic to
from_defaultsbut extracts the underlying function viatool._fn. - Supports the
return_directflag in the metadata.
Internal Method: _parse_args
def _parse_args(self, *args: Any, **kwargs: Any) -> Tuple[str, List[Document]]
Extracts the query string and loads documents from the provided arguments:
- Validates that
_query_str_kwargs_keyis present inkwargs; raisesValueErrorif missing. - If
_use_query_str_in_loaderisTrue, the query string remains inkwargsand is passed to the loader. - If
_use_query_str_in_loaderisFalse, the query string is popped fromkwargsbefore calling the loader. - Calls
self._loader(*args, **kwargs)to load documents. - Returns a tuple of
(query_str, docs).
Execution Methods
call (Synchronous)
def call(self, *args: Any, **kwargs: Any) -> ToolOutput
- Parses arguments via
_parse_argsto get the query string and loaded documents. - Creates an index from the documents using
self._index_cls.from_documents(docs, **self._index_kwargs). - Converts the index to a query engine via
index.as_query_engine(). - Queries the engine with
query_engine.query(query_str). - Returns a
ToolOutputcontaining the response.
acall (Asynchronous)
async def acall(self, *args: Any, **kwargs: Any) -> ToolOutput
Identical to call but uses await query_engine.aquery(query_str) for the query step.
Execution Flow
Agent calls OnDemandLoaderTool(query_str="...", loader_param1="...", ...)
|
v
_parse_args() -- extracts query_str, calls loader with remaining args
|
v
loader(*args, **kwargs) -- loads documents from data source
|
v
index_cls.from_documents(docs) -- builds index from loaded documents
|
v
index.as_query_engine() -- creates query engine from index
|
v
query_engine.query(query_str) -- queries the index
|
v
ToolOutput(content=response) -- returns structured output
Design Patterns
Pipeline Composition
The tool composes a full retrieval pipeline on the fly: loading, indexing, and querying all happen within a single tool invocation. This makes it suitable for scenarios where the data source is dynamic or not pre-indexed.
Schema Auto-Generation
Both factory methods use create_schema_from_function to automatically derive a Pydantic schema from the loader function's signature. This ensures the tool's parameter schema matches the loader's actual parameters, enabling accurate tool-use by LLMs.
Query String Separation
The use_query_str_in_loader flag controls whether the query string is passed to the loader function or only used for querying the resulting index. This provides flexibility for loaders that accept search queries (e.g., web search) versus those that take static parameters (e.g., file paths).
See Also
- Run_llama_Llama_index_Tool_Schema_Utils - Schema generation from function signatures
- Run_llama_Llama_index_Tool_Calling - Tool invocation utilities
- Run_llama_Llama_index_QueryPlanTool - Multi-tool query plan execution