Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Run llama Llama index OnDemandLoaderTool

From Leeroopedia

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:

  1. Defaults index_cls to VectorStoreIndex.
  2. If no description is provided, generates one from the reader class name.
  3. If no fn_schema is provided, generates one automatically from the reader's load_data method signature using create_schema_from_function, adding the query string parameter as an additional field.
  4. Wraps reader.load_data as 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:

  1. Similar logic to from_defaults but extracts the underlying function via tool._fn.
  2. Supports the return_direct flag 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:

  1. Validates that _query_str_kwargs_key is present in kwargs; raises ValueError if missing.
  2. If _use_query_str_in_loader is True, the query string remains in kwargs and is passed to the loader.
  3. If _use_query_str_in_loader is False, the query string is popped from kwargs before calling the loader.
  4. Calls self._loader(*args, **kwargs) to load documents.
  5. Returns a tuple of (query_str, docs).

Execution Methods

call (Synchronous)

def call(self, *args: Any, **kwargs: Any) -> ToolOutput
  1. Parses arguments via _parse_args to get the query string and loaded documents.
  2. Creates an index from the documents using self._index_cls.from_documents(docs, **self._index_kwargs).
  3. Converts the index to a query engine via index.as_query_engine().
  4. Queries the engine with query_engine.query(query_str).
  5. Returns a ToolOutput containing 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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment