InvalidRequestError
openai.error.InvalidRequestError
Stack trace
openai.error.InvalidRequestError: Invalid hyperparameter value: 'batch_size' must be a positive integer less than or equal to 256
at openai.api_requestor.APIRequestor._interpret_response (openai/api_requestor.py:123)
at openai.api_requestor.APIRequestor.request (openai/api_requestor.py:70)
at openai.api_resources.finetune.FineTune.create (openai/api_resources/finetune.py:45)
at main.py:25 Why it happens
The OpenAI fine-tuning API validates hyperparameters strictly. If you provide a hyperparameter with a value outside the allowed range or an unsupported type, the API rejects the request with an InvalidRequestError. This prevents invalid fine-tuning jobs from being created.
Detection
Check the error message returned by the OpenAI fine-tuning API call; it explicitly states which hyperparameter is invalid and why. Logging the full API response helps identify the exact parameter causing the failure.
Causes & fixes
Setting 'batch_size' to a value greater than the maximum allowed (e.g., >256)
Set 'batch_size' to an integer between 1 and 256 inclusive as per OpenAI fine-tuning API documentation.
Providing a non-integer or negative value for 'n_epochs'
Ensure 'n_epochs' is a positive integer, typically between 1 and 100, following OpenAI guidelines.
Using unsupported hyperparameters not recognized by the fine-tuning API
Remove or correct any hyperparameters not listed in the official OpenAI fine-tuning parameter list.
Passing hyperparameters as strings instead of their expected types (e.g., 'batch_size' as '32' string)
Pass hyperparameters using the correct data types, such as integers for 'batch_size' and 'n_epochs'.
Code: broken vs fixed
import os
from openai import OpenAI
client = OpenAI()
response = client.fine_tunes.create(
training_file="file-abc123",
model="curie",
batch_size=512, # Invalid: exceeds max allowed
n_epochs='3' # Invalid: should be int, not str
) # This line raises InvalidRequestError
print(response) import os
from openai import OpenAI
client = OpenAI()
response = client.fine_tunes.create(
training_file="file-abc123",
model="curie",
batch_size=32, # Fixed: valid integer within allowed range
n_epochs=3 # Fixed: integer type
) # Now this call succeeds
print(response) Workaround
Catch InvalidRequestError exceptions, parse the error message to identify invalid hyperparameters, and programmatically adjust or remove them before retrying the fine-tuning request.
Prevention
Always validate hyperparameters against the official OpenAI fine-tuning API documentation or schema before sending requests, and implement automated checks in your deployment pipeline.