High severity HTTP 400 beginner · Fix: 2-5 min

OpenAIError

openai.OpenAIError (file upload size limit exceeded)

What this error means
This error occurs when the file uploaded to OpenAI exceeds the maximum allowed size limit for file uploads.

Stack trace

traceback
openai.OpenAIError: File size exceeds the maximum allowed limit of 10485760 bytes (10MB)
QUICK FIX
Validate and reduce your file size below 10MB before calling the OpenAI file upload API.

Why it happens

OpenAI enforces a maximum file size limit (commonly 10MB) for file uploads to ensure efficient processing and resource management. Uploading a file larger than this limit triggers this error.

Detection

Check the file size before uploading by inspecting the file's byte size and log or raise a warning if it exceeds the allowed limit to prevent the error.

Causes & fixes

1

Uploading a file larger than OpenAI's maximum allowed size (typically 10MB).

✓ Fix

Check the file size before upload and split or compress the file to be under the limit.

2

Not validating file size client-side before sending to the OpenAI API.

✓ Fix

Add client-side validation to reject or resize files exceeding the size limit before API call.

3

Using an outdated or incorrect API method that does not handle large files properly.

✓ Fix

Use the latest OpenAI SDK file upload methods and confirm file size limits in the official docs.

Code: broken vs fixed

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

file_path = 'large_file.json'
with open(file_path, 'rb') as f:
    # This line triggers the error if file is too large
    response = client.files.create(file=f, purpose='fine-tune')
    print(response)
Fixed - works correctly
python
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

file_path = 'large_file.json'
file_size = os.path.getsize(file_path)
max_size = 10 * 1024 * 1024  # 10MB limit

if file_size > max_size:
    raise ValueError(f'File size {file_size} exceeds 10MB limit')

with open(file_path, 'rb') as f:
    response = client.files.create(file=f, purpose='fine-tune')  # Added size check before upload
    print(response)
Added a file size check before uploading to ensure the file does not exceed OpenAI's 10MB limit, preventing the error.

Workaround

If you cannot reduce the file size, split the file into smaller chunks and upload them separately or compress the file before upload.

Prevention

Implement client-side file size validation and use compression or chunking strategies to keep uploads within OpenAI's size limits.

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

Community Notes

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