Implementation:Hiyouga LLaMA Factory Tool Utils
| Knowledge Sources | |
|---|---|
| Domains | Tool Calling, Function Calling |
| Last Updated | 2026-02-06 19:00 GMT |
Overview
Concrete model-specific tool/function calling format utilities for 11+ model architectures provided by LLaMA Factory.
Description
This module provides the formatting and parsing logic for tool-use (function calling) capabilities across different model families. It defines a ToolUtils abstract base class with three core static methods and implements model-specific subclasses:
- DefaultToolUtils -- Generic Action/Action Input format with structured tool descriptions
- GLM4ToolUtils -- ChatGLM-4 format with Chinese-language tool prompts and newline-delimited calls
- GLM4MOEToolUtils -- GLM-4 MOE format with XML-based tool_call tags and arg_key/arg_value pairs
- Llama3ToolUtils -- LLaMA-3.x JSON-based tool calling with date-stamped system prompts
- MiniMaxM1ToolUtils -- MiniMax-M1 format with <tool_calls> XML wrapper
- MiniMaxM2ToolUtils -- MiniMax-M2 format with <minimax:tool_call> and <invoke> XML tags
- MistralToolUtils -- Mistral format with [TOOL_CALLS] prefix and JSON array
- QwenToolUtils -- Qwen format with <tool_call> XML tags
- SeedToolUtils -- Seed/Doubao format with <seed:tool_call> and <function=name> XML tags
- LingToolUtils -- Ling format similar to Qwen with <tool_call> tags
- LFM2ToolUtils -- LFM2 format with <|tool_list_start|>/<|tool_list_end|> delimiters and <|tool_call_start|> tags
Each subclass defines model-specific prompt templates stored as module-level constants (DEFAULT_TOOL_PROMPT, GLM4_TOOL_PROMPT, LLAMA3_TOOL_PROMPT, QWEN_TOOL_PROMPT, etc.).
The FunctionCall NamedTuple represents a parsed function call with name and arguments fields.
Usage
Tool utilities are referenced by the ToolFormatter class in the formatter module, which is in turn used by templates during encoding. The tool_formatter method generates system prompt additions, function_formatter formats assistant outputs during training, and tool_extractor parses model responses during inference.
Code Reference
Source Location
- Repository: Hiyouga_LLaMA_Factory
- File: src/llamafactory/data/tool_utils.py
- Lines: 1-676
Signature
class FunctionCall(NamedTuple):
name: str
arguments: str
@dataclass
class ToolUtils(ABC):
@staticmethod
@abstractmethod
def tool_formatter(tools: list[dict[str, Any]]) -> str: ...
@staticmethod
@abstractmethod
def function_formatter(functions: list["FunctionCall"]) -> str: ...
@staticmethod
@abstractmethod
def tool_extractor(content: str) -> Union[str, list["FunctionCall"]]: ...
class DefaultToolUtils(ToolUtils): ...
class GLM4ToolUtils(ToolUtils): ...
class GLM4MOEToolUtils(ToolUtils): ...
class Llama3ToolUtils(ToolUtils): ...
class MiniMaxM1ToolUtils(ToolUtils): ...
class MiniMaxM2ToolUtils(ToolUtils): ...
class MistralToolUtils(ToolUtils): ...
class QwenToolUtils(ToolUtils): ...
class SeedToolUtils(ToolUtils): ...
class LingToolUtils(ToolUtils): ...
class LFM2ToolUtils(ToolUtils): ...
def get_tool_utils(name: str) -> type["ToolUtils"]: ...
Import
from llamafactory.data.tool_utils import (
ToolUtils,
FunctionCall,
DefaultToolUtils,
QwenToolUtils,
get_tool_utils,
)
I/O Contract
Inputs (tool_formatter)
| Name | Type | Required | Description |
|---|---|---|---|
| tools | list[dict[str, Any]] | Yes | List of tool/function definitions in OpenAI format with name, description, and parameters |
Inputs (function_formatter)
| Name | Type | Required | Description |
|---|---|---|---|
| functions | list[FunctionCall] | Yes | List of FunctionCall tuples with name and JSON arguments string |
Inputs (tool_extractor)
| Name | Type | Required | Description |
|---|---|---|---|
| content | str | Yes | Raw assistant message text potentially containing tool calls |
Outputs
| Name | Type | Description |
|---|---|---|
| tool_formatter result | str | Formatted system prompt section describing available tools |
| function_formatter result | str | Formatted assistant message with tool call syntax |
| tool_extractor result | Union[str, list[FunctionCall]] | Parsed function calls or original text if no calls found |
Usage Examples
from llamafactory.data.tool_utils import DefaultToolUtils, FunctionCall
# Format tool descriptions for the system prompt
tools = [
{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
},
"required": ["location"],
},
}
]
system_addition = DefaultToolUtils.tool_formatter(tools)
# Format function calls for training data
calls = [FunctionCall("get_weather", '{"location": "Tokyo"}')]
assistant_text = DefaultToolUtils.function_formatter(calls)
# "Action: get_weather\nAction Input: {\"location\": \"Tokyo\"}"
# Extract function calls from model output
result = DefaultToolUtils.tool_extractor(
"Action: get_weather\nAction Input: {\"location\": \"Tokyo\"}"
)
# [FunctionCall(name='get_weather', arguments='{"location": "Tokyo"}')]
Related Pages
- Hiyouga_LLaMA_Factory_Chat_Template - Templates that reference tool utilities via ToolFormatter
- Hiyouga_LLaMA_Factory_Data_Converter - OpenAIDatasetConverter processes tool_calls using these utilities
- Hiyouga_LLaMA_Factory_HfChatEngine - Engine that passes tool descriptions through the template system