Implementation:Huggingface Open r1 Make Conversation
Metadata
| Field | Value |
|---|---|
| Sources | Repo: huggingface/open-r1 |
| Domains | NLP, Data_Preprocessing |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for converting raw text prompts into structured chat message format for GRPO training provided by Open-R1. The make_conversation function serves as the dataset preprocessing step that transforms flat prompt strings into role-based chat message lists.
Description
This is a Pattern Doc. The make_conversation function is defined inline within both grpo.py and compute_pass_rate.py as a closure that captures script_args.dataset_prompt_column and training_args.system_prompt. It converts a raw prompt string into a list of chat messages with "system" (optional) and "user" roles. After mapping, the original "messages" column (if present) is removed to avoid conflicts with the trainer's expectations.
In pass_rate_filtering, an additional apply_chat_template step converts the message list to a formatted string for vLLM. This two-phase approach (message construction followed by template application) separates the structural formatting from the tokenizer-specific serialization.
The function is not imported from a shared module — it is defined inline in each script as a closure, allowing it to capture script-specific configuration arguments directly from the enclosing scope.
Usage
Used as a dataset.map function in grpo.py and compute_pass_rate.py. Not imported directly — defined inline within each script. Call it via dataset.map(make_conversation) after configuring the prompt column name and optional system prompt through the script's argument parser.
Code Reference
Source
| Field | Value |
|---|---|
| Repository | open-r1 |
| File | src/open_r1/grpo.py (GRPO); scripts/pass_rate_filtering/compute_pass_rate.py (pass rate) |
| Lines | L91-107 (GRPO); L80-100 (pass rate) |
Signature
def make_conversation(example, prompt_column: str = script_args.dataset_prompt_column):
prompt = []
if training_args.system_prompt is not None:
prompt.append({"role": "system", "content": training_args.system_prompt})
if prompt_column not in example:
raise ValueError(f"Dataset Question Field Error: {prompt_column} is not supported.")
prompt.append({"role": "user", "content": example[prompt_column]})
return {"prompt": prompt}
Usage
dataset = dataset.map(make_conversation)
if "messages" in dataset.column_names:
dataset = dataset.remove_columns("messages")
I/O Contract
Inputs
| Parameter | Type | Required | Description |
|---|---|---|---|
| example | dict |
Yes | A single dataset row containing the prompt text under the column specified by prompt_column.
|
| prompt_column | str |
Yes | The column name for the raw prompt text. Defaults to script_args.dataset_prompt_column via closure.
|
| system_prompt | str |
No | Optional system message prepended to the conversation. Captured from training_args.system_prompt via closure.
|
Outputs
| Output | Type | Description |
|---|---|---|
| return value | dict |
A dictionary with a "prompt" key containing a list[dict] of chat messages in the format [{"role": "user", "content": "..."}], optionally preceded by a {"role": "system", "content": "..."} entry.
|
Usage Examples
Example 1: Basic Conversation Formatting for GRPO
from datasets import load_dataset
dataset = load_dataset("openai/gsm8k", split="train")
# Define make_conversation as a closure (as done in grpo.py)
system_prompt = "You are a helpful math tutor."
prompt_column = "question"
def make_conversation(example):
prompt = []
if system_prompt is not None:
prompt.append({"role": "system", "content": system_prompt})
prompt.append({"role": "user", "content": example[prompt_column]})
return {"prompt": prompt}
dataset = dataset.map(make_conversation)
if "messages" in dataset.column_names:
dataset = dataset.remove_columns("messages")
# Each example now has a "prompt" field:
# [{"role": "system", "content": "You are a helpful math tutor."},
# {"role": "user", "content": "What is 2 + 2?"}]
Example 2: Conversation Formatting with Chat Template for vLLM
from datasets import load_dataset
from transformers import AutoTokenizer
dataset = load_dataset("openai/gsm8k", split="train")
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Qwen-7B")
prompt_column = "question"
def make_conversation(example):
prompt = []
prompt.append({"role": "user", "content": example[prompt_column]})
return {"prompt": prompt}
dataset = dataset.map(make_conversation)
if "messages" in dataset.column_names:
dataset = dataset.remove_columns("messages")
# Apply chat template to produce formatted strings for vLLM
prompts = [
tokenizer.apply_chat_template(example["prompt"], tokenize=False, add_generation_prompt=True)
for example in dataset
]