Fine-tuning a gpt-oss model lets you specialize its behavior for a specific task — whether that’s domain-specific reasoning, multilingual output, or a custom response style — without starting from scratch. Because gpt-oss ships as open weights, you have full control over the training process, the data, and the resulting model. This guide walks through fine-tuningDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/openai/openai-cookbook/llms.txt
Use this file to discover all available pages before exploring further.
gpt-oss-20b using Hugging Face Transformers and the TRL library’s SFTTrainer, covering data preparation, model loading, the training loop, and how to handle gpt-oss-specific behaviors like the Harmony format and chain-of-thought in your training data.
This workflow is designed to run on a single H100 GPU with 80 GB of memory. If you are using a smaller GPU, reduce
per_device_train_batch_size and max_seq_length in the training arguments. You can also use LoRA (see below) to dramatically reduce VRAM requirements.Install dependencies
Set up a fresh Python environment and install the required libraries:trl provides the SFTTrainer for supervised fine-tuning. peft adds LoRA support for memory-efficient training. kernels and triton are needed for MXFP4 inference with Transformers.
Prepare your training data
OpenAI message format
The most convenient format for fine-tuning data is the same message structure used by the OpenAI Chat Completions API. Each training example is a list of{"role": ..., "content": ...} dictionaries:
Load a dataset
Converting to Hugging Face format
If your data is already in OpenAI format,SFTTrainer can apply the Harmony chat template automatically using the tokenizer.apply_chat_template method. Verify that your dataset has a messages column containing the list of role/content dictionaries:
Load the model and tokenizer
Adding LoRA for memory-efficient fine-tuning
LoRA (Low-Rank Adaptation) dramatically reduces the number of trainable parameters, making it possible to fine-tune on smaller GPUs. Configure a LoRA adapter usingpeft:
Fine-tune with SFTTrainer
SFTTrainer from TRL handles the training loop, chat template application, and sequence packing automatically. Pass your model, tokenizer, dataset, and training arguments:
Full training configuration
For more control over training, provide a completeSFTConfig:
SFTTrainer applies tokenizer.apply_chat_template to your messages column automatically, so the Harmony format is handled without manual tokenization.
Save the fine-tuned model
OpenAI Harmony format in training data
The Harmony format defines how the gpt-oss models expect their training examples to be encoded. When you useSFTTrainer with the Transformers chat template, Harmony encoding is applied automatically. However, if you are constructing training examples at the token level, be aware of the key rules:
- Training targets (the assistant’s response) should end with
<|return|>— not<|end|>. The<|return|>token signals end of generation during sampling. When you store a completed response in conversation history for a subsequent turn, replace<|return|>with<|end|>. - The
analysischannel (chain-of-thought) should be included in training targets if you want the model to learn reasoning patterns. See the section below on handling chain-of-thought. - The reasoning effort level (
low,medium,high) is set in the system message and is part of the training signal.
Handling chain-of-thought in training data
The gpt-oss models output raw chain-of-thought (CoT) to theanalysis channel before producing a final answer on the final channel. When preparing training data, you need to decide whether to include CoT in your targets.
Include CoT when: you want to teach the model how to reason through problems, you are fine-tuning for a task where reasoning quality matters, or you are doing interpretability research.
Exclude CoT when: you only care about the final answer format, your dataset does not contain reasoning traces, or you want to reduce training sequence length.
If your training data includes CoT, structure the assistant messages to reflect the Harmony channel separation:
analysis message and include only the final response.
Verifying your fine-tuned model
After fine-tuning, verify that the model produces well-formed outputs and that the chat template is applied correctly.Quick inference check
Run a simple generation to check that outputs look correct:Check Harmony channel routing
Verify that the model correctly separates reasoning from final output by inspecting the raw generated tokens. If you see content on thefinal channel, the Harmony template is working correctly:
API-level verification
If you are serving the fine-tuned model via vLLM or Transformers serve, use the verification scripts from the gpt-oss GitHub repository to run tool-calling smoke tests against the Chat Completions and Responses API endpoints:Next steps
Run locally with Ollama or LM Studio
Test your fine-tuned model locally before deploying it to a server.
gpt-oss overview
Review the Harmony format, model variants, and hosting options for gpt-oss models.