Skip to content

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]

fix(azure): honor base_url client param and drop stale api-version on v1 image routes

Devin AI1cf683c98062
diff --git a/litellm/llms/azure/azure.py b/litellm/llms/azure/azure.py
index ce65d62ca45..c2d4d8b9306 100644
--- a/litellm/llms/azure/azure.py
+++ b/litellm/llms/azure/azure.py
@@ -1091,9 +1091,10 @@ class AzureChatCompletion(BaseAzureLLM, BaseLLM):
AzureFoundryMAIImageGenerationConfig,
)
- api_base: str = azure_client_params.get("azure_endpoint", "") # "https://example-endpoint.openai.azure.com"
- if api_base.endswith("/"):
- api_base = api_base.rstrip("/")
+ # deployment-scoped endpoints are moved to "base_url" by select_azure_base_url_or_endpoint
+ api_base: str = (azure_client_params.get("azure_endpoint") or azure_client_params.get("base_url") or "").rstrip(
+ "/"
+ )
api_version: Final[str] = azure_client_params.get("api_version", "")
if model is None:
model = ""
diff --git a/litellm/llms/azure/common_utils.py b/litellm/llms/azure/common_utils.py
index ae9a43a540c..8d5c7687ec8 100644
--- a/litellm/llms/azure/common_utils.py
+++ b/litellm/llms/azure/common_utils.py
@@ -794,7 +794,8 @@ class BaseAzureLLM(BaseOpenAILLM):
def get_azure_v1_image_url(api_base: str, api_version: str | None, route: str) -> str | None:
"""
Azure's v1 surface serves images at ``/openai/v1/images/{generations,edits}`` and routes by
- ``model`` in the request body, so any deployment path in ``api_base`` has to be dropped.
+ ``model`` in the request body, so any deployment path and stale ``api-version`` in
+ ``api_base`` have to be dropped.
Returns None when ``api_version`` is a dated one, which still uses the deployment route.
"""
@@ -803,8 +804,11 @@ class BaseAzureLLM(BaseOpenAILLM):
base_url: Final = httpx.URL(api_base)
openai_path_start: Final = base_url.path.find("/openai")
- resource_base: Final = (
- api_base if openai_path_start == -1 else str(base_url.copy_with(path=base_url.path[:openai_path_start]))
+ resource_base: Final = str(
+ base_url.copy_with(
+ path=base_url.path if openai_path_start == -1 else base_url.path[:openai_path_start],
+ params=httpx.QueryParams({k: v for k, v in base_url.params.items() if k != "api-version"}),
+ )
)
return BaseAzureLLM._get_base_azure_url(
api_base=resource_base,
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.py
index 48fa7221dff..5c7b249ae72 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
@@ -286,7 +286,7 @@ def test_v1_api_version_replaces_deployment_scoped_api_base(monkeypatch):
url = AzureImageEditConfig().get_complete_url(
model=_FALLBACK_MODEL,
- api_base=f"{_FALLBACK_API_BASE}/openai/deployments/{_FALLBACK_MODEL}/images/edits",
+ api_base=f"{_FALLBACK_API_BASE}/openai/deployments/{_FALLBACK_MODEL}/images/edits?api-version=2024-10-21",
litellm_params={"api_version": "preview"},
)
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.py
index cfec4eea2f4..65beec2b707 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
@@ -477,3 +477,15 @@ def test_azure_image_generation_v1_api_version_replaces_deployment_scoped_api_ba
base_model=None,
)
assert url == "https://my-resource.openai.azure.com/openai/v1/images/generations?api-version=preview"
+
+
+def test_azure_image_generation_v1_api_version_uses_base_url_client_param():
+ url = AzureChatCompletion().create_azure_base_url(
+ azure_client_params={
+ "base_url": "https://my-resource.openai.azure.com/openai/deployments/gpt-image-1?api-version=2024-10-21",
+ "api_version": "preview",
+ },
+ model="gpt-image-1",
+ base_model=None,
+ )
+ assert url == "https://my-resource.openai.azure.com/openai/v1/images/generations?api-version=preview"