The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(azure): route image generation and edits through /openai/v1 for v1 api versions
Devin AI80fd9970c26e
diff --git a/litellm/llms/azure/azure.py b/litellm/llms/azure/azure.pyindex 980b27cda55..3a30444f06d 100644--- a/litellm/llms/azure/azure.py+++ b/litellm/llms/azure/azure.py@@ -1113,6 +1113,13 @@ class AzureChatCompletion(BaseAzureLLM, BaseLLM):api_version=api_version,)+ if BaseAzureLLM._is_azure_v1_api_version(api_version):+ return BaseAzureLLM._get_base_azure_url(+ api_base=api_base,+ litellm_params={"api_version": api_version},+ route="/openai/images/generations",+ )+if "/openai/deployments/" in api_base:base_url_with_deployment = api_baseelse:diff --git a/litellm/llms/azure/image_edit/transformation.py b/litellm/llms/azure/image_edit/transformation.pyindex 15592968bad..eb1a51ca3e8 100644--- a/litellm/llms/azure/image_edit/transformation.py+++ b/litellm/llms/azure/image_edit/transformation.py@@ -93,8 +93,6 @@ class AzureImageEditConfig(OpenAIImageEditConfig):raise ValueError(f"api_base is required for Azure AI Studio. Please set the api_base parameter. Passed `api_base={api_base}`")- original_url: Final = httpx.URL(api_base)-# Resolve api_version: litellm_params > litellm.api_version > AZURE_API_VERSION env > default.# Mirrors the fallback chain used by the Azure chat path in common_utils.py,# so callers that set a global / env api_version don't get an unversioned URL.@@ -105,6 +103,15 @@ class AzureImageEditConfig(OpenAIImageEditConfig):or litellm.AZURE_DEFAULT_API_VERSION)+ if BaseAzureLLM._is_azure_v1_api_version(api_version):+ return BaseAzureLLM._get_base_azure_url(+ api_base=api_base,+ litellm_params={"api_version": api_version},+ route="/openai/images/edits",+ )++ original_url: Final = httpx.URL(api_base)+# Create a new dictionary with existing paramsquery_params: Final = dict(original_url.params)diff --git a/tests/test_litellm/llms/azure/image_edit/test_azure_image_edit_transformation.py b/tests/test_litellm/llms/azure/image_edit/test_azure_image_edit_transformation.pyindex 59472d1a49d..251b98aa8d4 100644--- a/tests/test_litellm/llms/azure/image_edit/test_azure_image_edit_transformation.py+++ b/tests/test_litellm/llms/azure/image_edit/test_azure_image_edit_transformation.py@@ -233,3 +233,56 @@ def test_api_version_in_api_base_query_is_preserved(monkeypatch):)assert _query_params(url) == {"api-version": "2024-05-01-preview"}+++# ---------------------------------------------------------------------------+# Azure v1 API surface (api_version in {"v1", "preview", "latest"})+#+# The v1 surface exposes /openai/v1/images/edits and routes by ``model`` in the+# multipart form. Building the deployment-scoped path instead makes Azure 404.+# ---------------------------------------------------------------------------+++def test_v1_api_version_uses_v1_route_and_keeps_model(monkeypatch):+ monkeypatch.setattr(litellm, "api_version", None, raising=False)+ monkeypatch.delenv("AZURE_API_VERSION", raising=False)+ config = AzureImageEditConfig()++ for api_version in ("v1", "preview", "latest"):+ url = config.get_complete_url(+ model=_FALLBACK_MODEL,+ api_base=_FALLBACK_API_BASE,+ litellm_params={"api_version": api_version},+ )+ assert urllib.parse.urlparse(url).path == "/openai/v1/images/edits"+ assert _query_params(url) == {"api-version": api_version}+ assert config.finalize_image_edit_request_data({"model": _FALLBACK_MODEL, "prompt": "x"}, url) == {+ "model": _FALLBACK_MODEL,+ "prompt": "x",+ }+++def test_v1_api_version_from_global_uses_v1_route(monkeypatch):+ monkeypatch.setattr(litellm, "api_version", "preview", raising=False)+ monkeypatch.delenv("AZURE_API_VERSION", raising=False)++ url = AzureImageEditConfig().get_complete_url(+ model=_FALLBACK_MODEL,+ api_base=_FALLBACK_API_BASE,+ litellm_params={},+ )++ assert urllib.parse.urlparse(url).path == "/openai/v1/images/edits"+++def test_dated_api_version_still_uses_deployment_route(monkeypatch):+ monkeypatch.setattr(litellm, "api_version", None, raising=False)+ monkeypatch.delenv("AZURE_API_VERSION", raising=False)++ url = AzureImageEditConfig().get_complete_url(+ model=_FALLBACK_MODEL,+ api_base=_FALLBACK_API_BASE,+ litellm_params={"api_version": "2024-10-21"},+ )++ assert urllib.parse.urlparse(url).path == f"/openai/deployments/{_FALLBACK_MODEL}/images/edits"diff --git a/tests/test_litellm/llms/azure/image_generation/test_azure_image_generation_init.py b/tests/test_litellm/llms/azure/image_generation/test_azure_image_generation_init.pyindex 560fee17328..ebb6c672f96 100644--- a/tests/test_litellm/llms/azure/image_generation/test_azure_image_generation_init.py+++ b/tests/test_litellm/llms/azure/image_generation/test_azure_image_generation_init.py@@ -433,3 +433,35 @@ async def test_azure_aimage_generation_base_model_vs_deployment_name():wire_json = post_kwargs.get("json") or {}assert "model" not in wire_jsonassert data.get("model") == base_model++[email protected]("api_version", ["v1", "preview", "latest"])+def test_azure_image_generation_v1_api_version_uses_v1_route(api_version):+ """The v1 Azure surface exposes /openai/v1/images/generations and routes by body ``model``."""+ url = AzureChatCompletion().create_azure_base_url(+ azure_client_params={+ "azure_endpoint": "https://my-resource.openai.azure.com",+ "api_version": api_version,+ },+ model="gpt-image-1",+ base_model=None,+ )+ assert url == f"https://my-resource.openai.azure.com/openai/v1/images/generations?api-version={api_version}"+ data = {"model": "gpt-image-1", "prompt": "x"}+ assert azure_deployment_image_generation_json_body(url, data) == data+++def test_azure_image_generation_dated_api_version_uses_deployment_route():+ url = AzureChatCompletion().create_azure_base_url(+ azure_client_params={+ "azure_endpoint": "https://my-resource.openai.azure.com",+ "api_version": "2024-10-21",+ },+ model="gpt-image-1",+ base_model=None,+ )+ assert (+ url+ == "https://my-resource.openai.azure.com/openai/deployments/gpt-image-1/images/generations?api-version=2024-10-21"+ )+ assert "model" not in azure_deployment_image_generation_json_body(url, {"model": "gpt-image-1", "prompt": "x"})