Implementation:Run llama Llama index FunctionTool From Defaults
Overview
FunctionTool.from_defaults is the primary factory method for creating tools from Python functions in LlamaIndex. It inspects the function signature, extracts docstring parameter descriptions, generates a Pydantic schema, and constructs a FunctionTool instance with full metadata. LlamaIndex also provides QueryEngineTool.from_defaults and RetrieverTool.from_defaults for wrapping query engines and retrievers respectively.
Principle:Run_llama_Llama_index_Tool_Definition
Source File
llama-index-core/llama_index/core/tools/function_tool.py, Lines 67-253
Class Definition
class FunctionTool(AsyncBaseTool):
"""
Function Tool.
A tool that takes in a function, optionally handles workflow context,
and allows the use of callbacks. The callback can return a new ToolOutput
to override the default one or a string that will be used as the final content.
"""
FunctionTool.from_defaults
Signature
@classmethod
def from_defaults(
cls,
fn: Optional[Callable[..., Any]] = None,
name: Optional[str] = None,
description: Optional[str] = None,
return_direct: bool = False,
fn_schema: Optional[Type[BaseModel]] = None,
async_fn: Optional[AsyncCallable] = None,
tool_metadata: Optional[ToolMetadata] = None,
callback: Optional[Callable[[Any], Any]] = None,
async_callback: Optional[AsyncCallable] = None,
partial_params: Optional[Dict[str, Any]] = None,
) -> "FunctionTool":
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| fn | Optional[Callable[..., Any]] |
None |
The synchronous function to wrap. At least one of fn or async_fn must be provided.
|
| name | Optional[str] |
None |
Tool name exposed to the LLM. Defaults to fn.__name__.
|
| description | Optional[str] |
None |
Natural language description for the LLM. Defaults to auto-generated from function signature + docstring. |
| return_direct | bool |
False |
If True, the tool's output is returned directly to the user without further LLM processing.
|
| fn_schema | Optional[Type[BaseModel]] |
None |
Explicit Pydantic model for the function's parameters. If None, auto-generated from the function signature.
|
| async_fn | Optional[AsyncCallable] |
None |
The async version of the function. If only one of fn/async_fn is given, the other is auto-generated.
|
| tool_metadata | Optional[ToolMetadata] |
None |
Pre-built ToolMetadata object. If provided, name, description, and fn_schema are ignored.
|
| callback | Optional[Callable[[Any], Any]] |
None |
Sync callback invoked with the tool's raw output. Can return a ToolOutput or string to override the default output.
|
| async_callback | Optional[AsyncCallable] |
None |
Async version of the callback. |
| partial_params | Optional[Dict[str, Any]] |
None |
Dictionary of parameter names to values that are pre-bound. These parameters are excluded from the generated schema. |
Return Value
Returns a FunctionTool instance with:
- Sync and async function variants (auto-generated if only one is provided)
ToolMetadatawith name, description, and JSON-compatible parameter schema- Optional callback hooks for post-processing
- Context awareness detection (if function accepts a
Contextparameter)
Internal Logic
When tool_metadata is not provided, the method performs these steps:
- Parses the function to extract its name and docstring
- Inspects the function signature to identify all parameters
- Extracts docstring parameter descriptions (supports Sphinx, Google, and Javadoc styles)
- Filters out
Context-typed parameters andself - Removes parameters listed in
partial_paramsfrom the schema - Strips
FieldInfodefaults (Pydantic internal) from remaining parameters - Builds the description from the signature and docstring if not explicitly provided
- Generates a Pydantic schema via
create_schema_from_function - Populates schema field descriptions from parsed docstring
Usage Example
from llama_index.core.tools import FunctionTool
# Basic function tool
def multiply(a: int, b: int) -> int:
"""Multiply two numbers together.
Args:
a (int): The first number.
b (int): The second number.
"""
return a * b
multiply_tool = FunctionTool.from_defaults(
fn=multiply,
name="multiply",
description="Multiply two integers and return the product.",
)
# Async function tool with partial parameters
async def search(query: str, api_key: str, max_results: int = 5) -> str:
"""Search for documents matching the query."""
...
search_tool = FunctionTool.from_defaults(
async_fn=search,
name="search",
description="Search the knowledge base.",
partial_params={"api_key": "sk-..."}, # api_key is hidden from the LLM
)
# Tool with callback
def get_weather(city: str) -> dict:
"""Get the current weather for a city."""
return {"city": city, "temp": 72, "condition": "sunny"}
weather_tool = FunctionTool.from_defaults(
fn=get_weather,
callback=lambda result: f"Weather in {result['city']}: {result['temp']}F, {result['condition']}",
)
QueryEngineTool.from_defaults
Source: llama-index-core/llama_index/core/tools/query_engine.py, Lines 40-58
@classmethod
def from_defaults(
cls,
query_engine: BaseQueryEngine,
name: Optional[str] = None,
description: Optional[str] = None,
return_direct: bool = False,
resolve_input_errors: bool = True,
) -> "QueryEngineTool":
| Parameter | Type | Default | Description |
|---|---|---|---|
| query_engine | BaseQueryEngine |
required | The query engine to wrap (e.g., from a VectorStoreIndex). |
| name | Optional[str] |
"query_engine_tool" |
Tool name for the LLM. |
| description | Optional[str] |
"Useful for running a natural language query..." |
Description of the tool's purpose. |
| return_direct | bool |
False |
Whether to return the tool output directly. |
| resolve_input_errors | bool |
True |
If True, attempts to resolve malformed input by converting kwargs to a string query.
|
QueryEngineTool Usage
from llama_index.core.tools import QueryEngineTool
query_engine_tool = QueryEngineTool.from_defaults(
query_engine=index.as_query_engine(),
name="financial_reports",
description="Search and answer questions about quarterly financial reports from 2020-2024.",
)
RetrieverTool.from_defaults
Source: llama-index-core/llama_index/core/tools/retriever_tool.py, Lines 51-66
@classmethod
def from_defaults(
cls,
retriever: BaseRetriever,
node_postprocessors: Optional[List[BaseNodePostprocessor]] = None,
name: Optional[str] = None,
description: Optional[str] = None,
) -> "RetrieverTool":
| Parameter | Type | Default | Description |
|---|---|---|---|
| retriever | BaseRetriever |
required | The retriever to wrap. |
| node_postprocessors | Optional[List[BaseNodePostprocessor]] |
None |
Post-processors applied to retrieved nodes before returning. |
| name | Optional[str] |
"retriever_tool" |
Tool name for the LLM. |
| description | Optional[str] |
"Useful for running a natural language query..." |
Description of the tool's purpose. |
RetrieverTool Usage
from llama_index.core.tools import RetrieverTool
retriever_tool = RetrieverTool.from_defaults(
retriever=index.as_retriever(similarity_top_k=5),
name="document_retriever",
description="Retrieve relevant document passages about company policies.",
)
Import Statements
from llama_index.core.tools import FunctionTool, QueryEngineTool, RetrieverTool
from llama_index.core.tools.types import ToolMetadata, ToolOutput