Fix SFTTrainer TypeError: Unexpected Keyword Argument 'tokenizer'

Original question: TypeError in SFTTrainer Initialization: Unexpected Keyword Argument 'tokenizer'

Stable Diffusionhow-tointermediate6 min readVerified Jul 19, 2026

The error TypeError: SFTTrainer.__init__() got an unexpected keyword argument 'tokenizer' occurs because in TRL version 0.12.0 and later, the tokenizer parameter was renamed to processing_class. To fix it, replace tokenizer=tokenizer with processing_class=tokenizer in your SFTTrainer initialization. If you need to use the old API, downgrade TRL to version 0.11.0 or earlier.

The Full Answer

Understanding the Error

The SFTTrainer class from the trl library is used for supervised fine-tuning of language models. In version 0.12.0, the library underwent a significant API change: the tokenizer argument was renamed to processing_class. This change was made to unify the interface across different trainers and to support more general processing classes beyond just tokenizers (e.g., image processors, feature extractors).

When you pass tokenizer=tokenizer to SFTTrainer in TRL 0.12.0+, Python raises a TypeError because the __init__ method no longer accepts that keyword. The error message is straightforward: "got an unexpected keyword argument 'tokenizer'".

Solution 1: Use the New Parameter Name (Recommended)

The simplest and most future-proof fix is to change tokenizer to processing_class in your SFTTrainer call. According to the accepted answer on Stack Overflow (by user nemo, score 27), this is the correct approach for TRL 0.12.0 and later.

Here is your corrected code snippet:

from trl import SFTTrainer, SFTConfig

# ... (all your previous setup code remains the same)

trainer = SFTTrainer(
    model=model,
    train_dataset=data,
    peft_config=peft_config,
    args=training_arguments,
    processing_class=tokenizer,  # Replace tokenizer with processing_class
)

trainer.train()

When to use this: You are running TRL 0.12.0 or newer, and you want to stay on the latest version for new features and bug fixes.

How to check your TRL version:

pip show trl

Or in Python:

import trl
print(trl.__version__)

If the version is 0.12.0 or higher, use processing_class. If it is 0.11.0 or lower, use tokenizer.

Solution 2: Downgrade TRL (Temporary Workaround)

If you prefer not to change your code or if you are working in an environment where updating to the new API is not feasible (e.g., a shared Colab notebook with pinned dependencies), you can downgrade TRL to version 0.11.0 or earlier.

Run this in your Colab cell or terminal:

pip install trl==0.11.0

After downgrading, restart your runtime (in Colab: Runtime > Restart runtime) to ensure the old version is loaded. Then your original code with tokenizer=tokenizer will work.

When to use this: You are in a hurry, your codebase is large and uses the old API everywhere, or you are collaborating with others who have not upgraded yet.

Caveat: Downgrading means you miss out on any improvements, security patches, or new features in later versions. It is a short-term fix.

Why the Change Happened

The rename from tokenizer to processing_class is part of TRL's effort to support multimodal models. In the future, SFTTrainer may need to handle not just text tokenizers but also image processors (for vision-language models) or audio feature extractors. A single parameter name processing_class is more generic and extensible. The official TRL release notes for 0.12.0 document this change.

Full Working Example

Here is a complete, corrected version of the script from the Stack Overflow question, using the new API:

import torch
from datasets import load_dataset, Dataset
from peft import LoraConfig, AutoPeftModelForCausalLM, prepare_model_for_kbit_training, get_peft_model
from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig, TrainingArguments
from trl import SFTTrainer, SFTConfig
import os

# Load and preprocess dataset
data = load_dataset("tatsu-lab/alpaca", split="train")
data_df = data.to_pandas()
data_df = data_df[:5000]
data_df["text"] = data_df[["input", "instruction", "output"]].apply(
    lambda x: "###Human: " + x["instruction"] + " " + x["input"] + " ###Assistant: " + x["output"], axis=1
)
data = Dataset.from_pandas(data_df)

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-GPTQ")
tokenizer.pad_token = tokenizer.eos_token

# Load and prepare model
quantization_config_loading = GPTQConfig(bits=4, disable_exllama=True, tokenizer=tokenizer)
model = AutoModelForCausalLM.from_pretrained(
    "TheBloke/Mistral-7B-Instruct-v0.1-GPTQ",
    device_map="auto"
)
model.config.use_cache = False
model.config.pretraining_tp = 1
model.gradient_checkpointing_enable()
model = prepare_model_for_kbit_training(model)

# LoRA configuration
peft_config = LoraConfig(
    r=16, lora_alpha=16, lora_dropout=0.05, bias="none", task_type="CAUSAL_LM", target_modules=["q_proj", "v_proj"]
)
model = get_peft_model(model, peft_config)

# Training configuration
training_arguments = SFTConfig(
    output_dir="mistral-finetuned-alpaca",
    per_device_train_batch_size=8,
    gradient_accumulation_steps=1,
    optim="paged_adamw_32bit",
    learning_rate=2e-4,
    lr_scheduler_type="cosine",
    save_strategy="epoch",
    logging_steps=100,
    num_train_epochs=1,
    max_steps=250,
    fp16=True,
    packing=False,
    max_seq_length=512,
    dataset_text_field="text",
    push_to_hub=True
)

# Initialize trainer with the new parameter name
trainer = SFTTrainer(
    model=model,
    train_dataset=data,
    peft_config=peft_config,
    args=training_arguments,
    processing_class=tokenizer,  # Changed from tokenizer to processing_class
)

trainer.train()

Common Pitfalls

  1. Forgetting to restart the runtime after downgrading TRL. If you downgrade in a Colab notebook but do not restart the runtime, Python may still have the old (newer) version loaded in memory. Always restart after pip install to clear the cached module.

  2. Using both tokenizer and processing_class simultaneously. Do not pass both arguments. The SFTTrainer will raise an error if you try to pass both. Stick to one or the other based on your TRL version.

  3. Assuming the fix is in SFTConfig. The Stack Overflow question mentions trying to pass the tokenizer inside training_arguments. That does not work because SFTConfig does not have a tokenizer or processing_class parameter. The tokenizer must be passed directly to SFTTrainer.

  4. Community-reported issue: tokenizer not being used during training. After fixing the TypeError, some users report that the model does not tokenize the data correctly, leading to a different error about tokenization. This usually happens because the dataset_text_field in SFTConfig does not match the actual column name in your dataset. In the example above, the column is named "text", so dataset_text_field="text" is correct. Double-check your dataset column names.

  5. Version mismatch between trl and transformers. If you upgrade trl to 0.12.0, ensure your transformers library is also up to date (4.38.0 or later). An older transformers may not support some features that trl 0.12.0 relies on. Run pip install transformers --upgrade to be safe.

Related Questions

What is the processing_class parameter in SFTTrainer?

processing_class is the new name for the tokenizer parameter in TRL 0.12.0+. It accepts any object that has a __call__ method for processing inputs, such as a tokenizer, image processor, or feature extractor. This change allows SFTTrainer to work with multimodal models in the future. When you pass a tokenizer, it will be used to tokenize the dataset during training, just as before.

How do I check which version of TRL I have installed?

You can check your TRL version by running pip show trl in your terminal or Colab cell. The output will include a line like Version: 0.12.0. Alternatively, in Python, you can run import trl; print(trl.__version__). If the version is 0.12.0 or higher, use processing_class; otherwise, use tokenizer.

Can I still use the old tokenizer parameter if I don't want to change my code?

Yes, you can downgrade TRL to version 0.11.0 or earlier by running pip install trl==0.11.0. After downgrading, your existing code with tokenizer=tokenizer will work without changes. However, this is a temporary workaround. The old API is deprecated and will be removed in a future release, so it is recommended to update your code to use processing_class.

Why does removing tokenizer cause a different error about tokenization?

When you remove the tokenizer argument entirely, SFTTrainer does not know how to process your dataset. It expects either a processing_class (or tokenizer in older versions) to tokenize the text data. Without it, the trainer cannot convert your text into input IDs, leading to an error during training. Always provide a tokenizer via the appropriate parameter.

Was this helpful?
Newsletter

The #1 Stable diffusion Newsletter

The most important stable diffusion updates, guides, and fixes โ€” one weekly email.

No spam, unsubscribe anytime. Privacy policy

Sources & References

This page was researched from 2 independent sources, combined and verified for completeness.

Keep exploring Stable Diffusion

Skip the manual work

Ready-made AI workflows and automation templates โ€” import and run instead of building from scratch.

Explore workflows