SFT for multi-turn tool call data is not supported properly
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8.5k
- Forks
- 1.3k
- Avg merge
- 5h 36m
- Merged PRs (30d)
- 22
Description
SFT Tool Support Proposal
Problem
-
When
--apply-chat-templateis used, the message history (list[dict]) gets converted to a string indata.py. This breaksgen_multi_turn_loss_mask_qwenand other functions inmask_utils.py, which expect alist[dict]. -
--tool-keyrequires--apply-chat-template, which triggers the same string conversion issue. -
In
mask_utils.py,apply_chat_templateis called on each message individually without thetoolsparameter, so tool-related messages (assistant withtool_calls,toolrole responses) are not formatted correctly.
Downstream Effects
- Without these flags: Tool definitions are never added to the conversation, so the model never sees them during training.
- With these flags: A fatal error is thrown because
mask_utilsreceives a string instead oflist[dict]. - Even if it didn't crash:
apply_chat_templatewithouttools=produces incorrect tokenization for tool-calling messages.
Proposal
A non-invasive fix with two changes:
1. data.py - Store tools in metadata
When --tool-key is provided:
- Extract tools from the data
- Store them in
sample.metadata["tools"] - Keep messages as
list[dict](do NOT apply chat template)
2. mask_utils.py - Pass tools to apply_chat_template
Update the mask generation functions to:
- Accept an optional
toolsparameter - Pass
toolstoapply_chat_templateso tool-calling messages are formatted correctly
Also this fixes Issue #289
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read data.py and mask_utils.py, starting with the --apply-chat-template and --tool-key handling and the gen_multi_turn_loss_mask_qwen path. Trace how message history and metadata reach mask generation. Done means tool definitions remain available through metadata, messages stay as list[dict], and apply_chat_template receives tools for tool-calling messages without the reported failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100