M1: Why Fine-Tune M2: Dataset Prep M3: LoRA M4: RLHF M5: Evaluation M6: Deployment Capstone
Intermediate Read time: 15 min Module 2 of 6

Dataset Preparation and Formatting

A team at a major bank spent three months fine-tuning a model on their compliance documents. The model got worse. The problem wasn't the training process — it was that 40% of their examples contradicted each other. Garbage in, garbage out. This module teaches you to prepare data the right way.

Module 2 of 6 — Fine-Tuning LLMs

By the end of this module, you will be able to format a dataset for instruction fine-tuning, apply the five most important data quality filters, and avoid the three most common dataset mistakes that cause fine-tuning to fail.

The Three Formats You Need to Know

Fine-tuning datasets take different shapes depending on your task. The most common format today is the instruction-response pair, sometimes called a "prompt-completion" pair.

Format 1: Instruction-Response (most common)

Each example has two fields: an instruction (what you ask the model to do) and a response (the ideal output). This format teaches the model to follow instructions in your domain.

{
  "instruction": "Classify this customer complaint as billing, technical, or general.",
  "input": "My invoice shows a charge I don't recognize from last Tuesday.",
  "output": "billing"
}

Format 2: Conversational (for chat models)

Uses a list of turns with roles: system, user, and assistant. This format trains the model to maintain context across a conversation.

{
  "messages": [
    {"role": "system", "content": "You are a helpful assistant for TechCorp."},
    {"role": "user", "content": "How do I reset my password?"},
    {"role": "assistant", "content": "Go to Settings > Security > Reset Password. You'll receive an email within 2 minutes."}
  ]
}

Format 3: Completion (for older-style models)

A single text string where the model continues from a prompt. Less common for modern instruction-following fine-tuning, but still used for text generation tasks where you want to extend content in a particular style.

Dataset preparation pipeline Raw data unstructured Quality filters dedup / length consistency coverage Formatting instruction / response pairs JSONL output Training dataset directional illustration

The Five Quality Filters

Before you format your data, you need to clean it. These five filters catch 90% of the problems that cause fine-tuning to fail.

1. Deduplication

Remove exact duplicates and near-duplicates. A model trained on 1,000 examples where 200 are copies of the same 10 examples will behave as if it has only 800 unique examples — but with those 10 over-represented. Use MinHash or exact-match hashing for deduplication.

2. Length filtering

Responses shorter than 10 tokens teach the model nothing useful. Responses longer than your model's context window will be truncated, which teaches the model to start answers it cannot finish. Set minimum and maximum token lengths appropriate for your task.

3. Consistency checking

If two examples have nearly identical inputs but contradictory outputs, the model receives contradictory training signal. It will average between them, satisfying neither. This is the hardest filter to automate. Embedding-based clustering helps: group similar inputs and check whether their outputs agree.

4. Coverage analysis

Make sure your dataset covers all the sub-tasks you need the model to handle. A customer support dataset that has 1,000 billing examples and 5 technical examples will produce a model that is great at billing and helpless at technical. Use stratified sampling to balance coverage.

5. Toxicity and PII removal

Any toxic content or personally identifiable information in training data ends up in model outputs. Run a classifier for toxicity (e.g., Perspective API or a local model) and use regular expressions for common PII patterns (emails, phone numbers, social security numbers).

Key concept

Dataset quality matters more than dataset size. A dataset of 500 carefully curated, consistent, well-formatted examples will outperform a dataset of 10,000 noisy examples every time.

Dataset Quality Simulator Interactive

Adjust your dataset characteristics and see the predicted model quality score.

500
10
5
70

The JSONL Format: What the Training Script Actually Reads

Most fine-tuning frameworks (Hugging Face Transformers, Axolotl, OpenAI fine-tuning API) expect data in JSONL format: one JSON object per line, each object being one training example.

# Step 1: Load and validate your raw examples
import json
import hashlib
from pathlib import Path

def load_and_clean_dataset(raw_path: str, min_resp_tokens: int = 10, max_resp_tokens: int = 512):
    examples = []
    seen_hashes = set()

    with open(raw_path) as f:
        for line in f:
            ex = json.loads(line.strip())

            # Filter: require instruction and output fields
            if not ex.get('instruction') or not ex.get('output'):
                continue

            # Filter: length
            output_len = len(ex['output'].split())
            if output_len < min_resp_tokens or output_len > max_resp_tokens:
                continue

            # Dedup: hash the instruction+output together
            key = hashlib.md5((ex['instruction'] + ex['output']).encode()).hexdigest()
            if key in seen_hashes:
                continue
            seen_hashes.add(key)

            examples.append(ex)

    return examples

# Step 2: Format for instruction fine-tuning (Alpaca-style)
def format_example(ex: dict) -> str:
    prompt = f"### Instruction:\n{ex['instruction']}\n"
    if ex.get('input'):
        prompt += f"### Input:\n{ex['input']}\n"
    prompt += f"### Response:\n{ex['output']}"
    return prompt

# Step 3: Write to JSONL
def write_jsonl(examples, output_path):
    with open(output_path, 'w') as f:
        for ex in examples:
            f.write(json.dumps({"text": format_example(ex)}) + "\n")

# Usage
raw = load_and_clean_dataset("data/raw.jsonl")
write_jsonl(raw, "data/train.jsonl")
print(f"Wrote {len(raw)} clean examples.")

How Much Data Do You Need?

This is the most common question and the honest answer is: it depends on task complexity. Research by Wei et al. (arXiv:2109.01652) on instruction tuning showed that relatively small, high-quality datasets can produce strong instruction-following behavior in models that were already large and well-pre-trained.

Practical ranges used by practitioners:

Research grounding The LIMA paper (Zhou et al., arXiv:2305.11206) showed that training on just 1,000 carefully selected examples produced a model that matched GPT-4-level alignment on many tasks. The paper argued that alignment is more about data quality and diversity than raw volume.
Try this — no setup required Take 10 real examples from your domain and apply the five filters manually. For each example, check: Is it a duplicate? Is the response an appropriate length? Does it contradict any of the other 9 examples? Is it representative of the full task? Does it contain any PII or problematic content? You'll likely find 2–3 examples that fail at least one filter. That ratio, extrapolated to a dataset of 1,000, tells you how much cleanup work is ahead.

Knowledge check

Q1: Your dataset has 2,000 examples for billing inquiries and 20 for technical issues. What is the most important problem this causes?

A) The model will overfit on the billing examples
B) The model will perform poorly on technical inquiries because of insufficient coverage
C) The training will take longer than necessary
D) The model will mix up billing and technical responses

Q2: Two examples in your dataset have nearly identical inputs but opposite outputs. What is the correct action?

A) Keep both — the model will average them and find the right answer
B) Keep the longer response — it is probably more complete
C) Resolve the contradiction — pick the correct one or rewrite both to be consistent
D) Remove both examples to be safe
Why is stratified sampling important for fine-tuning? Think first, then reveal. +
If your training data is skewed toward one type of example, the model's gradient updates are dominated by that type. Parameters that matter for underrepresented tasks receive few updates and stay close to the pre-trained values. The model learns the common task well and the rare tasks poorly — even if the rare tasks are just as important in practice. Stratified sampling guarantees each task type gets proportional representation in every training batch.

Before you go

Reflect: If you were building a fine-tuning dataset for your domain right now, which filter would be hardest to apply and why?

Share this insight
"The best fine-tuning dataset isn't the biggest one. It's the most consistent one. 500 examples that agree beat 5,000 examples that contradict each other."

You might also like

Want help designing a fine-tuning dataset for your use case?

Book a free 30-min call with Arjun

References

  1. Wei, J. et al. (2022). Finetuned Language Models are Zero-Shot Learners. arXiv:2109.01652.
  2. Zhou, C. et al. (2023). LIMA: Less Is More for Alignment. arXiv:2305.11206.
  3. Longpre, S. et al. (2023). The Flan Collection: Designing Data and Methods for Effective Instruction Tuning. arXiv:2301.13688.
  4. Lee, K. et al. (2022). Deduplicating Training Data Makes Language Models Better. arXiv:2107.06499.
  5. Taori, R. et al. (2023). Stanford Alpaca: An Instruction-following LLaMA Model. GitHub, Stanford CRFM.
  6. Rao, A., Jaggi, A., Naidu, S. (2025). MEDFIT-LLM. IEEE RMKMATE 2025. DOI:10.1109/RMKMATE64574.2025.11042816.
← Module 1: Why Fine-Tune Module 3: LoRA →
Was this helpful?