Jido.Action.Tool (Jido Action v2.3.0)

View Source

Provides functionality to convert Jido Actions into generic tool representations.

This module allows Jido Actions to be converted into standardized tool maps that can be used by various AI integration layers.

Tool execution preserves the legacy {:ok, json} / {:error, json} contract. Successful results are sanitized before JSON encoding, and failures still serialize as %{"error" => binary} payloads with sanitizer-backed fallback when raw inspection is unsafe.

Tool Formats

  • to_tool/1 - Returns a generic tool map with name, description, function, and schema
  • to_tool/2 - Same as to_tool/1 with JSON schema options (e.g., strict mode)

Utility Functions

Summary

Functions

Builds a parameters schema for the tool based on the action's schema.

Helper function to convert params using schema information.

Executes an action and formats the result for tool output.

Converts a Jido Exec into a tool representation.

Types

tool()

@type tool() :: %{
  name: String.t(),
  description: String.t(),
  function: (map(), map() -> {:ok, String.t()} | {:error, String.t()}),
  parameters_schema: map()
}

Functions

build_parameters_schema(schema)

@spec build_parameters_schema(Jido.Action.Schema.t()) :: map()

Builds a parameters schema for the tool based on the action's schema.

Arguments

  • schema - The NimbleOptions or Zoi schema from the action.

Returns

A map representing the parameters schema in a format compatible with LangChain.

build_parameters_schema(schema, opts)

@spec build_parameters_schema(
  Jido.Action.Schema.t(),
  keyword()
) :: map()

convert_params_using_schema(params, schema)

Helper function to convert params using schema information.

Converts string keys to atom keys and handles type conversion based on schema. Supports both atom and string input keys, and preserves unknown keys (open validation).

execute_action(action, params, context)

@spec execute_action(module(), map(), map()) ::
  {:ok, String.t()} | {:error, String.t()}

Executes an action and formats the result for tool output.

This function is typically used as the function value in the tool representation. Error payloads intentionally remain the legacy %{"error" => binary} JSON shape for compatibility.

to_tool(action)

@spec to_tool(module()) :: tool()

Converts a Jido Exec into a tool representation.

Arguments

  • action - The module implementing the Jido.Action behavior.

Returns

A map representing the action as a tool, compatible with systems like LangChain.

Examples

iex> tool = Jido.Action.Tool.to_tool(MyExec)
%{
  name: "my_action",
  description: "Performs a specific task",
  function: #Function<...>,
  parameters_schema: %{...}
}

to_tool(action, opts)

@spec to_tool(
  module(),
  keyword()
) :: tool()