ToolNode
Represents a tool node in a directed graph. This node can be used to execute specific tools or functions within a workflow.
YAML example:
kind: ToolNode
name: get_weather
description:
en: |
This tool retrieves the current weather for a specified location.
ru: |
Эта функция получает текущую погоду для указанного местоположения.
func: lib.tools.get_weather
arguments:
- name: lat
type: float
description:
en: latitude of the location
ru: широта местоположения (latitude)
- name: lon
type: float
description:
en: longitude of the location
ru: долгота местоположения (longitude)
# Optionally, you can specify example triggers for the tool.
triggers:
en:
- What's the weather in New York?
ru:
- Какая погода в Нью-Йорке?
# Optionally, you can specify a template for the tool prompt.
# It will allow to improve tool execution accuracy.
# supported only \{name\}, \{description\} and \{triggers\} variables.
tool_prompt_template:
en: |
\{name\} - \{description\}
Examples:
\{triggers\}
ru: |
\{name\} - \{description\}
Примеры:
\{triggers\}Usage:
kind: LLMNode
---
tools:
- get_weather
- another_toolAttributes
attributespec_type= ToolNodeSpecattributestate_type= ToolNodeStateattributeregistry= registryattributefuncCallable[..., Any] | None= NoneFunctions
func__init____init__(self, /, spec, registry, *, initial_data=None, yaml_path=None, strict=False, default_lang='en', fallback_lang='en') -> NoneInitialize tool node with specification and registry.
Args: spec: Tool node specification defining function and arguments registry: Component registry for dependency resolution initial_data: Optional initial data for the component yaml_path: Optional path to the YAML file this node was loaded from strict: Whether to enforce strict validation and imports default_lang: Default language code for description selection fallback_lang: Fallback language code when default is unavailable
paramselfparamspecToolNodeSpecparamregistryRegistryparaminitial_datadict[str, Any] | None= Noneparamyaml_pathstr | None= Noneparamstrictbool= Falseparamdefault_langstr= 'en'paramfallback_langstr= 'en'Returns
Nonefunccompilecompile(self) -> NoneCompile the tool node by loading the target function.
Loads the function specified in spec.func and prepares the node for execution. Must be called before invoke().
paramselfReturns
Nonefuncset_funcset_func(self, /, func) -> NoneManually set the function for this tool node.
Args: func: Callable function that matches the tool specification signature
paramselfparamfuncCallable[..., Any]Returns
Nonefuncinvokeinvoke(self, /, tool_call, execution_context=None) -> ToolMessageExecute the tool function with provided arguments.
Calls the underlying function with extracted arguments and returns the result wrapped in a ToolMessage. Handles both sync and async functions.
Args: tool_call: Tool call containing name, arguments, and call ID execution_context: Optional execution context for dependency injection
Returns: ToolMessage containing function result and call metadata
Raises: ValueError: If no function is set or required parameters are missing
paramselfparamtool_callToolCallparamexecution_contextExecutionContext[ToolNodeState] | None= NoneReturns
ToolMessagefunc_extract_function_args_extract_function_args(self, /, args_dict, execution_context=None) -> dict[str, Any]Extract function arguments based on function signature from provided args dict. Automatically inject Liman dependency if function parameter is typed as Liman.
Args: args_dict: Dictionary containing all available arguments execution_context: Optional ExecutionContext
Returns: Dictionary with only the arguments that match function signature
paramselfparamargs_dictdict[str, Any]paramexecution_contextExecutionContext[ToolNodeState] | None= NoneReturns
dict[str, Any]func_load_func_load_func(self) -> Callable[..., Any]paramselfReturns
Callable[..., Any]funcget_tool_descriptionget_tool_description(self, /, lang) -> strGenerate tool description for specified language.
Creates a formatted description using the tool prompt template, description, and example triggers for the given language.
Args: lang: Language code for description generation
Returns: Formatted tool description string
paramselfparamlangLanguageCodeReturns
strfuncget_json_schemaget_json_schema(self, /, lang=None) -> dict[str, Any]Generate JSON Schema representation for LLM function calling.
Creates OpenAI-compatible function schema including name, description, and parameter definitions for the specified language.
Args: lang: Language code for schema generation (uses default_lang if None)
Returns: JSON Schema dict compatible with LLM function calling
Raises: InvalidSpecError: If description is missing or invalid
paramselfparamlangLanguageCode | None= NoneReturns
dict[str, Any]funcget_new_stateget_new_state(self) -> ToolNodeStateCreate new state instance for this tool node.
Returns: Fresh ToolNodeState for execution
paramselfReturns
ToolNodeStatefunc_get_tool_prompt_template_get_tool_prompt_template(self, /, lang) -> strGet the tool prompt template from the declaration or use the default template.
paramselfparamlangLanguageCodeReturns
strLast updated on