Skip to content

SFT on multi-turn tool trajectories: messages datasets, assistant_only_loss markers, gpt-oss response marker - #1663

Merged
danielhanchen merged 10 commits into
mainfrom
sft-tool-trajectories
Oct 10, 2026
Merged

danielhanchen merged 10 commits into
mainfrom
sft-tool-trajectories

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

SFT on multi-turn tool-calling trajectories (agent traces).

  1. messages datasets: a messages (+ optional tools) dataset with no text column and no formatting_func raised Unsloth: You must specify a formatting_func. sft_prepare_dataset now tokenizes these rows with the chat template and tools, using TRL's training template when TRL swapped one in, and drops the None keys Arrow fills into messages (they crashed the Llama template).
  2. assistant_only_loss: when Unsloth set _unsloth_assistant_mask_fallback (TRL has no training template for this chat template), or the template in use has no {% generation %} markers (TRL < 1.7 never swaps one in, so masks would be all zero and train nothing), the assistant mask comes from train_on_responses_only markers. gpt-oss gets markers that keep its analysis, tool calls and final answer.
  3. train_on_responses_only shared opener: edge trimming cut gpt-oss response_part="<|start|>assistant" down to <|start|>, which every role shares, so system, user and tool turns were trained. Only that case changes: 249 cases (all 51 Unsloth chat templates plus hub tokenizers, auto-detected markers and every marker pair the notebooks use) give byte-identical labels except the 8 gpt-oss <|start|>assistant ones, which now train only tool calls and final answers (107/115 trained tokens before).

Tests: tests/test_tool_trajectory_masks.py (11, CPU, TRL 1.0 / 1.13 / 1.15). Plain-text, formatting_func and messages + text datasets tokenize to identical rows before and after. Pairs with the unsloth PR for the assistant_only_loss fallback.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T07:29:31.516324Z e38dfb2 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 69549ac08f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth_zoo/dataset_utils.py Outdated
Comment on lines +2881 to +2882
truncate_messages = do_truncation and max_seq_length > 0 and \
not locals().get("_unsloth_wrapped_packing", False)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve full conversations when packing

When packing=True, this condition still enables per-row truncation because _unsloth_wrapped_packing is never defined anywhere in the repository and the actual packing argument is not checked. Consequently, _tokenize_messages slices every trajectory to max_seq_length before pack_dataset runs, silently discarding later tool calls and assistant answers instead of allowing the packer to chunk the full token stream; disable this truncation whenever packing is enabled.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsloth defines _unsloth_wrapped_packing when it rewrites this function into the trainer (rl_replacements.py _WRAPPED_PACKING_SETUP), true only for packing_strategy wrapped. For bfd packing rows are truncated before packing, the same as the text path and TRL.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 56a2181220

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2872 to +2873
if isinstance(template, dict):
return "\n".join(v for v in template.values() if isinstance(v, str))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Resolve generation markers from the selected named template

The follow-up mapping fix concatenates every named template before detecting generation markers, which is incorrect when, for example, tool_use contains {% generation %} but default does not. A row without tools selects default, yet the marker found in tool_use disables marker fallback globally, so Transformers returns an all-zero assistant mask and this code drops the row; if all rows select the markerless template, preparation then raises StopIteration. Determine marker support from the template actually selected for each row rather than from the union of all entries.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below the bar: it needs a named template set where only one entry carries {% generation %} markers. Those markers come from TRL's training templates, which are single strings; no shipped tokenizer has such a set.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4f0f8de048

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth_zoo/dataset_utils.py Outdated
Comment on lines +2893 to +2894
truncate_messages = do_truncation and max_seq_length > 0 and \
not locals().get("_unsloth_wrapped_packing", False)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Allow max_length=None for message datasets

When packing is disabled and SFTConfig.max_length=None is used to request no truncation, do_truncation is set to true because None != 0, and this comparison then raises TypeError before any conversational row is tokenized. This is a supported configuration and affects message datasets regardless of whether assistant_only_loss is enabled; treat None as truncation disabled instead of comparing it with an integer.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e38dfb2: a None max_length turns message truncation off. Test added.

Comment thread unsloth_zoo/dataset_utils.py Outdated
masks = masks[0]
result["assistant_masks"] = list(masks)
if truncate_messages:
result = {k: v[:max_seq_length] for k, v in result.items()}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Honor keep_end when truncating conversations

For runs configured with truncation_mode="keep_end", this unconditional prefix slice keeps the beginning of the conversation instead of the requested tail. In multi-turn datasets that mode is commonly used to retain the final assistant answer; this can instead remove every supervised token and cause the subsequent filter to drop the row, silently changing the training data.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e38dfb2: keep_end keeps the last max_length tokens, as TRL and Unsloth's other caps do. Test added (fails before).

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: e38dfb2ad0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant