Discover how to fine-tune OpenAI's gpt-oss-20b for chain-of-thought reasoning in multiple languages using Hugging Face's TRL library. From setup to inference, build a model that reasons in English, Spanish, French, Italian, German—or even mixes them for precise, interpretable responses. Perfect for underrepresented languages!
Authored by: Edward Beeching, Quentin Gallouédec, and Lewis Tunstall
Advanced reasoning models like OpenAI's o3 employ chain-of-thought processes to enhance the precision and quality of their outputs. That said, these models generally perform their reasoning in English, regardless of the language used in the query.
In this notebook, we demonstrate how to fine-tune OpenAI's open-weight reasoning model, OpenAI gpt-oss-20b, to deliver effective reasoning across multiple languages. We'll achieve this by incorporating a new *"reasoning language"* option into the model's system prompt and performing supervised fine-tuning with Hugging Face's TRL library on a multilingual reasoning dataset.
We'll cover the following steps:
- **Setup:** Install the required libraries.
- **Prepare the dataset:** Download and format the dataset for fine-tuning.
- **Prepare the model:** Loading the base model and configure it for fine-tuning LoRA, a memory efficient technique.
- **Fine-tuning:** Train the model with our multilingual reasoning data.
- **Inference:** Generate reasoning responses in different languages using the fine-tuned model.
The end result is a multilingual reasoning model that can generate a chain-of-thought in English, Spanish, French, Italian, or German. You can even *mix languages*—for example, ask a question in Spanish, request reasoning in German, and receive the final response in Spanish:
```
User:
¿Cuál es el capital de Australia?
Assistant reasoning:
Okay, der Benutzer fragt nach der Hauptstadt Australiens. Ich erinnere mich, dass Canberra die Hauptstadt ist. Ich
sollte das bestätigen. Lass mich sehen, ob es irgendwelche potenziellen Verwirrungen gibt. Der Benutzer könnte auch
an der größten Stadt interessiert sein. Die größte Stadt ist Sydney, aber die Hauptstadt ist Canberra. Ich sollte
das klarstellen. Vielleicht auch erwähnen, dass Canberra eine geplante Stadt ist und nicht die größte. Der Benutzer
könnte auch nach der Geografie fragen. Vielleicht erwähne ich, dass Canberra im südwestlichen Teil der Australian
Capital Territory liegt. Ich sollte die Antwort präzise und freundlich halten. Vielleicht auch erwähnen, dass
Canberra oft mit Sydney verwechselt wird. Ich sollte sicherstellen, dass die Antwort klar und korrekt ist.
Assistant response:
La capital de Australia es **Canberra**. Aunque es la ciudad más pequeña de las principales capitales del país, fue
elegida en 1908 como la sede del gobierno federal para equilibrar la influencia entre las ciudades de Sydney y
Melbourne. Canberra está ubicada en el Territorio de la Capital Australiana (ACT), en el este de Australia.
```
We hope this tutorial will empower AI developers focusing on under-represented languages to boost the interpretability of `openai/gpt-oss-20b` in their native tongues.
**Note:** This notebook is designed to be run on a single H100 GPU with 80GB of memory. If you have access to a smaller GPU, you can reduce the batch size and sequence length in the hyperparameters below.