Critical severity HTTP 401 beginner · Fix: 2-5 min

AuthenticationError

openai.AuthenticationError (HTTP 401)

What this error means
OpenAI AuthenticationError occurs when the API key is missing, invalid, or unauthorized, blocking all API requests.

Stack trace

traceback
openai.AuthenticationError: Invalid API key provided: sk-xxxxxx
    at openai.api_requestor.APIRequestor._handle_error_response (/path/to/sdk/requestor.py:123)
    at openai.api_requestor.APIRequestor.request (/path/to/sdk/requestor.py:85)
    at openai.client.ChatCompletion.create (/path/to/sdk/client.py:45)
    at main.py:15
QUICK FIX
Set the OPENAI_API_KEY environment variable correctly and restart your application to authenticate successfully.

Why it happens

This error happens because the OpenAI client cannot authenticate your request due to a missing, malformed, expired, or revoked API key. The server returns HTTP 401 Unauthorized, preventing any further API calls.

Detection

Check for openai.AuthenticationError exceptions during API calls and verify that the API key environment variable is set and correctly loaded before making requests.

Causes & fixes

1

API key environment variable is not set or misspelled

✓ Fix

Set the environment variable OPENAI_API_KEY correctly before running your application, e.g., export OPENAI_API_KEY='your_key_here'.

2

Using an expired or revoked API key

✓ Fix

Generate a new API key from the OpenAI dashboard and update your environment variable accordingly.

3

Passing the API key incorrectly in code (e.g., hardcoded or wrong parameter)

✓ Fix

Use os.environ to load the API key and pass it to the OpenAI client constructor properly, never hardcode keys.

4

Network or proxy issues causing the API key to be stripped or altered

✓ Fix

Ensure your network or proxy does not modify headers or environment variables; test connectivity without proxies.

Code: broken vs fixed

Broken - triggers the error
python
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}])  # Fails with AuthenticationError
Fixed - works correctly
python
import os
from openai import OpenAI
os.environ["OPENAI_API_KEY"] = "your_actual_api_key_here"  # Set your API key securely
client = OpenAI()
response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}])
print(response)
Added setting of OPENAI_API_KEY environment variable to ensure the OpenAI client authenticates requests properly.

Workaround

If you cannot set environment variables immediately, pass the API key explicitly when initializing the client if supported, or catch AuthenticationError and prompt for key input.

Prevention

Use environment variables to manage API keys securely and validate their presence at application startup to avoid runtime authentication failures.

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.