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
9 changes: 7 additions & 2 deletions src/openai/lib/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ def _build_request(
*,
retries_taken: int = 0,
) -> httpx.Request:
if options.url in _deployments_endpoints and is_mapping(options.json_data):
model = options.json_data.get("model")
json_data = options.json_data
if options.url in _deployments_endpoints and is_mapping(json_data):
model = json_data.get("model")
if "model" in json_data:
options = model_copy(options)
options.json_data = {key: value for key, value in json_data.items() if key != "model"}

if model is not None and "/deployments" not in str(self.base_url.path):
options.url = f"/deployments/{model}{options.url}"

Expand Down
14 changes: 14 additions & 0 deletions tests/lib/test_azure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
import logging
from typing import Union, cast
from typing_extensions import Literal, Protocol
Expand Down Expand Up @@ -259,6 +260,18 @@ async def test_azure_bearer_token_redacted_async(
{"model": "deployment-body"},
"https://example-resource.azure.openai.com/openai/deployments/deployment-body/chat/completions?api-version=2024-02-01",
),
# AzureOpenAI: Image deployment names can differ from model names.
(
AzureOpenAI(
api_version="2024-02-01",
api_key="example API key",
azure_endpoint="https://example-resource.azure.openai.com",
),
"https://example-resource.azure.openai.com/openai/",
"/images/generations",
{"model": "gpt-image-1-5", "prompt": "A sunset over mountains"},
"https://example-resource.azure.openai.com/openai/deployments/gpt-image-1-5/images/generations?api-version=2024-02-01",
),
# AzureOpenAI: Deployment specified
(
AzureOpenAI(
Expand Down Expand Up @@ -386,6 +399,7 @@ def test_prepare_url_deployment_endpoint(
)
)
assert req.url == expected
assert "model" not in json.loads(req.content)
assert client.base_url == base_url


Expand Down