OpenAIError
openai.OpenAIError (model deprecated migration error)
Stack trace
openai.OpenAIError: The model you requested is deprecated and no longer available. Please migrate to a supported model as per the latest OpenAI API documentation.
Why it happens
OpenAI periodically deprecates older models to improve service quality and security. When your code references a deprecated model name, the API rejects the request with this error. This forces developers to migrate to newer, supported models.
Detection
Monitor API error responses for OpenAIError messages indicating model deprecation, and log the model name used in requests to identify outdated references before failures occur.
Causes & fixes
Using a model name that OpenAI has deprecated and removed from the API.
Update your code to use a currently supported model such as 'gpt-4o' or 'gpt-4o-mini' as documented in the latest OpenAI SDK.
Hardcoded model names in legacy code or configuration files that have not been updated.
Replace all hardcoded deprecated model names with environment variables or config entries pointing to supported models, and verify usage throughout your codebase.
Using an outdated OpenAI SDK version that defaults to deprecated models.
Upgrade the OpenAI Python SDK to the latest version and explicitly specify a supported model in your client calls.
Code: broken vs fixed
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
response = client.chat.completions.create(
model='gpt-4', # deprecated model triggers error
messages=[{'role': 'user', 'content': 'Hello'}]
)
print(response) import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
response = client.chat.completions.create(
model='gpt-4o', # updated to supported model
messages=[{'role': 'user', 'content': 'Hello'}]
)
print(response) # fixed: uses supported model Workaround
Catch OpenAIError exceptions, detect deprecated model messages, and programmatically switch to a fallback supported model before retrying the request.
Prevention
Regularly audit your code and dependencies for deprecated model usage and adopt environment-driven model configuration to enable seamless updates without code changes.