The Problem: Azure OpenAI vs. Standard OpenAI Naming
Switching from OpenAI’s direct API to Azure OpenAI usually feels like a lateral move, but one specific naming convention trips up almost everyone. You have your API key ready. Your endpoint is verified. You know for a fact that GPT-4o is active in your region, such as eastus or swedencentral. Yet, when you run your script, the SDK throws a 404 error.
openai.NotFoundError: Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The model deployment at ID gpt-4o does not exist.'}}
The core of the problem lies in how Azure identifies models. With the direct OpenAI API, the model parameter refers to the base engine, like gpt-4o. Azure is different. In your Azure code, the model parameter must match the Deployment Name you created in Azure AI Studio. It does not matter what the underlying model is; the SDK only cares about that specific label.
The Debugging Process
I spent about 20 minutes double-checking my environment variables before realizing the issue wasn't my credentials. I had named my deployment gpt-4o-prod-01 in the portal, but my code was still looking for gpt-4o. It’s a simple mismatch, but it’s enough to break the connection.
The openai-python library (v1.0.0+) treats the model argument as a pointer to a specific deployment ID. If these strings don't match character-for-character, the Azure gateway cannot route your request. It returns a 404 because it literally cannot find a compute instance with that name.
Common triggers for this error:
- The deployment name in Azure AI Studio differs from the string in your Python script.
- Your deployment exists in a different Azure resource than the one your endpoint points to.
- The model is still in the 'Provisioning' state and isn't ready for traffic yet.
The Solution: Aligning Your Deployment Names
Fixing this requires a quick trip to the Azure portal to sync your identifiers.
1. Verify the Name in Azure AI Studio
Log into Azure AI Studio and click on the Deployments tab. Look closely at the Deployment name column. This exact string—including hyphens and underscores—is what your code needs.
For instance, if you named your deployment marketing-gpt4-v1, you must use that specific string in your script.
2. Update Your Python Code
Check your AzureOpenAI client initialization. Ensure the create call uses the deployment name from the portal. Here is a clean, working example:
import os
from openai import AzureOpenAI
# Initialize the client with your resource details
client = AzureOpenAI(
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version="2024-08-01-preview" # Use a current, stable API version
)
# WRONG: model="gpt-4o"
# RIGHT: model="your-actual-deployment-name"
response = client.chat.completions.create(
model="gpt-4o-prod-01", # This MUST match the Deployment Name in Azure
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Test connection."}
]
)
print(response.choices[0].message.content)
3. Check API Version Compatibility
Using an old api_version can sometimes cause routing failures with newer models like GPT-4o. If you still see errors, check the Azure OpenAI docs for the latest version string. Values like 2024-02-01 or 2024-08-01-preview are common choices for modern deployments.
Verification: List Your Deployments Programmatically
If you want to stop guessing, use a small helper script to see exactly what Azure sees. This confirms your API key works and lists every available deployment ID currently active on your resource.
import os
import requests
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
api_key = os.getenv("AZURE_OPENAI_API_KEY")
url = f"{endpoint}/openai/deployments?api-version=2024-02-01"
headers = {"api-key": api_key}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()["data"]
for deployment in data:
print(f"Valid Deployment ID: {deployment['id']} | Base Model: {deployment['model']}")
else:
print(f"Failed to fetch: {response.status_code}")
Summary of Best Practices
Azure OpenAI requires a slightly different mindset than the standard OpenAI API. Keep these rules in mind to avoid future 404s:
- Model Name vs. Deployment ID: Always treat the
modelparameter as adeployment_id. - Use Environment Variables: Avoid hardcoding deployment names. Store them in a variable like
AZURE_DEPLOYMENT_IDso you can swap between 'dev' and 'prod' environments easily. - Check Regional Availability: Ensure your resource region (e.g.,
uksouth) actually supports the specific model version you want to deploy.

