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

PermissionDeniedError

openai.PermissionDeniedError (HTTP 403)

What this error means
OpenAI PermissionDeniedError 403 occurs when your API key lacks access rights to the requested model or resource.

Stack trace

traceback
openai.PermissionDeniedError: Error code: 403 - {'error': {'message': 'You do not have access to model: gpt-4o', 'type': 'permission_denied', 'param': 'model', 'code': 'permission_denied'}}
QUICK FIX
Confirm your API key has access to the requested model and update the model parameter to a supported one in your code.

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

1

Using a model name that your API key is not authorized to access

✓ Fix

Verify your OpenAI account subscription and ensure the model name is spelled correctly and included in your plan.

2

API key is revoked, expired, or incorrectly set in environment variables

✓ Fix

Check that your API key is valid, active, and correctly loaded from environment variables before making requests.

3

Requesting a deprecated or private model that requires special access

✓ Fix

Switch to a publicly available model or contact OpenAI support to request access to the restricted model.

Code: broken vs fixed

Broken - triggers the error
python
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)
Fixed - works correctly
python
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
Changed the model parameter to a model that the API key is authorized to access, preventing the 403 PermissionDeniedError.

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.

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

Community Notes

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