Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: support azure new generation api version #2585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hsuyuming wants to merge 1 commit into openai:main
base: main
Choose a base branch
Loading
from hsuyuming:fix/support-azure-new-preview-api-version
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
View file Open in desktop
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
View file Open in desktop
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}"
)

AltStyle によって変換されたページ (->オリジナル) /