hal_tool: Create a Tool Definition from an R Function

View source: R/tool-builder.R

hal_toolR Documentation

Create a Tool Definition from an R Function

Description

Converts an R function into a hal tool definition suitable for ⁠$register_tool()⁠. Infers parameter types from function formals; override with the types argument for non-string parameters.

Usage

hal_tool(fn, name = NULL, description = NULL, types = NULL)

Arguments

fn

The R function to expose as a tool.

name

Tool name (defaults to the symbol name of fn).

description

Description shown to the LLM (when to call this tool).

types

Optional named list mapping argument names to JSON Schema type strings: "string", "number", "integer", "boolean", "array", "object".

Value

A list with name, description, fun, and parameters – ready for chat$register_tool().

Examples

my_sum <- function(x, y) x + y
tool <- hal_tool(my_sum, "add_numbers",
  description = "Add two numbers",
  types = list(x = "number", y = "number")
)
str(tool)
## Not run: 
# register on the active session so the model can call it
hal_register_tools(list(tool))

## End(Not run)


hal documentation built on Oct. 6, 2026, 5:07 p.m.