Skip to content

Add Azure AI MAI image generation support - #27061

Closed
emerzon wants to merge 3 commits into
BerriAI:mainfrom
emerzon:egomes/azure-mai-image-models
Closed

Add Azure AI MAI image generation support#27061
emerzon wants to merge 3 commits into
BerriAI:mainfrom
emerzon:egomes/azure-mai-image-models

Conversation

@emerzon

@emerzon emerzon commented May 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add Azure AI catalog entries for MAI-Image-2 and MAI-Image-2e with verified pricing, modalities, and support metadata
  • route Azure AI MAI image generation through /mai/v1/images/generations with MAI-specific request transformation for size / width / height
  • extend Azure AI image cost calculation for token-priced image models and add focused MAI metadata + transport tests

Testing

  • uv run black litellm/llms/azure_ai/image_generation/mai_transformation.py litellm/llms/azure_ai/image_generation/__init__.py litellm/images/main.py litellm/types/llms/openai.py tests/test_litellm/llms/azure_ai/image_generation/test_azure_ai_mai_image_generation_metadata.py
  • uv run pytest tests/test_litellm/llms/azure_ai/image_generation/test_azure_ai_mai_image_generation_metadata.py tests/test_litellm/images/test_image_generation_extra_headers.py -q
  • live litellm.image_generation() calls against both Azure AI MAI deployments (MAI-Image-2, MAI-Image-2e) via the Foundry MAI endpoint

Copilot AI review requested due to automatic review settings May 2, 2026 20:13
@codspeed-hq

codspeed-hq Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing emerzon:egomes/azure-mai-image-models (e4172c2) with main (934ecdc)

Open in CodSpeed

@codecov

codecov Bot commented May 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.90083% with 34 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ms/azure_ai/image_generation/mai_transformation.py 70.88% 23 Missing ⚠️
litellm/llms/azure_ai/image_generation/__init__.py 60.00% 8 Missing ⚠️
.../llms/azure_ai/image_generation/cost_calculator.py 86.36% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@greptile-apps

greptile-apps Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds Azure AI MAI image generation support (MAI-Image-2, MAI-Image-2e) by routing through /mai/v1/images/generations with a dedicated transformation class, a metadata-driven config selector driven by supports_mai_endpoint in the model cost JSON, and an extended cost calculator that prefers token-based usage over flat per-image pricing. Previous review comments around normalisation consistency, output_format hardcoding, and _parse_size error messages have all been addressed.

Confidence Score: 5/5

Safe to merge — no functional bugs found, all prior review concerns resolved.

No P0 or P1 findings. The routing logic, cost calculation, dimension validation, and header handling are all correct. The _supports_mai_endpoint two-step lookup (get_model_infomodel_cost.get) is necessary because ModelInfoBase TypedDict doesn't expose custom JSON fields like supports_mai_endpoint. Tests cover metadata, pricing math, config selection, request mapping, invalid input, and end-to-end HTTP routing without real network calls.

No files require special attention.

Important Files Changed

Filename Overview
litellm/llms/azure_ai/image_generation/mai_transformation.py New MAI transformation class with URL construction, request/response mapping, and dimension validation — clean implementation with good error handling
litellm/llms/azure_ai/image_generation/init.py Refactored to metadata-driven routing via _supports_mai_endpoint using get_model_info + model_cost lookup; normalization is now consistent via _normalize_model_name
litellm/llms/azure_ai/image_generation/cost_calculator.py Extended to support token-based pricing with usage-first calculation that falls back to per-image pricing; math is correct for both MAI models
litellm/images/main.py Added MAI routing branch that dispatches to llm_http_handler when config is AzureFoundryMAIImageGenerationConfig; both extra_headers and headers kwargs are correctly forwarded
tests/test_litellm/llms/azure_ai/image_generation/test_azure_ai_mai_image_generation_metadata.py New test file covering model metadata, cost calculation, config selection, request mapping, and HTTP routing — all mock-only, no real network calls

Reviews (2): Last reviewed commit: "Break Azure AI image cost import cycle" | Re-trigger Greptile

Comment thread litellm/llms/azure_ai/image_generation/__init__.py Outdated
Comment thread litellm/llms/azure_ai/image_generation/__init__.py
Comment thread litellm/llms/azure_ai/image_generation/mai_transformation.py Outdated
Comment thread litellm/llms/azure_ai/image_generation/mai_transformation.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Azure AI Foundry “MAI-Image-2” / “MAI-Image-2e” image generation support to LiteLLM, including catalog metadata, request routing to the MAI-specific endpoint, and token-based image cost calculation.

Changes:

  • Add MAI model entries (pricing + modalities + endpoints) to the model cost maps and validate via new metadata/cost tests.
  • Introduce an Azure Foundry MAI image-generation provider config that transforms sizewidth/height and targets /mai/v1/images/generations.
  • Extend Azure AI image cost calculation to prefer usage/token-based computation when usage metadata is present.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/test_litellm/llms/azure_ai/image_generation/test_azure_ai_mai_image_generation_metadata.py Adds tests for MAI metadata, cost calculation, request mapping, and routing behavior.
model_prices_and_context_window.json Registers azure_ai/MAI-Image-2 and azure_ai/MAI-Image-2e with token-based output pricing metadata.
litellm/model_prices_and_context_window_backup.json Mirrors the MAI entries for the packaged/local cost map used in tests.
litellm/llms/azure_ai/image_generation/mai_transformation.py New MAI request/response transformation + URL builder for the Foundry MAI endpoint.
litellm/llms/azure_ai/image_generation/cost_calculator.py Uses usage-based cost calculation when available, otherwise falls back to per-image pricing.
litellm/llms/azure_ai/image_generation/init.py Routes MAI model names to the new MAI image generation config.
litellm/images/main.py Routes Azure AI MAI image generation through llm_http_handler.image_generation_handler.
litellm/types/llms/openai.py Extends the image generation optional-param typing to include width/height.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread litellm/images/main.py
Comment thread litellm/llms/azure_ai/image_generation/cost_calculator.py Fixed
@emerzon

emerzon commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Closing due to partially superseed by #29688

@emerzon emerzon closed this Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants