
Finetune OpenAI Model with JSON Schema Structured Output
OpenAI documentation covers fine tuning of their GPT large language models well. It also describes the structured output schema details for json_schema or json_output. However, the combination of these two are not described or documented well anywhere.
I had to read through plethora of forums both on OpenAI and elsewhere to find out how to provide the structured JSON schema for the fine tuning of the GPT models.
I am providing with a tested fine-tuning solution that works and the model will correctly output JSON schema, when requested as such (e.g. you will need to provide JSON schema in your requests to the fine-tuned model, but more on that later).
How to fine-tune OpenAI model with structured JSON schema (json_schema) inputs?
I used the supervised fine-tuning method (SFT) to provide structured JSON data for the fine-tune.
JSON schema (desired output) example
{"task": "fine-tune", "output": "json_schema", "period": "short", "type": "question", "abusive": "false"}
JSONL format with structured JSON data included
If you require JSON schema output, you need to include it as part of the assistant response / content. That means you will neeed to escape existing double quotes as to keep your JSON schema valid on the output.
Warning: Do not use single quotes! Single quotes are invalid in JSON and while OpenAI will happily accept that, the following output from the fine-tuned model will be messed up (e.g. responding with JSON with single quotes). Always escape (prefix with slashes) any double quotes in your desired structured format.
{
"messages": [
{
"role": "system",
"content": "You are my IT assistant for fine-tuning of ChatGPT models..."
},
{
"role": "user",
"content": "How do you fine-tune large language model in OpenAI with structured JSON schema data?"
},
{
"role": "assistant",
"content": "{\"task\": \"fine-tune\", \"output\": \"json_schema\", \"period\": \"short\", \"type\": \"question\", \"abusive\": \"false\"}"
}
]
}
Your JSONL file for fine-tuning process will include multiple messages like the one above, at least 100 are recommended, each on one line. You will have the final JSONL file formatted as this (usually with different user/assistant content on each line):
{"messages":[{"role":"system","content":"You are my IT assistant for the fine-tuning of ChatGPT models..."},{"role":"user","content":"How do you fine-tune large language model in OpenAI with structured JSON schema data?"},{"role":"assistant","content":"{\"task\": \"fine-tune\", \"output\": \"json_schema\", \"period\": \"short\", \"type\": \"question\", \"abusive\": \"false\"}"}]}
{"messages":[{"role":"system","content":"You are my IT assistant for the fine-tuning of ChatGPT models..."},{"role":"user","content":"How do you fine-tune large language model in OpenAI with structured JSON schema data?"},{"role":"assistant","content":"{\"task\": \"fine-tune\", \"output\": \"json_schema\", \"period\": \"short\", \"type\": \"question\", \"abusive\": \"false\"}"}]}
{"messages":[{"role":"system","content":"You are my IT assistant for the fine-tuning of ChatGPT models..."},{"role":"user","content":"How do you fine-tune large language model in OpenAI with structured JSON schema data?"},{"role":"assistant","content":"{\"task\": \"fine-tune\", \"output\": \"json_schema\", \"period\": \"short\", \"type\": \"question\", \"abusive\": \"false\"}"}]}
{"messages":[{"role":"system","content":"You are my IT assistant for the fine-tuning of ChatGPT models..."},{"role":"user","content":"How do you fine-tune large language model in OpenAI with structured JSON schema data?"},{"role":"assistant","content":"{\"task\": \"fine-tune\", \"output\": \"json_schema\", \"period\": \"short\", \"type\": \"question\", \"abusive\": \"false\"}"}]}
I have a fine-tuned model, now how do I request JSON schema structured response?
That’s a great question, so just follow the steps below:
- Prepare you json_schema (structured data format)
- Use strict schema and request no additional properties to be provided
- Use response_format as type = json_schema and then json_schema defined with your properties.
- Provide your structured JSON schema to OpenAI API
- Run the request
- Receive the response (in structured JSON formatted as per your request)
JSON Schema example to provide for the API requests
{
"model": "xxxx",
"temperature": 1,
"messages": [
{
"role": "system",
"content": "your system prompt"
},
{
"role": "user",
"content": "your user prompt"
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "any_name_you_want",
"strict": true,
"schema": {
"type": "object",
"properties": {
"task": {
"type": "string",
"description": "task classification based on user request",
"anyOf": [
{
"const": "fine-tune",
"title": "related to fine-tuning of LLMs"
},
{
"const": "os",
"title": "related to operating systems"
},
{
"const": "browser",
"title": "related to browsers"
},
{
"const": "web",
"title": "related to web pages"
},
{
"const": "other",
"title": "related to anything else"
}
],
"xxx": {
"type": "string",
"description": "etc.",
"anyOf": [
{
"const": "0",
"title": "neutral"
},
{
"const": "1",
"title": "positive"
},
{
"const": "-1",
"title": "negative"
}
]
},
"required": [
"task",
"xxx"
],
"additionalProperties": false
}
}
}
}
}
}
And that’s it! From now on the fine-tuned model will respond with proper JSON schema with only the properties you requested and no additional properties due to the strict setting.