Skip to main content
Proper data preparation is critical for successful fine-tuning. This guide covers the data format, preprocessing, and best practices for creating high-quality training datasets.

Data Format

Qwen uses a standardized JSON conversation format that follows the ChatML structure. Each training sample consists of a conversation with multiple turns between user and assistant.

Basic Structure

Field Descriptions

string
required
A unique identifier for each conversation. Used for logging and debugging.
array
required
An array of conversation turns. Must contain at least one user-assistant pair.
string
required
The role of the speaker. Must be either "user" or "assistant".
string
required
The actual message content. Can contain multi-line text, code, or any UTF-8 content.

Multi-turn Conversations

You can include multiple user-assistant exchanges in a single conversation:
Multi-turn conversations help the model learn context tracking and coherent dialogue flow.

ChatML Format Processing

The training script automatically converts your JSON data into ChatML format during preprocessing. Understanding this helps you prepare better training data.

Automatic Template Application

Your JSON conversations are transformed into this structure:

System Message

The default system message is "You are a helpful assistant." This is applied automatically by the preprocessing function in finetune.py:125-176.
The system message is currently hardcoded in the training script. To customize it, modify the system_message parameter in the preprocess() function.

Token Masking Strategy

The training script implements intelligent token masking to focus learning on assistant responses:

What Gets Masked (Not Trained)

  • System message content
  • User message content
  • Special tokens (<|im_start|>, role names)
  • Padding tokens

What Gets Trained

  • Only the assistant’s responses (excluding special tokens)
This is implemented using IGNORE_TOKEN_ID in the preprocessing function:
This masking strategy improves training efficiency by focusing gradient updates on the assistant’s outputs.

Creating Training Data

Step-by-Step Example

1

Define Your Task

Identify what you want the model to learn. Examples:
  • Customer service responses
  • Code generation in a specific framework
  • Domain-specific question answering
  • Style transfer or tone adaptation
2

Collect Raw Data

Gather examples of high-quality interactions:
3

Format as JSON

Convert to the required conversation structure:
4

Validate Format

Verify your data is correctly formatted:

Data Quality Guidelines

Best Practices

  • Consistent tone: Maintain consistent assistant personality
  • Factual accuracy: Verify all information is correct
  • Natural flow: Ensure conversations feel natural, not scripted
  • Diverse examples: Cover various phrasings and edge cases
  • Typical range: 50-500 tokens per response
  • Max length: Configure with --model_max_length (default: 8192)
  • Avoid truncation: Ensure important conversations fit within max length
  • Balance: Mix short and long responses
  • Minimum: 100-500 examples for domain adaptation
  • Recommended: 1,000-10,000 examples for robust fine-tuning
  • Large-scale: 10,000+ examples for significant behavior changes
  • Quality > Quantity: 100 high-quality examples beat 1,000 poor ones
  • Topic coverage: Include all relevant domains
  • Query variations: Multiple ways to ask the same thing
  • Response styles: Different levels of detail when appropriate
  • Edge cases: Include challenging or unusual scenarios

Special Use Cases

Code Generation

Multilingual Data

Qwen models are pretrained on multilingual data with focus on Chinese and English. They can handle code-switching naturally.

Data Preprocessing Options

Lazy Preprocessing

The training script supports two preprocessing modes:
Lazy preprocessing (LazySupervisedDataset):
  • Lower initial memory usage
  • Faster training startup
  • Recommended for large datasets
  • Caches processed samples
Eager preprocessing (SupervisedDataset):
  • All data processed upfront
  • Slightly faster iteration
  • Better for smaller datasets

Validation Data

You can optionally provide evaluation data to monitor training progress:
Your eval.json should follow the same format as training data.
Evaluation during training increases overall training time. For production fine-tuning, consider evaluating only at the end or at sparse intervals.

Complete Data Preparation Script

Here’s a production-ready script for converting various formats to Qwen training format:
Usage:

Next Steps

Full-Parameter Tuning

Start training with full-parameter fine-tuning

LoRA Fine-tuning

Efficient training with LoRA adapters