Skip to content

fix: add formatting_func for SFTTrainer compatibility#19

Merged
nvandessel merged 3 commits into
mainfrom
fix/sft-formatting-func
Apr 4, 2026
Merged

fix: add formatting_func for SFTTrainer compatibility#19
nvandessel merged 3 commits into
mainfrom
fix/sft-formatting-func

Conversation

@nvandessel
Copy link
Copy Markdown
Owner

Summary

  • Add formatting_func to SFTTrainer calls (notebook + trainer.py)
  • Newer unsloth/trl on Kaggle requires this parameter when passing raw datasets
  • Uses tokenizer.apply_chat_template() to convert messages to chat-formatted strings

Previous run failed with: RuntimeError: Unsloth: You must specify a formatting_func

Test plan

  • 78 tests passing, ruff clean
  • Kaggle notebook runs past SFTTrainer initialization

🤖 Generated with Claude Code

Newer unsloth/trl requires a formatting_func when passing raw
datasets. Use tokenizer.apply_chat_template to convert the
messages list to a chat-formatted string.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 4, 2026

Greptile Summary

This PR adds a formatting_func to the SFTTrainer calls in both trainer.py and the training notebook to satisfy the newer unsloth/trl requirement that raw messages-column datasets must be pre-formatted before being passed to the trainer. The fix is correct in approach — tokenizer.apply_chat_template(msgs, tokenize=False) produces the expected string output.

However, the PR bundles a second unrelated change: replacing warmup_ratio=self._config.warmup_ratio with a hardcoded warmup_steps=50. This silently drops the configurable warmup setting while still requiring it to be present in the YAML config, which is a latent correctness issue.

  • formatting_func addition — correct fix for the Unsloth RuntimeError; uses tokenize=False properly to return strings.
  • warmup_steps=50 hardcodewarmup_ratio is still a required field in TrainingConfig and configs/default.yaml, but its value is now silently ignored at training time. This should either revert to warmup_ratio=self._config.warmup_ratio or clean up the now-unused config field."

Confidence Score: 4/5

Safe to merge if the warmup_ratio silent-drop is addressed; the formatting_func fix itself is correct.

One P1 finding: the configured warmup_ratio is silently discarded in favour of a hardcoded warmup_steps=50, while TrainingConfig still requires warmup_ratio to be specified in the YAML. This is a present defect that will cause incorrect training warmup schedules without any warning.

src/hippofloop/training/trainer.py (line 79) and configs/default.yaml / src/hippofloop/training/config.py for the warmup_ratio vs warmup_steps inconsistency.

Important Files Changed

Filename Overview
src/hippofloop/training/trainer.py Adds correct formatting_func for SFTTrainer, but hardcodes warmup_steps=50 while warmup_ratio is still required in config — silently ignored.
notebooks/train-hippofloop.ipynb Mirrors the trainer.py changes in notebook form; formatting_func addition is correct, warmup_ratio→warmup_steps change has the same silent-drop issue.

Sequence Diagram

sequenceDiagram
    participant YAML as configs/default.yaml
    participant CFG as TrainingConfig
    participant TR as UnslothTrainer.train()
    participant SFT as SFTTrainer

    YAML->>CFG: load warmup_ratio=0.03 (required)
    CFG->>TR: self._config.warmup_ratio (never read)
    Note over TR: warmup_steps=50 hardcoded ❌
    TR->>SFT: formatting_func=formatting_func ✅
    TR->>SFT: warmup_steps=50 (ignores config)
    SFT-->>TR: trainer instance
    TR-->>TR: trainer.train()
Loading

Reviews (3): Last reviewed commit: "fix: formatting_func receives batched ex..." | Re-trigger Greptile

Comment thread notebooks/train-hippofloop.ipynb Outdated
nvandessel and others added 2 commits April 4, 2026 04:59
Unsloth expects formatting_func to return a list of strings,
not a single string. Also replace deprecated warmup_ratio with
warmup_steps.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When batched=True (unsloth default), formatting_func gets a dict
of lists. Iterate over examples["messages"] and return a list
of formatted strings.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nvandessel nvandessel merged commit 9aac77a into main Apr 4, 2026
5 checks passed
@nvandessel nvandessel deleted the fix/sft-formatting-func branch April 4, 2026 05:40
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