PermissionDeniedError
openai.PermissionDeniedError (HTTP 403)
Stack trace
openai.PermissionDeniedError: Error code: 403 - {'error': {'message': 'You do not have access to model: gpt-4o', 'type': 'permission_denied', 'param': 'model', 'code': 'permission_denied'}} Why it happens
This error happens because the API key used does not have permission to access the specified model. It can occur if the model is restricted, deprecated, or your account plan does not include access. The OpenAI server rejects the request with a 403 Forbidden status.
Detection
Monitor API responses for HTTP 403 errors and PermissionDeniedError exceptions. Log the model name and API key context to identify unauthorized access attempts before failing.
Causes & fixes
Using a model name that your API key is not authorized to access
Verify your OpenAI account subscription and ensure the model name is spelled correctly and included in your plan.
API key is revoked, expired, or incorrectly set in environment variables
Check that your API key is valid, active, and correctly loaded from environment variables before making requests.
Requesting a deprecated or private model that requires special access
Switch to a publicly available model or contact OpenAI support to request access to the restricted model.
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-4o",
messages=[{"role": "user", "content": "Hello"}]
) # This line triggers PermissionDeniedError 403
print(response) import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
# Changed model to one accessible by the API key
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello"}]
)
print(response) # Fixed: model access granted Workaround
Catch PermissionDeniedError exceptions and fallback to a default public model that your API key can access to maintain service continuity.
Prevention
Regularly verify your API key permissions and model availability in your OpenAI dashboard. Use environment variables securely and avoid hardcoding model names to prevent unauthorized access errors.