SFT dataset format: JSONL examples for chat models and tool calling
For agent builders4 min read
The SFT dataset format depends on the trainer, but the common ones are plain text, prompt and completion pairs, and chat messages. Supervised fine-tuning (SFT) data is a file of examples that show a model the answer you want. This post shows each format as JSONL, following the Hugging Face TRL docs, and adds a short script to check rows before you train.
SFT dataset format: what TRL accepts
TRL's dataset guide separates format from type. The format is standard (plain strings) or conversational (lists of messages with a role and content). The type is the task the data suits. For SFT, the guide says its trainer expects a language modeling dataset or a prompt-completion dataset.
Source: TRL: dataset formats and types(opens in a new tab)
| Type | Standard example | Conversational example |
|---|---|---|
| Language modeling | {"text": "The sky is blue."} | {"messages": [{"role": "user", ...}, {"role": "assistant", ...}]} |
| Prompt-completion | {"prompt": "The sky is", "completion": " blue."} | {"prompt": [{"role": "user", ...}], "completion": [{"role": "assistant", ...}]} |
Chat rows in JSONL
For chat models, the conversational prompt-completion layout keeps the question and the answer in separate fields, which makes it clear which part the model should learn to produce. Each line of the file is one example.
{"prompt": [{"role": "user", "content": "What color is the sky?"}], "completion": [{"role": "assistant", "content": "It is blue."}]}{"prompt": [{"role": "user", "content": "Where is the sun?"}], "completion": [{"role": "assistant", "content": "In the sky."}]}The language modeling form puts everything in one messages list instead. The guide also notes the two are processed differently, since a prompt expects the model to continue and a full sequence is treated as finished.
{"messages": [ {"role": "user", "content": "What color is the sky?"}, {"role": "assistant", "content": "It is blue."}]}In the TRL docs, a dataset object is built in Python from a dictionary of columns, and the same column names apply.
from datasets import Dataset dataset = Dataset.from_dict({ "prompt": ["The sky is", "The sun is"], "completion": [" blue.", " in the sky."],})Tool calling rows
For models that call tools, the guide says the assistant message carries a tool_calls field instead of content, a tool message returns the result, and the row needs an extra tools column with the list of tools as JSON schema. Tool arguments are arbitrary JSON, so the guide uses a Json type when loading with the datasets library. Older versions of that library need the tools stored as a JSON string.
The companion post on function calling datasets shows a full row and a validator for it.
A check before you train
Most training failures with new data come from small format slips. This script checks the fields the conversational prompt-completion layout needs and a few things that reviewers often miss.
import json def check_line(line): problems = [] row = json.loads(line) for key in ("prompt", "completion"): if key not in row or not row[key]: problems.append(f"missing {key}") return problems for msg in row["prompt"] + row["completion"]: if msg.get("role") not in {"system", "user", "assistant", "tool"}: problems.append(f"bad role {msg.get('role')}") if not (msg.get("content") or msg.get("tool_calls")): problems.append("empty message") if row["completion"][0].get("role") != "assistant": problems.append("completion must start with assistant") if row["prompt"][-1].get("role") not in {"user", "tool"}: problems.append("prompt should end with user or tool turn") return problems good = '{"prompt": [{"role": "user", "content": "Hi"}], ' \ '"completion": [{"role": "assistant", "content": "Hello"}]}'bad = '{"prompt": [{"role": "user", "content": ""}], ' \ '"completion": [{"role": "user", "content": "Hello"}]}'print(check_line(good))print(check_line(bad))What makes an SFT example good
The format is the easy part. The answer is what the model will copy. Write the completions the way you want the model to behave on an average day, not the best answer you could write in an hour. Keep style consistent: one tone, one level of detail, the same way of refusing. If half the examples apologize and half do not, the model will do both.
- Cover the real mix of requests, including short, vague and badly spelled ones.
- Include cases where the right behavior is to refuse or ask a question.
- Remove duplicates, since repeated rows push the model toward them.
- Read a random sample after every export. Format bugs show up quickly to a human eye.
- Keep a record of who wrote or reviewed each example.
Curatrain's experts write expert demonstrations for supervised fine-tuning, with specialists in fields like law, medicine and code. For a trainer, the same rule applies as above: have a person who knows the field read the examples before they go into a training run.
