Tool Decorator
Overview
The Tool decorator helps you define Tools for use in function calling. It automatically instruments function calls and creates Tool Logs on Humanloop.
Python
TypeScript
Calling a decorated function will create a Tool Log with the following fields:
inputs: The function arguments.output: The function return value.error: The error message if the function call fails.
Definition
Python
TypeScript
The decorated function will have the same signature as the original function and will have a json_schema attribute containing the inferred JSON Schema.
Parameters
Usage
Python
TypeScript
Decorating a function will set a json_schema attribute that can be used for function calling.
Behavior
Schema Definition
Python
TypeScript
In Python, the decorator automatically infers a JSON Schema from the source code, argument signature, and docstrings:
- Function name becomes the tool name
- Function docstring becomes the tool description
- Parameter type hints are converted to JSON Schema types
- Optional parameters (using
Optional[T]orT | None) are marked as not required - Return type is not included in the schema
Supported type hints:
Log Creation
Each function call creates a Tool Log with the following fields:
Python
TypeScript
Error Handling
Python
TypeScript
- Function errors are caught and logged in the Log’s
errorfield. - The decorated function returns
Nonewhen an error occurs. HumanloopRuntimeErroris not caught and will be re-raised, as it indicates incorrect SDK or decorator usage.
Best Practices
Python
TypeScript
- Use clear and descriptive docstrings in Python to provide good tool descriptions
- Ensure all function parameters have appropriate type hints in Python
- Make return values JSON-serializable
- Use the
json_schemaattribute when passing the tool toprompts.call()
Related Documentation
For a deeper understanding of Tools and their role in the Humanloop platform, refer to our Tools documentation.
For attaching a Tool to a Prompt, see Tool calling in Editor and linking a Tool to a Prompt.