Implementation:Neuml Txtai FunctionTool Init
| Knowledge Sources | |
|---|---|
| Domains | NLP, Agent |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete tool for wrapping an arbitrary Python callable as an agent-invocable tool with explicit metadata, provided by the txtai library.
Description
FunctionTool is a subclass of smolagents.Tool that adapts any Python callable into the standardised tool interface. Unlike the automatic smolagents.tool() decorator (which requires type annotations), FunctionTool works with any callable by relying on an explicit configuration dictionary for metadata.
The constructor (__init__) performs the following:
- Extracts name, description, inputs, and target from the configuration dictionary.
- Resolves output_type from either the "output" or "output_type" key, defaulting to "any" if neither is present.
- Stores the callable as self.target.
- Sets skip_forward_signature_validation = True to prevent smolagents from rejecting the tool because forward uses *args, **kwargs rather than named parameters matching the inputs schema.
- Calls super().__init__() to validate the tool metadata and complete registration.
The forward method simply delegates all positional and keyword arguments to self.target, returning whatever the callable returns.
Usage
Use FunctionTool when you have a callable that lacks type annotations or Google-style docstrings, or when you want explicit control over the tool's name, description, and input schema. It is typically created indirectly by ToolFactory.createtool when automatic tool creation fails, but can also be instantiated directly.
Code Reference
Source Location
- Repository: txtai
- File:
src/python/txtai/agent/tool/function.py - Lines: 15-49
Signature
class FunctionTool(Tool):
def __init__(self, config):
self.name = config["name"]
self.description = config["description"]
self.inputs = config["inputs"]
self.output_type = config.get("output", config.get("output_type", "any"))
self.target = config["target"]
self.skip_forward_signature_validation = True
super().__init__()
def forward(self, *args, **kwargs):
return self.target(*args, **kwargs)
Import
from txtai.agent.tool import FunctionTool
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | dict | Yes | Configuration dictionary describing the function tool. |
| config["name"] | str | Yes | Short identifier for the tool, used by the LLM to reference it in tool-call actions. |
| config["description"] | str | Yes | Natural-language description injected into the agent prompt to explain the tool's purpose. |
| config["inputs"] | dict | Yes | Schema mapping parameter names to dicts with type and description keys (e.g., {"x": {"type": "number", "description": "The input value"}}).
|
| config["target"] | callable | Yes | The Python callable to invoke when the agent calls this tool. |
| config["output"] / config["output_type"] | str | No | Declaration of the return type (e.g., "string", "number", "any"). Defaults to "any". |
Outputs
| Name | Type | Description |
|---|---|---|
| instance | FunctionTool | A smolagents.Tool subclass with a forward(*args, **kwargs) method. |
| forward() return | any | Whatever the wrapped callable (self.target) returns. |
Usage Examples
Basic Example
from txtai.agent.tool import FunctionTool
def celsius_to_fahrenheit(celsius):
"""Convert Celsius to Fahrenheit."""
return (celsius * 9 / 5) + 32
tool = FunctionTool(
{
"name": "celsius_to_fahrenheit",
"description": "Converts a temperature from Celsius to Fahrenheit",
"inputs": {
"celsius": {
"type": "number",
"description": "Temperature in degrees Celsius",
}
},
"output": "number",
"target": celsius_to_fahrenheit,
}
)
# Invoke the tool directly
result = tool.forward(celsius=100)
# result: 212.0
Using with an Agent
from txtai.agent import Agent
def lookup_price(product_id):
"""Look up the price of a product by its ID."""
prices = {"SKU001": 29.99, "SKU002": 49.99, "SKU003": 9.99}
return prices.get(product_id, "Product not found")
agent = Agent(
tools=[
{
"name": "price_lookup",
"description": "Look up the price of a product given its SKU identifier",
"inputs": {
"product_id": {
"type": "string",
"description": "The SKU of the product to look up",
}
},
"target": lookup_price,
}
],
model="huggingface-hub/Meta-Llama-3-8B-Instruct",
)
result = agent("How much does SKU002 cost?")