portia.tool_decorator
Tool decorator for creating tools from functions.
DecoratedTool Objects
class DecoratedTool(Tool[T])
Decorated tool class.
make_run_method
def make_run_method(sig: inspect.Signature, fn: Callable) -> Callable
Make the run method for the tool.
make_arun_method
def make_arun_method(sig: inspect.Signature, fn: Callable) -> Callable
Make the arun method for the tool.
make_not_implemented_method
def make_not_implemented_method() -> Callable
Make a run method that raises a NotImplementedError.
tool
def tool(fn: Callable[..., T]) -> type[DecoratedTool]
Convert a function into a Tool class.
This decorator automatically creates a Tool subclass from a function by:
- Using the function's docstring as the tool description
- Creating an ID and name based on the function name
- Generating input schema from function parameters and type hints
- Determining output schema from return type annotation
Example:
@tool def add_numbers(a: int, b: int) -> int: """Add two numbers together.""" return a + b
Arguments:
fn
- The function to convert to a Tool class
Returns:
A Tool subclass that wraps the original function
Raises:
ValueError
- If the function has invalid signature or return type