TypeError with TrainingArguments evaluation_strategy in SFTTrainer with Unsloth

Getting TypeError: TrainingArguments.__init__() got an unexpected keyword argument 'evaluation_strategy' when initializing SFTTrainer with TrainingArguments including evaluation_strategy='steps' and load_best_model_at_end=True.

Using:

  • transformers 4.54.1
  • trl 0.20.0
  • unsloth 2025.7.11

Have already resolved pip dependency conflicts after upgrading transformers and installing tyro/msgspec.

The error persists specifically when including evaluation_strategy in TrainingArguments. Code snippet for trainer initialization:

from trl import SFTTrainer
from transformers import DataCollatorForSeq2Seq, TrainingArguments # Import TrainingArguments
from google.colab import userdata # Import userdata

# Define the formatting function to work with the 'messages' column
def formatting_prompts_func(examples):
    texts = []
    for conversation in examples["messages"]: # 'examples["messages"]' is a list of conversations
        # Ensure the conversation is a list and contains dictionaries
        if not isinstance(conversation, list):
            print(f"Skipping invalid conversation entry: {conversation}")
            continue # Skip this entry if it's not a list

        processed_conversation = []
        for message in conversation:
            # Ensure each message is a dictionary with 'role' and 'content'
            if isinstance(message, dict) and 'role' in message and 'content' in message:
                processed_conversation.append(message)
            else:
                print(f"Skipping invalid message format in conversation: {message}")
                # Decide how to handle malformed messages - skipping for now
                continue # Skip this message if it's not a valid dictionary format

        if not processed_conversation:
            print(f"Skipping conversation with no valid messages after filtering.")
            continue # Skip if no valid messages were found

        # Apply the chat template to the list of valid message dictionaries
        try:
            text = tokenizer.apply_chat_template(
                processed_conversation,
                tokenize=False,
                add_generation_prompt=False
            )
            texts.append(text)
        except Exception as e:
            print(f"Error applying chat template to conversation: {processed_conversation}. Error: {e}")
            continue # Skip conversation if chat template application fails


        # print(text) # Uncomment to debug the formatted text
    return texts # Return the list of strings directly

# Define TrainingArguments instead of SFTConfig
training_args = TrainingArguments(
    per_device_train_batch_size = 8,
    gradient_accumulation_steps = 8,
    warmup_steps = 5,
    num_train_epochs = 1, # Set this for 1 full training run.
   # max_steps = 30,
    learning_rate = 2e-4,
    logging_steps = 1,
    optim = "adamw_8bit",
    weight_decay = 0.01,
    lr_scheduler_type = "linear",
    seed = 3407,
    output_dir = "outputs",
    report_to = "wandb", # Use this for WandB etc

    # Add checkpointing and saving to Hugging Face
    save_strategy="steps",
    save_steps=250, # Save a checkpoint every 100 steps
    push_to_hub=True,
    hub_model_id="your_name/your_model", # Replace with your HF username and model name
    hub_token=userdata.get('HF_TOKEN'), # Use the HF token from secrets

    # Add save_total_limit and load_best_model_at_end
    save_total_limit=3, # Keep a maximum of 3 checkpoints
    load_best_model_at_end=True, # Load the best model based on evaluation at the end

    # Add evaluation strategy for load_best_model_at_end
    evaluation_strategy="steps",
    eval_steps=250, # Evaluate every 100 steps
)


trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    # dataset_text_field is not needed when formatting_func returns list[str]
    max_seq_length = max_seq_length,
    data_collator = DataCollatorForSeq2Seq(tokenizer = tokenizer),
    packing = False, # Can make training 5x faster for short sequences.
    args = training_args, # Pass the TrainingArguments object
    formatting_func = formatting_prompts_func, # Add the formatting function
).

Any guidance on resolving this compatibility issue to enable load_best_model_at_end is appreciated

1 Like

I think evaluation_strategy has been discontinued and renamed to eval_strategy. Please try it out.

yeah I tried It too but nothing, thanks either way!

1 Like
trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset
# eval_dataset = eval_dataset is missing?

Hmm, that’s about all I can think of. I think you’ll be able to figure it out by following the error messages.