Skip to content

AzureOpenAi is unable to process chat.completions when the API version is set to preview. #2584

@hsuyuming

Description

@hsuyuming

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • 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

  1. Create virtual env
python -m venv .venv
source .venv/bin/activate
pip install openai==1.101.0
pip install azure-identity
  1. 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)) 
  1. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions