AzureOpenAi is unable to process chat.completions when the API version is set to preview.
Confirm this is an issue with the Python library and not an underlying OpenAI API
- [x] This is an issue with the Python library
Describe the bug
When we attempt to use "chat.completions.create" and set the api-version to "preview," we receive the following error: "openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}."
Root cause
The root cause of this error is that Azure has changed how the "preview" API version works. They now use base_url instead of azure_endpoint, and the new endpoint for /chat/completions is https://your-resource-name.openai.azure.com/openai/v1/chat/completions?api-version=preview. However, the current SDK code tries to add /deployments/{model} to the URL, resulting in `https://your-resource-name.openai.azure.com/openai/v1/openai/deployments/gpt-5-chat/chat/completions?api-version=preview``, which is incorrect. This causes the resource not found error.
Suggestion:
Now
@override
def _build_request(
self,
options: FinalRequestOptions,
*,
retries_taken: int = 0,
) -> httpx.Request:
if options.url in _deployments_endpoints and is_mapping(options.json_data):
model = options.json_data.get("model")
if model is not None and "/deployments" not in str(self.base_url.path):
options.url = f"/deployments/{model}{options.url}"
return super()._build_request(options, retries_taken=retries_taken)
After
@override
def _build_request(
self,
options: FinalRequestOptions,
*,
retries_taken: int = 0,
) -> httpx.Request:
if options.url in _deployments_endpoints and is_mapping(options.json_data):
model = options.json_data.get("model")
if model is not None and self._api_version not in ["preview", "latest"] and "/deployments" not in str(self.base_url.path):
options.url = f"/deployments/{model}{options.url}"
return super()._build_request(options, retries_taken=retries_taken)
Reference: [1] https://learn.microsoft.com/en-us/azure/ai-foundry/openai/api-version-lifecycle?tabs=entra#next-generation-api-1 [2] https://learn.microsoft.com/en-us/azure/ai-foundry/openai/reference-preview-latest#create-chatcompletion [3] https://github.com/openai/openai-python/blob/main/src/openai/lib/azure.py#L65-L66
https://learn.microsoft.com/en-us/azure/ai-foundry/openai/reference-preview-latest#create-chatcompletion
To Reproduce
- Create virtual env
python -m venv .venv
source .venv/bin/activate
pip install openai==1.101.0
pip install azure-identity
- create testing code
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.chat.completions.create(
model="gpt-5-chat",
messages=[
{"role": "user", "content": input},
]
)
print(response.model_dump_json(indent=2))
- python test.py
(.venv) user@xxx:~/genai_workshop/azure_workshop$ python test.py
> /home/user/genai_workshop/azure_workshop/call_o1_model.py(33)<module>()
-> openai_client = AzureOpenAI(
(Pdb) c
Current model you are using gpt-5-chat
> /home/user/genai_workshop/.venv/lib/python3.10/site-packages/openai/lib/azure.py(66)_build_request()
-> if model is not None and "/deployments" not in str(self.base_url.path):
(Pdb) c
> /home/user/genai_workshop/.venv/lib/python3.10/site-packages/openai/_base_client.py(983)request()
-> response = self._client.send(
(Pdb) c
Traceback (most recent call last):
File "/home/user/genai_workshop/azure_workshop/call_o1_model.py", line 41, in <module>
response = openai_client.chat.completions.create(
File "/home/user/genai_workshop/.venv/lib/python3.10/site-packages/openai/_utils/_utils.py", line 287, in wrapper
return func(*args, **kwargs)
File "/home/user/genai_workshop/.venv/lib/python3.10/site-packages/openai/resources/chat/completions/completions.py", line 1147, in create
return self._post(
File "/home/user/genai_workshop/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 1260, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File "/home/user/genai_workshop/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 1048, in request
raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}
Code snippets
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.chat.completions.create(
model="gpt-5-chat",
messages=[
{"role": "user", "content": input},
]
)
print(response.model_dump_json(indent=2))
OS
linux
Python version
Python 3.10.12
Library version
1.101.0
Hi Team: Are there any plan when this issue can be fixed?
Is there any update to this? We would really like this feature to unblock using the new API version.
We are also interested in using the V1 API Version as it is now GA on Azure OpenAI services.
This works for me:
import os
from openai import AzureOpenAI
model_name = "gpt-5-nano"
subscription_key = <KEY>
client = AzureOpenAI(
api_key="dummy",
base_url="https://<URL>/openai/v1",
default_headers={"api-key": subscription_key}
)
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": "I am going to Paris, what should I see?",
}
],
model=model_name
)
print(response.choices[0].message.content)