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 @@ -61,9 +61,11 @@ 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 = cast(Mapping[str, Any], options.json_data)
model = 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}"
options.json_data = {k: v for k, v in json_data.items() if k != "model"}

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

Expand Down
33 changes: 33 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 @@ -47,6 +48,38 @@ def test_implicit_deployment_path(client: Client) -> None:
)


@pytest.mark.parametrize("client", [sync_client, async_client])
@pytest.mark.parametrize(
"endpoint,model",
[
("/chat/completions", "gpt-4o"),
("/completions", "gpt-4o"),
("/embeddings", "text-embedding-ada-002"),
("/images/generations", "gpt-image-1-5"),
("/images/edits", "gpt-image-1-5"),
("/audio/transcriptions", "whisper-1"),
("/audio/translations", "whisper-1"),
("/audio/speech", "tts-1"),
],
)
def test_implicit_deployment_strips_model_from_body(client: Client, endpoint: str, model: str) -> None:
req = client._build_request(
FinalRequestOptions.construct(
method="post",
url=endpoint,
json_data={"model": model, "extra": "value"},
)
)

body = json.loads(req.content.decode())
assert "model" not in body
assert body["extra"] == "value"
assert (
str(req.url)
== f"https://example-resource.azure.openai.com/openai/deployments/{model}{endpoint}?api-version=2023-07-01"
)


@pytest.mark.parametrize(
"client,method",
[
Expand Down