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

AuthenticationError

openai.error.AuthenticationError

What this error means
Azure OpenAI AuthenticationError occurs when the API key or endpoint URL is invalid or misconfigured, blocking access.

Stack trace

traceback
openai.error.AuthenticationError: Invalid API key or endpoint URL provided for Azure OpenAI service.
QUICK FIX
Set AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT environment variables correctly and configure the OpenAI client with Azure parameters.

Why it happens

This error happens because the Azure OpenAI client requires a valid API key and a correctly formatted endpoint URL. If either is missing, incorrect, or mismatched, the service rejects the request with an authentication failure.

Detection

Check for AuthenticationError exceptions when initializing or calling the Azure OpenAI client; log the API key and endpoint environment variables to verify correctness before requests.

Causes & fixes

1

API key environment variable is missing or empty

✓ Fix

Set the environment variable AZURE_OPENAI_API_KEY with your valid Azure OpenAI API key before running the code.

2

Endpoint URL environment variable is missing or malformed

✓ Fix

Set AZURE_OPENAI_ENDPOINT to the full Azure OpenAI endpoint URL including https:// and the correct resource name.

3

Using OpenAI SDK client without specifying Azure deployment and endpoint parameters

✓ Fix

Use the Azure-specific parameters: set 'api_key', 'api_base', 'api_type'='azure', and 'api_version' when creating the OpenAI client.

4

Mixing OpenAI public API key with Azure endpoint or vice versa

✓ Fix

Ensure you use the Azure API key with the Azure endpoint and specify 'api_type'='azure' to avoid authentication conflicts.

Code: broken vs fixed

Broken - triggers the error
python
from openai import OpenAI
client = OpenAI(api_key="wrong_or_missing_key")  # causes AuthenticationError
response = client.chat.completions.create(model="gpt-4o", messages=[{"role": "user", "content": "Hello"}])
Fixed - works correctly
python
import os
from openai import OpenAI

os.environ["AZURE_OPENAI_API_KEY"] = "your_azure_api_key_here"
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://your-resource-name.openai.azure.com/"

client = OpenAI(
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    api_base=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_type="azure",
    api_version="2023-05-15"
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}]
)
print(response.choices[0].message.content)  # fixed: correct Azure auth and endpoint
Configured the OpenAI client with correct Azure API key, endpoint, api_type, and api_version to authenticate successfully.

Workaround

Catch AuthenticationError exceptions and prompt the user or system to verify and reset the AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT environment variables before retrying.

Prevention

Use environment variables for Azure API key and endpoint, validate them at app startup, and always configure the OpenAI client with Azure-specific parameters to avoid auth errors.

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

Community Notes

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