InvalidRequestError
openai.InvalidRequestError (fine-tuning JSONL format invalid)
Stack trace
openai.InvalidRequestError: Invalid JSONL file format: each line must be a valid JSON object with required keys like 'prompt' and 'completion'.
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
Lines in the JSONL file are not valid JSON objects (e.g., missing quotes, trailing commas).
Use a JSON validator or linter to ensure each line is a valid JSON object without syntax errors.
Missing required keys like 'prompt' or 'completion' in some JSONL lines.
Ensure every JSON object line includes all required keys exactly as specified by OpenAI fine-tuning docs.
File contains empty lines or extra whitespace lines breaking JSONL format.
Remove all empty or whitespace-only lines from the JSONL file before uploading.
Using a text editor that inserts BOM or invisible characters corrupting JSONL format.
Save the file in UTF-8 without BOM and verify no hidden characters exist.
Code: broken vs fixed
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) 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 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.