From ca25712f9cd7023ef9c750d2d21cb5013db84312 Mon Sep 17 00:00:00 2001 From: Junhyuk Lee Date: Fri, 1 May 2026 07:43:01 +0000 Subject: [PATCH] Advance OSS contribution for Azure OpenAI - `model` parameter in request body causes failures when deployment name differs from model name Nightly Codex produced a focused contribution for https://github.com/openai/openai-python/issues/2892. Constraint: Automated nightly run; keep changes small and reviewable. Confidence: medium Scope-risk: narrow Tested: See uploaded nightly artifacts and workflow logs. Not-tested: Maintainer CI beyond this workflow. --- src/openai/lib/azure.py | 9 +++++++-- tests/lib/test_azure.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/openai/lib/azure.py b/src/openai/lib/azure.py index 09fdd9507e..55c216b138 100644 --- a/src/openai/lib/azure.py +++ b/src/openai/lib/azure.py @@ -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}" diff --git a/tests/lib/test_azure.py b/tests/lib/test_azure.py index 52c24eba27..5bbd149faf 100644 --- a/tests/lib/test_azure.py +++ b/tests/lib/test_azure.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json import logging from typing import Union, cast from typing_extensions import Literal, Protocol @@ -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( @@ -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