Skip to content

Commit bf06378

Browse files
author
eycjur
committed
support flux image edit with azure ai
1 parent be193fb commit bf06378

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

litellm/llms/azure_ai/common_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def api_version(self, api_version: Optional[str] = None) -> Optional[str]:
2929
api_version = (
3030
api_version
3131
or litellm.api_version
32-
or get_secret_str("AZURE_API_VERSION")
32+
or get_secret_str("AZURE_AI_API_VERSION")
3333
)
3434
return api_version
3535

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfig
2+
3+
from .transformation import AzureFoundryFluxImageEditConfig
4+
5+
__all__ = ["AzureFoundryFluxImageEditConfig"]
6+
7+
8+
def get_azure_ai_image_edit_config(model: str) -> BaseImageEditConfig:
9+
model = model.lower()
10+
model = model.replace("-", "")
11+
model = model.replace("_", "")
12+
if model == "" or "flux" in model: # empty model is flux
13+
return AzureFoundryFluxImageEditConfig()
14+
else:
15+
raise ValueError(f"Model {model} is not supported for Azure AI image editing.")
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from typing import Optional
2+
3+
import httpx
4+
5+
import litellm
6+
from litellm.llms.azure_ai.common_utils import AzureFoundryModelInfo
7+
from litellm.llms.openai.image_edit.transformation import OpenAIImageEditConfig
8+
from litellm.secret_managers.main import get_secret_str
9+
from litellm.utils import _add_path_to_api_base
10+
11+
12+
class AzureFoundryFluxImageEditConfig(OpenAIImageEditConfig):
13+
"""
14+
Azure AI Foundry FLUX image edit config
15+
16+
Supports FLUX models including FLUX-1.1-pro and FLUX-1-kontext-pro for image editing.
17+
18+
Azure AI Foundry FLUX models handle image editing through the /images/edits endpoint,
19+
same as standard Azure OpenAI models. The request format uses multipart/form-data
20+
with image files and prompt.
21+
"""
22+
23+
def validate_environment(
24+
self,
25+
headers: dict,
26+
model: str,
27+
api_key: Optional[str] = None,
28+
) -> dict:
29+
"""
30+
Validate Azure AI Foundry environment and set up authentication
31+
Uses Api-Key header format like Azure OpenAI
32+
33+
Azure AI Foundry uses the same authentication format as Azure OpenAI:
34+
- Header: Api-Key (not Authorization Bearer)
35+
- Endpoint: /openai/deployments/{model}/images/edits (for image editing)
36+
"""
37+
api_key = AzureFoundryModelInfo.get_api_key(api_key)
38+
39+
if not api_key:
40+
raise ValueError(
41+
f"Azure AI API key is required for model {model}. Set AZURE_AI_API_KEY environment variable or pass api_key parameter."
42+
)
43+
44+
headers.update(
45+
{
46+
"Api-Key": api_key, # Azure AI Foundry uses Api-Key header format
47+
}
48+
)
49+
return headers
50+
51+
def get_complete_url(
52+
self,
53+
model: str,
54+
api_base: Optional[str],
55+
litellm_params: dict,
56+
) -> str:
57+
"""
58+
Constructs a complete URL for Azure AI Foundry image edits API request.
59+
60+
Azure AI Foundry FLUX models handle image editing through the /images/edits
61+
endpoint.
62+
63+
Args:
64+
- model: Model name (deployment name for Azure AI Foundry)
65+
- api_base: Base URL for Azure AI endpoint
66+
- litellm_params: Additional parameters including api_version
67+
68+
Returns:
69+
- Complete URL for the image edits endpoint
70+
"""
71+
api_base = AzureFoundryModelInfo.get_api_base(api_base)
72+
73+
if api_base is None:
74+
raise ValueError(
75+
"Azure AI API base is required. Set AZURE_AI_API_BASE environment variable or pass api_base parameter."
76+
)
77+
78+
api_version = (litellm_params.get("api_version") or litellm.api_version
79+
or get_secret_str("AZURE_AI_API_VERSION")
80+
)
81+
if api_version is None:
82+
# API version is mandatory for Azure AI Foundry
83+
raise ValueError(
84+
"Azure API version is required. Set AZURE_AI_API_VERSION environment variable or pass api_version parameter."
85+
)
86+
87+
# Add the path to the base URL using the model as deployment name
88+
# Azure AI Foundry FLUX models use /images/edits for editing
89+
if "/openai/deployments/" in api_base:
90+
new_url = _add_path_to_api_base(
91+
api_base=api_base,
92+
ending_path="/images/edits",
93+
)
94+
else:
95+
new_url = _add_path_to_api_base(
96+
api_base=api_base,
97+
ending_path=f"/openai/deployments/{model}/images/edits",
98+
)
99+
100+
# Use the new query_params dictionary
101+
final_url = httpx.URL(new_url).copy_with(params={"api-version": api_version})
102+
103+
return str(final_url)

litellm/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7501,6 +7501,12 @@ def get_provider_image_edit_config(
75017501
)
75027502

75037503
return RecraftImageEditConfig()
7504+
elif LlmProviders.AZURE_AI == provider:
7505+
from litellm.llms.azure_ai.image_edit import (
7506+
get_azure_ai_image_edit_config,
7507+
)
7508+
7509+
return get_azure_ai_image_edit_config(model)
75047510
elif LlmProviders.LITELLM_PROXY == provider:
75057511
from litellm.llms.litellm_proxy.image_edit.transformation import (
75067512
LiteLLMProxyImageEditConfig,

0 commit comments

Comments
 (0)