How to beginner · 3 min read

How to use fine-tuned model with OpenAI API

Quick answer
Use the OpenAI Python SDK to upload your training file, create a fine-tuning job with client.fine_tuning.jobs.create(), then call your fine-tuned model by specifying its name in client.chat.completions.create(model=). Always retrieve the fine-tuned model name from the job status before usage.

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 openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl
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 completion, 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"])

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

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

# Step 3: 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}")

    # Step 4: 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 file ID: file-abc123xyz
Created fine-tuning job ID: job-xyz789abc
Job status: running
Job status: running
Job status: succeeded
Fine-tuned model ready: gpt-4o-mini-fine-tuned-2026-04-01
Response: Hello! I'm your fine-tuned assistant. How can I help you today?

Common variations

  • Use async calls with asyncio and await for non-blocking fine-tuning job polling.
  • Specify different base models like gpt-4o or gpt-4o-mini when creating fine-tuning jobs.
  • Use streaming completions with stream=True when calling the fine-tuned model for real-time output.
python
import asyncio
import os
from openai import OpenAI

async def async_fine_tune_and_use():
    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"
    )

    while True:
        status = await client.fine_tuning.jobs.aretrieve(job.id)
        print(f"Job status: {status.status}")
        if status.status in ["succeeded", "failed"]:
            break
        await asyncio.sleep(10)

    if status.status == "succeeded":
        fine_tuned_model = status.fine_tuned_model
        stream = await client.chat.completions.acreate(
            model=fine_tuned_model,
            messages=[{"role": "user", "content": "Hello async!"}],
            stream=True
        )
        async for chunk in stream:
            print(chunk.choices[0].delta.content or "", end="", flush=True)
        print()

asyncio.run(async_fine_tune_and_use())
output
Job status: running
Job status: running
Job status: succeeded
Hello async! (streamed output)

Troubleshooting

  • If you see file not found errors, verify the training file path and format (must be JSONL).
  • If the fine-tuning job fails, check the job logs via client.fine_tuning.jobs.list_events(job.id) for error details.
  • Ensure your API key has fine-tuning permissions and quota available.
  • Use the correct model base name when creating the fine-tuning job; outdated model names cause errors.

Key Takeaways

  • Upload your training data as a JSONL file with client.files.create() for fine-tuning.
  • Create and monitor fine-tuning jobs with client.fine_tuning.jobs.create() and retrieve().
  • Use the fine-tuned model name from the job status to call client.chat.completions.create(model=).
  • Async and streaming completions are supported for fine-tuned models.
  • Check job events and logs if fine-tuning fails to diagnose issues.
Verified 2026-04 · gpt-4o-mini-2024-07-18, gpt-4o-mini-fine-tuned-2026-04-01
Verify ↗