OpenAIError
openai.OpenAIError (Azure OpenAI deployment not found)
Stack trace
openai.OpenAIError: The deployment named 'my-model-deployment' was not found. Please verify your Azure OpenAI deployment name and region configuration.
Why it happens
This error occurs because the Azure OpenAI client cannot find the specified model deployment in the configured Azure resource. It usually means the deployment name is misspelled, missing, or the Azure region or resource endpoint is incorrect.
Detection
Check for OpenAIError exceptions with 404 status and error messages mentioning deployment not found. Log the deployment name and Azure endpoint used in the request.
Causes & fixes
Incorrect or misspelled deployment name in the client configuration
Verify and correct the deployment name string to exactly match the Azure OpenAI deployment name.
Azure OpenAI resource region or endpoint is misconfigured or missing
Ensure the Azure OpenAI API base URL and region match the resource where the deployment exists.
Deployment was not created or has been deleted in the Azure portal
Create the deployment in the Azure portal or redeploy the model with the correct name.
Code: broken vs fixed
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ['AZURE_OPENAI_API_KEY'],
api_base=os.environ['AZURE_OPENAI_API_BASE'],
api_type='azure',
api_version='2023-05-15'
)
response = client.chat.completions.create(
model='my-model-deployment', # Incorrect or missing deployment name
messages=[{"role": "user", "content": "Hello"}]
) # This line triggers the deployment not found error import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ['AZURE_OPENAI_API_KEY'],
api_base=os.environ['AZURE_OPENAI_API_BASE'],
api_type='azure',
api_version='2023-05-15'
)
response = client.chat.completions.create(
model=os.environ['AZURE_OPENAI_DEPLOYMENT_NAME'], # Fixed: use correct deployment name from env
messages=[{"role": "user", "content": "Hello"}]
)
print(response) # Shows successful response Workaround
Catch OpenAIError exceptions, log the deployment name and endpoint, and fallback to a default or backup deployment if available.
Prevention
Automate deployment name and Azure endpoint configuration via environment variables or config files, and validate deployment existence during startup.