Skip to content

Commit 7275d84

Browse files
committed
fix: support azure new generation api verison
1 parent 4e28a42 commit 7275d84

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/openai/lib/azure.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def __init__(self) -> None:
5252
class BaseAzureClient(BaseClient[_HttpxClientT, _DefaultStreamT]):
5353
_azure_endpoint: httpx.URL | None
5454
_azure_deployment: str | None
55+
# https://learn.microsoft.com/en-us/azure/ai-foundry/openai/api-version-lifecycle?tabs=entra#next-generation-api-1
56+
_azure_next_supported_generation_api_version: list[str] = ["preview", "latest"]
5557

5658
@override
5759
def _build_request(
@@ -62,7 +64,7 @@ def _build_request(
6264
) -> httpx.Request:
6365
if options.url in _deployments_endpoints and is_mapping(options.json_data):
6466
model = options.json_data.get("model")
65-
if model is not None and "/deployments" not in str(self.base_url.path):
67+
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
6668
options.url = f"/deployments/{model}{options.url}"
6769

6870
return super()._build_request(options, retries_taken=retries_taken)

tests/lib/test_azure.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,25 @@ def test_client_sets_base_url(client: Client) -> None:
802802
)
803803
)
804804
assert req.url == "https://example-resource.azure.openai.com/openai/models?api-version=2024-02-01"
805+
806+
@pytest.mark.parametrize("api_version", ["preview", "latest"])
807+
def test_client_sets_base_url_with_new_generation_api_version(api_version: str) -> None:
808+
client = AzureOpenAI(
809+
api_version=api_version,
810+
api_key="example API key",
811+
base_url="https://example-resource.azure.openai.com/openai/v1/"
812+
)
813+
assert client.base_url == "https://example-resource.azure.openai.com/openai/v1/"
814+
815+
816+
req = client._build_request(
817+
FinalRequestOptions.construct(
818+
method="post",
819+
url="/chat/completions",
820+
json_data={"model": "placeholder"},
821+
)
822+
)
823+
assert (
824+
req.url
825+
== f"https://example-resource.azure.openai.com/openai/v1/chat/completions?api-version={api_version}"
826+
)

0 commit comments

Comments
 (0)