diff --git a/src/openai/lib/azure.py b/src/openai/lib/azure.py index a994e4256c..99b54c00e1 100644 --- a/src/openai/lib/azure.py +++ b/src/openai/lib/azure.py @@ -52,6 +52,8 @@ def __init__(self) -> None: class BaseAzureClient(BaseClient[_HttpxClientT, _DefaultStreamT]): _azure_endpoint: httpx.URL | None _azure_deployment: str | None + # https://learn.microsoft.com/en-us/azure/ai-foundry/openai/api-version-lifecycle?tabs=entra#next-generation-api-1 + _azure_next_supported_generation_api_version: list[str] = ["preview", "latest"] @override def _build_request( @@ -62,7 +64,7 @@ def _build_request( ) -> 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): + if model is not None and self._api_version not in self._azure_next_supported_generation_api_version and "/deployments" not in str(self.base_url.path): # pylint: disable=no-member options.url = f"/deployments/{model}{options.url}" return super()._build_request(options, retries_taken=retries_taken) diff --git a/tests/lib/test_azure.py b/tests/lib/test_azure.py index 52c24eba27..d79caf5915 100644 --- a/tests/lib/test_azure.py +++ b/tests/lib/test_azure.py @@ -802,3 +802,25 @@ def test_client_sets_base_url(client: Client) -> None: ) ) assert req.url == "https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01" + +@pytest.mark.parametrize("api_version", ["preview", "latest"]) +def test_client_sets_base_url_with_new_generation_api_version(api_version: str) -> None: + client = AzureOpenAI( + api_version=api_version, + api_key="example API key", + base_url="https://example-resource.azure.openai.com/openai/v1/" + ) + assert client.base_url == "https://example-resource.azure.openai.com/openai/v1/" + + + req = client._build_request( + FinalRequestOptions.construct( + method="post", + url="/chat/completions", + json_data={"model": "placeholder"}, + ) + ) + assert ( + req.url + == f"https://example-resource.azure.openai.com/openai/v1/chat/completions?api-version={api_version}" + )