Skip to content

Commit 8622ec9

Browse files
committed
fix(review): address PR feedback - type narrowing, full URL assertion, parametrized test
- Replace type: ignore[union-attr] with proper cast() type narrowing - Assert full URL instead of substring match in deployment test - Add parametrized test for /chat/completions endpoint - Move import json to module scope for consistency Refs: openai#2910
1 parent aa2310d commit 8622ec9

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

src/openai/lib/azure.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ def _build_request(
6161
retries_taken: int = 0,
6262
) -> httpx.Request:
6363
if options.url in _deployments_endpoints and is_mapping(options.json_data):
64-
model = options.json_data.get("model")
64+
json_data = cast(Mapping[str, Any], options.json_data)
65+
model = json_data.get("model")
6566
if model is not None and "/deployments" not in str(self.base_url.path):
6667
options.url = f"/deployments/{model}{options.url}"
67-
options.json_data = {k: v for k, v in options.json_data.items() if k != "model"} # type: ignore[union-attr]
68+
options.json_data = {k: v for k, v in json_data.items() if k != "model"}
6869

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

tests/lib/test_azure.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import json
34
import logging
45
from typing import Union, cast
56
from typing_extensions import Literal, Protocol
@@ -56,12 +57,32 @@ def test_implicit_deployment_strips_model_from_body(client: Client) -> None:
5657
json_data={"model": "gpt-image-1-5", "prompt": "sunset"},
5758
)
5859
)
59-
import json
6060

6161
body = json.loads(req.content.decode())
6262
assert "model" not in body
6363
assert body["prompt"] == "sunset"
64-
assert "/deployments/gpt-image-1-5/" in str(req.url)
64+
assert (
65+
str(req.url)
66+
== "https://example-resource.azure.openai.com/openai/deployments/gpt-image-1-5/images/generations?api-version=2023-07-01"
67+
)
68+
69+
70+
@pytest.mark.parametrize("client", [sync_client, async_client])
71+
def test_implicit_deployment_strips_model_from_body_chat(client: Client) -> None:
72+
req = client._build_request(
73+
FinalRequestOptions.construct(
74+
method="post",
75+
url="/chat/completions",
76+
json_data={"model": "gpt-4o", "messages": [{"role": "user", "content": "hi"}]},
77+
)
78+
)
79+
body = json.loads(req.content.decode())
80+
assert "model" not in body
81+
assert body["messages"] == [{"role": "user", "content": "hi"}]
82+
assert (
83+
str(req.url)
84+
== "https://example-resource.azure.openai.com/openai/deployments/gpt-4o/chat/completions?api-version=2023-07-01"
85+
)
6586

6687

6788
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)