Tool calls aren't rendered by the chat template

#44
by qgallouedec - opened
Hugging Face Smol Models Research org

It seems like the tool calls aren't rendered by the chat template:

from transformers import AutoTokenizer

def multiply(a: int, b: int) -> int:
    """
    Multiplies two integers.

    Args:
        a: The first integer.
        b: The second integer.

    Returns:
        The product of the two integers.
    """
    return a * b

tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM3-3B")

messages = [
    {"role": "user", "content": "Multiply 3 and 4"},
    {"role": "assistant", "content": "", "tool_calls": [{"type": "function", "function": {"name": "multiply", "arguments": {"a": 3, "b": 4}}}]},  # this one won't be rendered
    { "role": "tool", "name": "multiply", "content": "12"},
]
print(tokenizer.apply_chat_template(messages, tools=[multiply], tokenize=False))
<|im_start|>system
## Metadata

Knowledge Cutoff Date: June 2025
Today Date: 25 November 2025
Reasoning Mode: /think

## Custom Instructions

You are a helpful AI assistant named SmolLM, trained by Hugging Face. Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracking, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: <think> Thought section </think> Solution section. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion.

### Tools

You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:

<tools>
{'type': 'function', 'function': {'name': 'multiply', 'description': 'Multiplies two integers.', 'parameters': {'type': 'object', 'properties': {'a': {'type': 'integer', 'description': 'The first integer.'}, 'b': {'type': 'integer', 'description': 'The second integer.'}}, 'required': ['a', 'b']}, 'return': {'type': 'integer', 'description': 'The product of the two integers.'}}}
</tools>

For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>

<|im_end|>
<|im_start|>user
Multiply 3 and 4<|im_end|>
<|im_start|>assistant
<|im_end|>
<|im_start|>user
12<|im_end|>

what I would expect vs what I get:

  ...
  <|im_start|>assistant
+ <tool_call>
+ {"name": "multiply", "arguments": {"a": 3, "b": 4}}
+ </tool_call>
  <|im_end|>
  ...
Hugging Face Smol Models Research org
edited 11 days ago

I think this is because in the training data, the messages for tool calls are not formatted like the ones in @qgallouedec ’s example. Looking at https://huggingface.co/datasets/HuggingFaceTB/smoltalk2/viewer/SFT/smolagents_toolcalling_traces_think?views%5B%5D=sft_smolagents_toolcalling_traces_think&conversation-viewer=0 it seem like the message is already formatted:

[
{
"content": "New task:\nAs of August 1 2024, what is the most recently described genus of Colosteidae?",
"role": "user"
},
{
"content": "<think>I need to search online for the latest scientific publications or announcements to determine which genus of Colosteidae was most recently described.</think>\n<tool_call>{'name': 'web_search', 'arguments': {'query': 'most recently described genus of Colosteidae'}}</tool_call>",
"role": "assistant"
},

]

I think we would need to adjust the chat template to be like the Qwen3 models in order to format messges with tool calls like the ones

Sign up or log in to comment