Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/openai/lib/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)