Hi Jeremy Seow,
I understood that you are encountering issues with API calls to ai.azure.com for models O1 and above, while oai.azure.com works fine.
Understanding the Issue:
BadRequest Error: Indicates that API Version Mismatch
The error indicates that the requested model (O1 and above) is only available for API versions 2024-12-01-preview or later.
If you are using an older API version (e.g., 2023-06-01-preview), the request will fail because the newer models are not supported with outdated API versions.
401 Access Denied Error: Indicates that Authentication Failure
This error usually occurs due to
Incorrect API Key.
Incorrect endpoint (regional vs. global).
Subscription does not have access to the requested model.
The API request is being made to the wrong authentication method (e.g., trying to use ai.azure.com with an API key when it expects Azure Active Directory authentication).
Possible Resolution Steps:
Use the Correct API Version:
Ensure you specify the required API version in your request. Since models O1 and above require 2024-12-01-preview, update your API calls like this:
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
url = f"https://{region}.api.cognitive.microsoft.com/openai/deployments/{deployment_id}/chat/completions?api-version=2024-12-01-preview"
Verify the Endpoint (ai.azure.com vs. oai.azure.com):
ai.azure.com requires Azure Active Directory (AAD) authentication.
oai.azure.com supports API key-based authentication.
Solution:
If you are using an API key, switch your endpoint to oai.azure.com.
If you must use ai.azure.com, you need to authenticate via Azure Active Directory.
Ensure Correct Authentication Method:
For ai.azure.com (AAD Authentication)
Use Azure Active Directory (OAuth 2.0) token authentication.
Retrieve a valid token before making requests:
import requests
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
token = credential.get_token("https://cognitiveservices.azure.com/.default")
headers = {
"Authorization": f"Bearer {token.token}",
"Content-Type": "application/json"
}
For oai.azure.com (API Key Authentication):
Use your API key in the headers:
headers = {
"api-key": "YOUR_OPENAI_API_KEY",
"Content-Type": "application/json"
}
Validate Your Subscription and Model Access:
Go to Azure Portal → OpenAI Service → Model Deployments.
Check if:
Your subscription includes access to O1 and newer models.
The models are properly deployed in your resource region.
Your API key or AAD authentication has the necessary permissions.
Verify Regional Endpoints:
If using ai.azure.com, ensure your endpoint is:
https://<your-region>.api.cognitive.microsoft.com/
If using oai.azure.com, ensure it matches the deployment:
https://<your-resource-name>.openai.azure.com/
Hope this helps. Do let us know if you any further queries.
Thank you.