How to Beginner · 3 min read

How to format chat data for fine-tuning

Quick answer
To format chat data for fine-tuning, structure your dataset as a JSONL file where each line contains a JSON object with a messages array representing the conversation turns. Each message must have a role (e.g., user, assistant) and content string. This format aligns with chat-based models and enables effective supervised fine-tuning.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the OpenAI Python SDK and set your API key as an environment variable to prepare for fine-tuning data creation.

bash
pip install openai

Step by step

Format your chat fine-tuning data as a JSONL file where each line is a JSON object with a messages array. Each message has a role and content. For example, a user prompt and assistant completion look like this:

json
{
  "messages": [
    {"role": "user", "content": "Hello, how do I write a loop in Python?"},
    {"role": "assistant", "content": "You can use a for loop like this: for i in range(5): print(i)"}
  ]
}

// Save multiple such JSON objects, one per line, in a file named fine_tune_data.jsonl

Common variations

You can also format fine-tuning data as prompt-completion pairs for older models by concatenating messages into a single prompt string and the assistant reply as completion. However, for chat models like gpt-4o, use the messages array format. Async or streaming fine-tuning data preparation is uncommon but possible with custom pipelines.

Troubleshooting

If your fine-tuning job fails, verify your JSONL file is valid JSON with no trailing commas and that each messages array contains at least one user and one assistant message. Also, ensure your API key is correctly set in os.environ and you are using a supported fine-tuning model.

Key Takeaways

  • Use a JSONL file with a messages array for each fine-tuning example to match chat model input format.
  • Each message must have a role (like user or assistant) and content string.
  • Validate your JSONL file to avoid syntax errors and ensure proper message sequencing.
  • For chat models, avoid concatenating prompts and completions into single strings; use structured messages instead.
  • Set your API key securely in os.environ and use the latest OpenAI SDK patterns for fine-tuning.
Verified 2026-04 · gpt-4o
Verify ↗