How to beginner · 3 min read

How to start fine-tuning job with OpenAI API

Quick answer
Use the OpenAI Python SDK v1 to upload your training file with client.files.create and then start a fine-tuning job with client.fine_tuning.jobs.create. Monitor the job status via client.fine_tuning.jobs.retrieve and use the fine-tuned model once ready.

PREREQUISITES

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

Setup

Install the official OpenAI Python SDK version 1 or higher and set your API key as an environment variable for secure authentication.

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

This example shows how to upload a training file, create a fine-tuning job, poll for its status, and then use the fine-tuned model for chat completions.

python
import os
import time
from openai import OpenAI

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

# Upload training data file (JSONL format)
training_file = client.files.create(
    file=open("training_data.jsonl", "rb"),
    purpose="fine-tune"
)
print(f"Uploaded training file ID: {training_file.id}")

# Create fine-tuning job
job = client.fine_tuning.jobs.create(
    training_file=training_file.id,
    model="gpt-4o-mini"
)
print(f"Started fine-tuning job ID: {job.id}")

# Poll job status until done
while True:
    status = client.fine_tuning.jobs.retrieve(job.id)
    print(f"Job status: {status.status}")
    if status.status in ["succeeded", "failed"]:
        break
    time.sleep(10)

if status.status == "succeeded":
    fine_tuned_model = status.fine_tuned_model
    print(f"Fine-tuned model ready: {fine_tuned_model}")

    # Use the fine-tuned model
    response = client.chat.completions.create(
        model=fine_tuned_model,
        messages=[{"role": "user", "content": "Hello, how are you?"}]
    )
    print("Response:", response.choices[0].message.content)
else:
    print("Fine-tuning job failed.")
output
Uploaded training file ID: file-abc123xyz
Started fine-tuning job ID: job-xyz789abc
Job status: running
Job status: running
Job status: succeeded
Fine-tuned model ready: gpt-4o-mini-ft-abc123
Response: Hello! I'm your fine-tuned assistant. How can I help you today?

Common variations

  • Use different base models like gpt-4o or gpt-4o-mini depending on your needs.
  • For asynchronous workflows, integrate job status polling with async frameworks or callbacks.
  • Streaming is not applicable for fine-tuning job creation but can be used when querying the fine-tuned model.

Troubleshooting

  • If you get a 403 Forbidden error, verify your API key and permissions.
  • Ensure your training file is in valid JSONL format with the correct message structure.
  • If the job fails, check the error details in the job status response for issues in training data or parameters.

Key Takeaways

  • Always upload your training data file with purpose="fine-tune" before starting a job.
  • Use client.fine_tuning.jobs.create to start fine-tuning with the uploaded file ID.
  • Poll the job status until it reaches 'succeeded' to get the fine-tuned model name.
  • Use the fine-tuned model name in your chat completions to leverage your custom model.
  • Validate your training data format and API key permissions to avoid common errors.
Verified 2026-04 · gpt-4o-mini, gpt-4o-mini
Verify ↗