How to intermediate · 3 min read

Fix fine-tuned model not following format

Quick answer
Ensure your fine-tuning training data uses the correct message format with system, user, and assistant roles in the messages array. Use the OpenAI SDK v1+ client.fine_tuning.jobs.create() with properly formatted JSONL files containing full conversation turns to enforce the desired output format.

PREREQUISITES

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

Setup

Install the latest OpenAI Python SDK and set your API key as an environment variable.

bash
pip install --upgrade openai
output
Requirement already satisfied: openai in /usr/local/lib/python3.10/site-packages (1.x.x)

Step by step

Prepare your fine-tuning training file with properly formatted messages and create a fine-tuning job using the OpenAI SDK v1+.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Example training data line (JSONL format):
# {"messages": [{"role": "system", "content": "You are a helpful assistant."},
#               {"role": "user", "content": "Provide a summary."},
#               {"role": "assistant", "content": "Here is the summary..."}]}

# Upload training file
training_file = client.files.create(
    file=open("training_data.jsonl", "rb"),
    purpose="fine-tune"
)

# Create fine-tuning job
job = client.fine_tuning.jobs.create(
    training_file=training_file.id,
    model="gpt-4o-mini-2024-07-18"
)

print(f"Fine-tuning job created with ID: {job.id}")

# After fine-tuning completes, use the fine-tuned model
# response = client.chat.completions.create(
#     model=job.fine_tuned_model,
#     messages=[{"role": "user", "content": "Your prompt here"}]
# )
# print(response.choices[0].message.content)
output
Fine-tuning job created with ID: ftjob-abc123xyz

Common variations

You can fine-tune different base models or use async calls. Ensure your training data always includes full conversation turns with system, user, and assistant roles to maintain format consistency.

python
import asyncio

async def fine_tune_async():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    training_file = await client.files.acreate(
        file=open("training_data.jsonl", "rb"),
        purpose="fine-tune"
    )
    job = await client.fine_tuning.jobs.acreate(
        training_file=training_file.id,
        model="gpt-4o-mini-2024-07-18"
    )
    print(f"Async fine-tuning job ID: {job.id}")

asyncio.run(fine_tune_async())
output
Async fine-tuning job ID: ftjob-abc123xyz

Troubleshooting

  • If your fine-tuned model output ignores the expected format, verify your training data uses the full messages array with system, user, and assistant roles.
  • Check that your training JSONL lines are valid JSON and not truncated.
  • Use the latest OpenAI SDK and avoid deprecated parameters like functions= or function_call= in fine-tuning.

Key Takeaways

  • Always format fine-tuning training data as full conversation messages with system, user, and assistant roles.
  • Use the OpenAI SDK v1+ client.fine_tuning.jobs.create() method with properly formatted JSONL files.
  • Avoid deprecated parameters and ensure your training data JSONL lines are valid and complete.
  • Test your fine-tuned model with chat completions using the fine-tuned model ID to confirm format adherence.
Verified 2026-04 · gpt-4o-mini-2024-07-18
Verify ↗