Mastering Multilingual Reasoning: Fine-Tuning gpt-oss-20b with Hugging Face Transformers
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.
Comments
More Blog
View allModel Predictive Control Fundamentals: Concepts, Math, and Python Implementation
Discover the essentials of Model Predictive Control (MPC), from its core principles and mathematical foundations to practical Python implementations for dynamic systems control.
Overcoming GPU Limitations: Implementing FP8 Emulation in Software for Legacy Hardware
Discover how to run FP8-optimized AI models on older GPUs without native hardware support using a clever software emulation layer. Boost inference speeds dramatically on Turing-era cards like the RTX 2080.
Hands-On Guide to Hugging Face Transformers: Supercharge Your NLP Projects with AI
Discover how Hugging Face's Transformers library makes advanced NLP accessible. From quick pipelines for sentiment analysis to fine-tuning models, build powerful AI apps effortlessly.
Demystifying Matrix-Matrix Multiplication: Essential Concepts and Practical Insights
Dive deep into matrix-matrix multiplication, from fundamental row-column rules to efficient algorithms like Strassen's, with Python examples and real-world applications in data science.
Demystifying Matrix Transpose: Your Ultimate Guide to A^T and Its Superpowers in Data Science
Dive into the exciting world of matrix transpose! Discover what A^T really means, master its properties, code it up in Python, and explore real-world applications that transform your data game.
Empowering AI Agents to Build Other Agents: A Practical Guide to Meta-Agent Development
Discover how large language models like Claude can generate code for autonomous AI agents, streamlining development and enabling rapid iteration on complex tasks. This approach turns manual coding into an automated, scalable process.