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]

[Bug]: Azure OpenAI & AI Foundry Reject Image Generation Payload Due to extra_body Injection in LiteLLM v1.76.3 (#14475)

Ishaan Jaff51d5255452f7
diff --git a/litellm/utils.py b/litellm/utils.py
index 84d86bea647..0d2fe5d4d64 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -2501,6 +2501,23 @@ def get_optional_params_transcription(
return optional_params
+def _map_openai_size_to_vertex_ai_aspect_ratio(size: Optional[str]) -> str:
+ """Map OpenAI size parameter to Vertex AI aspectRatio."""
+ if size is None:
+ return "1:1"
+
+ # Map OpenAI size strings to Vertex AI aspect ratio strings
+ # Vertex AI accepts: "1:1", "9:16", "16:9", "4:3", "3:4"
+ size_to_aspect_ratio = {
+ "256x256": "1:1", # Square
+ "512x512": "1:1", # Square
+ "1024x1024": "1:1", # Square (default)
+ "1792x1024": "16:9", # Landscape
+ "1024x1792": "9:16", # Portrait
+ }
+ return size_to_aspect_ratio.get(size, "1:1") # Default to square if size not recognized
+
+
def get_optional_params_image_gen(
model: Optional[str] = None,
n: Optional[int] = None,
@@ -2614,19 +2631,7 @@ def get_optional_params_image_gen(
# Map OpenAI size parameter to Vertex AI aspectRatio
if size is not None:
- # Map OpenAI size strings to Vertex AI aspect ratio strings
- # Vertex AI accepts: "1:1", "9:16", "16:9", "4:3", "3:4"
- size_to_aspect_ratio = {
- "256x256": "1:1", # Square
- "512x512": "1:1", # Square
- "1024x1024": "1:1", # Square (default)
- "1792x1024": "16:9", # Landscape
- "1024x1792": "9:16", # Portrait
- }
- aspect_ratio = size_to_aspect_ratio.get(
- size, "1:1"
- ) # Default to square if size not recognized
- optional_params["aspectRatio"] = aspect_ratio
+ optional_params["aspectRatio"] = _map_openai_size_to_vertex_ai_aspect_ratio(size)
openai_params: list[str] = list(default_params.keys())
if provider_config is not None:
@@ -2642,6 +2647,12 @@ def get_optional_params_image_gen(
openai_params=openai_params,
additional_drop_params=additional_drop_params,
)
+ # remove keys with None or empty dict/list values to avoid sending empty payloads
+ optional_params = {
+ k: v
+ for k, v in optional_params.items()
+ if v is not None and (not isinstance(v, (dict, list)) or len(v) > 0)
+ }
return optional_params
diff --git a/tests/image_gen_tests/request_payloads/azure_gpt_image_1.json b/tests/image_gen_tests/request_payloads/azure_gpt_image_1.json
new file mode 100644
index 00000000000..1ad57bc0d4c
--- /dev/null
+++ b/tests/image_gen_tests/request_payloads/azure_gpt_image_1.json
@@ -0,0 +1,4 @@
+{
+ "model": "gpt-image-1",
+ "prompt": "test prompt"
+}
diff --git a/tests/image_gen_tests/test_image_generation.py b/tests/image_gen_tests/test_image_generation.py
index 61843d3151e..c3900342954 100644
--- a/tests/image_gen_tests/test_image_generation.py
+++ b/tests/image_gen_tests/test_image_generation.py
@@ -5,6 +5,7 @@ import logging
import os
import sys
import traceback
+from unittest.mock import AsyncMock, patch
sys.path.insert(
@@ -329,3 +330,33 @@ async def test_aiml_image_generation_with_dynamic_api_key():
assert captured_json_data is not None
assert captured_json_data["prompt"] == "A cute baby sea otter"
assert captured_json_data["model"] == "flux-pro/v1.1"
+
+async def test_azure_image_generation_request_body():
+ from litellm import aimage_generation
+ test_dir = os.path.dirname(__file__)
+ expected_path = os.path.join(
+ test_dir, "request_payloads", "azure_gpt_image_1.json"
+ )
+ with open(expected_path, "r") as f:
+ expected_body = json.load(f)
+
+ with patch(
+ "litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
+ new_callable=AsyncMock,
+ ) as mock_post:
+ mock_post.side_effect = Exception("test")
+
+ with pytest.raises(Exception):
+ await aimage_generation(
+ model="azure/gpt-image-1",
+ prompt="test prompt",
+ api_base="https://example.azure.com",
+ api_key="test-key",
+ api_version="2025-04-01-preview",
+ )
+
+ mock_post.assert_called_once()
+ call_args = mock_post.call_args
+ request_json = call_args.kwargs.get("json", {})
+ assert request_json == expected_body
diff --git a/tests/test_litellm/test_utils.py b/tests/test_litellm/test_utils.py
index 2bf2935901e..f9f150b6234 100644
--- a/tests/test_litellm/test_utils.py
+++ b/tests/test_litellm/test_utils.py
@@ -84,6 +84,15 @@ def test_get_optional_params_image_gen_vertex_ai_size():
assert optional_params["sampleCount"] == 1
+def test_get_optional_params_image_gen_filters_empty_values():
+ optional_params = get_optional_params_image_gen(
+ model="gpt-image-1",
+ custom_llm_provider="openai",
+ extra_body={},
+ )
+ assert optional_params == {}
+
+
def test_all_model_configs():
from litellm.llms.vertex_ai.vertex_ai_partner_models.ai21.transformation import (
VertexAIAi21Config,