Repository navigation
feat(azure_ai): add MAI-Image-2.5 image generation support #29688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mateo-berri
merged 13 commits into
litellm_internal_staging
from
litellm_azure_mai_image_generation
Jun 9, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f2c1ff1
feat(azure_ai): add MAI-Image-2.5 image generation support
Sameerlite 6d8fd2b
fix(azure_ai): address MAI image generation review feedback
Sameerlite e30ad6b
feat(azure_ai): add MAI image edit and expand model cost map
Sameerlite 93c1ca7
fix(azure_ai): validate MAI edit size by consuming map iterator
Sameerlite c1316a4
fix(azure_ai): normalize MAI usage in generation response handler
Sameerlite 7c8ed6d
fix(azure_ai): narrow MAI edit size param type for mypy
Sameerlite e08d51f
Fix Azure MAI image response handling
cursoragent d9e4eaa
Fix MAI image generation base model routing
cursoragent 99a39c9
fix(azure_ai): preserve zero num_output_tokens in MAI usage normaliza…
mateo-berri 6266daa
fix(azure_ai): wrap MAI generation response JSON parsing in error han…
mateo-berri 2b6cb6d
Merge remote-tracking branch 'origin/litellm_internal_staging' into l…
mateo-berri 92a917f
fix(azure_ai): build MAI image edit URL correctly for /mai/ root bases
mateo-berri f108201
fix(azure_ai): build MAI image generation URL correctly for /mai/ roo…
mateo-berri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, cast | ||
|
|
||
| import httpx | ||
| from httpx._types import RequestFiles | ||
|
|
||
| from litellm.llms.azure_ai.common_utils import AzureFoundryModelInfo | ||
| from litellm.llms.azure_ai.image_generation.mai_transformation import ( | ||
| AzureFoundryMAIImageGenerationConfig, | ||
| ) | ||
| from litellm.llms.openai.common_utils import OpenAIError | ||
| from litellm.llms.openai.image_edit.transformation import OpenAIImageEditConfig | ||
| from litellm.secret_managers.main import get_secret_str | ||
| from litellm.types.images.main import ImageEditOptionalRequestParams | ||
| from litellm.types.llms.openai import FileTypes | ||
| from litellm.types.router import GenericLiteLLMParams | ||
| from litellm.types.utils import ImageResponse | ||
| from litellm.utils import convert_to_model_response_object | ||
|
|
||
| if TYPE_CHECKING: | ||
| from litellm.litellm_core_utils.logging import Logging as LiteLLMLoggingObj | ||
|
|
||
|
|
||
| class AzureFoundryMAIImageEditConfig(OpenAIImageEditConfig): | ||
| """Azure AI Foundry MAI image editing (e.g. MAI-Image-2.5).""" | ||
|
|
||
| DEFAULT_SIZE = "1024x1024" | ||
|
|
||
| def get_supported_openai_params(self, model: str) -> list: | ||
| return ["prompt", "image", "model", "n", "size"] | ||
|
|
||
| def map_openai_params( | ||
| self, | ||
| image_edit_optional_params: ImageEditOptionalRequestParams, | ||
| model: str, | ||
| drop_params: bool, | ||
| ) -> Dict: | ||
| optional_params: Dict[str, Any] = {} | ||
| supported_params = self.get_supported_openai_params(model) | ||
|
|
||
| for key, value in dict(image_edit_optional_params).items(): | ||
| if value is None or key in optional_params: | ||
| continue | ||
|
|
||
| if key in supported_params: | ||
| if key == "size" and value: | ||
| size_param = cast(str, value) | ||
| self._validate_size_param(size_param) | ||
| optional_params[key] = size_param | ||
| else: | ||
| optional_params[key] = value | ||
| elif not drop_params: | ||
| raise ValueError( | ||
| f"Parameter {key} is not supported for model {model}. " | ||
| f"Supported parameters are {supported_params}. " | ||
| f"Set drop_params=True to drop unsupported parameters." | ||
| ) | ||
|
|
||
| if "size" not in optional_params: | ||
| optional_params["size"] = self.DEFAULT_SIZE | ||
|
|
||
| return optional_params | ||
|
|
||
| def _validate_size_param(self, size: str) -> None: | ||
| known_sizes = { | ||
| "1024x1024", | ||
| "1792x1024", | ||
| "1024x1792", | ||
| "512x512", | ||
| "256x256", | ||
| } | ||
|
|
||
| if size in known_sizes: | ||
| return | ||
|
|
||
| if "x" in size: | ||
| try: | ||
| tuple(map(int, size.lower().split("x", 1))) | ||
| return | ||
| except ValueError: | ||
| raise ValueError( | ||
| f"Invalid size format: '{size}'. Expected format 'WIDTHxHEIGHT' (e.g., '1024x1024')." | ||
| ) | ||
|
Sameerlite marked this conversation as resolved.
Resolved
|
||
|
|
||
| raise ValueError( | ||
| f"Unsupported size value: '{size}'. " | ||
| f"Use a known size (e.g., '1024x1024') or a custom 'WIDTHxHEIGHT' string." | ||
| ) | ||
|
|
||
| def validate_environment( | ||
| self, | ||
| headers: dict, | ||
| model: str, | ||
| api_key: Optional[str] = None, | ||
| litellm_params: Optional[dict] = None, | ||
| api_base: Optional[str] = None, | ||
| ) -> dict: | ||
| api_key = AzureFoundryModelInfo.get_api_key(api_key) | ||
|
|
||
| if not api_key: | ||
| raise ValueError( | ||
| f"Azure AI API key is required for model {model}. " | ||
| "Set AZURE_AI_API_KEY environment variable or pass api_key parameter." | ||
| ) | ||
|
|
||
| headers.update({"api-key": api_key}) | ||
| return headers | ||
|
|
||
| def get_complete_url( | ||
| self, | ||
| model: str, | ||
| api_base: Optional[str], | ||
| litellm_params: dict, | ||
| ) -> str: | ||
| api_base = AzureFoundryModelInfo.get_api_base(api_base) | ||
|
|
||
| if api_base is None: | ||
| raise ValueError( | ||
| "Azure AI API base is required. Set AZURE_AI_API_BASE environment variable or pass api_base parameter." | ||
| ) | ||
|
|
||
| api_version = ( | ||
| litellm_params.get("api_version") | ||
| or get_secret_str("AZURE_AI_API_VERSION") | ||
| or "preview" | ||
| ) | ||
|
|
||
| return AzureFoundryMAIImageGenerationConfig.get_mai_image_edit_url( | ||
| api_base=api_base, | ||
| api_version=api_version, | ||
| ) | ||
|
|
||
| def transform_image_edit_request( | ||
| self, | ||
| model: str, | ||
| prompt: Optional[str], | ||
| image: Optional[FileTypes], | ||
| image_edit_optional_request_params: Dict, | ||
| litellm_params: GenericLiteLLMParams, | ||
| headers: dict, | ||
| ) -> Tuple[Dict, RequestFiles]: | ||
| request_params = { | ||
| "model": model, | ||
| **image_edit_optional_request_params, | ||
| } | ||
| if prompt is not None: | ||
| request_params["prompt"] = prompt | ||
|
|
||
| data_without_files = { | ||
| key: value | ||
| for key, value in request_params.items() | ||
| if key not in ["image", "mask"] | ||
| } | ||
| files_list: List[Tuple[str, Any]] = [] | ||
|
|
||
| if image is not None: | ||
| image_list = [image] if not isinstance(image, list) else image | ||
| for _image in image_list: | ||
| if _image is not None: | ||
| self._add_image_to_files( | ||
| files_list=files_list, | ||
| image=_image, | ||
| field_name="image", | ||
| ) | ||
| break | ||
|
|
||
| return data_without_files, files_list | ||
|
|
||
| def transform_image_edit_response( | ||
| self, | ||
| model: str, | ||
| raw_response: httpx.Response, | ||
| logging_obj: "LiteLLMLoggingObj", | ||
| ) -> ImageResponse: | ||
| try: | ||
| response = raw_response.json() | ||
| except Exception: | ||
| raise OpenAIError( | ||
| message=raw_response.text, status_code=raw_response.status_code | ||
| ) | ||
|
|
||
| if "usage" in response: | ||
| response["usage"] = ( | ||
| AzureFoundryMAIImageGenerationConfig.normalize_mai_image_usage( | ||
| response.get("usage") | ||
| ) | ||
| ) | ||
|
|
||
| logging_obj.post_call( | ||
| input="", | ||
| api_key="", | ||
| additional_args={"complete_input_dict": {}}, | ||
| original_response=response, | ||
| ) | ||
|
|
||
| return convert_to_model_response_object( | ||
| response_object=response, | ||
| model_response_object=ImageResponse(), | ||
| response_type="image_generation", | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.