High severity intermediate · Fix: 5-10 min

InvalidRequestError

openai.InvalidRequestError (fine-tuning JSONL format invalid)

What this error means
OpenAI fine-tuning fails because the uploaded JSONL file is not properly formatted as newline-delimited JSON objects.

Stack trace

traceback
openai.InvalidRequestError: Invalid JSONL file format: each line must be a valid JSON object with required keys like 'prompt' and 'completion'.
QUICK FIX
Validate and clean your JSONL file with a JSONL linter ensuring each line is a valid JSON object with required keys before upload.

Why it happens

OpenAI fine-tuning requires the training data file to be in JSONL format where each line is a valid JSON object with specific keys. If lines are malformed, missing required fields, or contain trailing commas, the API rejects the file with this error.

Detection

Validate your JSONL file locally by checking each line is valid JSON and contains the required keys before uploading to OpenAI fine-tuning API.

Causes & fixes

1

Lines in the JSONL file are not valid JSON objects (e.g., missing quotes, trailing commas).

✓ Fix

Use a JSON validator or linter to ensure each line is a valid JSON object without syntax errors.

2

Missing required keys like 'prompt' or 'completion' in some JSONL lines.

✓ Fix

Ensure every JSON object line includes all required keys exactly as specified by OpenAI fine-tuning docs.

3

File contains empty lines or extra whitespace lines breaking JSONL format.

✓ Fix

Remove all empty or whitespace-only lines from the JSONL file before uploading.

4

Using a text editor that inserts BOM or invisible characters corrupting JSONL format.

✓ Fix

Save the file in UTF-8 without BOM and verify no hidden characters exist.

Code: broken vs fixed

Broken - triggers the error
python
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# This JSONL file has invalid format (trailing commas and missing keys)
file_path = 'fine_tune_data_invalid.jsonl'

with open(file_path, 'rb') as f:
    response = client.fine_tunes.create(training_file=f)  # Raises InvalidRequestError here
    print(response)
Fixed - works correctly
python
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Fixed JSONL file with valid JSON objects and required keys
file_path = 'fine_tune_data_valid.jsonl'

with open(file_path, 'rb') as f:
    response = client.fine_tunes.create(training_file=f)  # Fixed: valid JSONL format
    print(response)

# Note: Ensure environment variable OPENAI_API_KEY is set for authentication
Fixed the JSONL file to contain valid JSON objects per line with required keys, removing trailing commas and syntax errors.

Workaround

If you cannot fix the JSONL file immediately, parse it line-by-line in Python, validate each JSON object, and rewrite a clean JSONL file before uploading.

Prevention

Automate JSONL validation in your data pipeline using JSON parsers and schema checks to guarantee compliance before fine-tuning uploads.

Python 3.9+ · openai >=1.0.0 · tested on 1.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.