Skip to content

feat(aiml): add openai/gpt-image-2 image model - #31323

Merged
5 commits merged into
litellm_internal_stagingfrom
litellm_aiml-gpt-image-2-5b10
Jun 25, 2026
Merged

5 commits merged into
litellm_internal_stagingfrom
litellm_aiml-gpt-image-2-5b10

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

Linear ticket

Resolves LIT-3777

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all unit tests on make test-unit (for the new + adjacent AIML image-gen suites)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Type

New Feature

Changes

Adds the aiml/openai/gpt-image-2 model and fixes the AI/ML API image generation transformer so OpenAI-style image models go through the correct upstream schema.

Two things were needed to make aiml/openai/gpt-image-2 work end to end:

  1. A cost-map entry (aiml/openai/gpt-image-2) with mode=image_generation so get_model_info resolves the model under the AI/ML provider and the existing aiml_image_cost_calculator finds an output_cost_per_image. The flat per-image rate of $0.054 matches the price AI/ML quotes in the public docs example for the default medium-quality 1536x1024 generation (https://docs.aimlapi.com/api-references/image-models/openai/gpt-image-2)

  2. A model-aware request transformation in AimlImageGenerationConfig. The existing transformer was written for the flux/Imagen request schema and unconditionally remaps n -> num_images, size -> image_size{width, height}, and (incorrectly) response_format -> output_format. The openai/gpt-image-2 endpoint on api.aimlapi.com instead expects the OpenAI shape (n, size: "1024x1024", separate response_format for url/b64_json and output_format for png/jpeg/webp). Models whose name starts with openai/ now pass their OpenAI-shaped params through unchanged; everything else keeps the existing flux-style remapping

Tests:

  • tests/test_litellm/llms/aiml/image_generation/test_aiml_image_generation_transformation.py covers both branches of the new routing (param surface, request body, drop-params behavior) and locks in the $0.054 per-image cost for aiml/openai/gpt-image-2
  • tests/image_gen_tests/test_image_generation.py::test_aiml_openai_gpt_image_2_request_uses_openai_param_shape is an end-to-end check through litellm.aimage_generation that confirms the upstream POST body keeps size/n/response_format (and never grows image_size/num_images/output_format) and that the model identifier sent to AI/ML is openai/gpt-image-2

Screenshots / Proof of Fix

To reproduce against a live proxy:

export AIML_API_KEY=...
export LITELLM_LOCAL_MODEL_COST_MAP=True
python litellm/proxy/proxy_cli.py --config litellm/proxy/dev_config.yaml --detailed_debug --reload --use_v2_migration_resolver 2>&1 | tee litellm.log

LITELLM_LOCAL_MODEL_COST_MAP=True is required until this PR merges. By default litellm pulls the cost map from main on GitHub, which does not carry the aiml/openai/gpt-image-2 entry yet, so the spend logs as $0.00; forcing the bundled local map lets the new $0.054 rate resolve

curl -sS http://localhost:4000/v1/images/generations \
  -H 'Content-Type: application/json' \
  -H "Authorization: Bearer $LITELLM_MASTER_KEY" \
  -d '{
    "model": "aiml/openai/gpt-image-2",
    "prompt": "A T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses.",
    "size": "1536x1024",
    "quality": "medium",
    "n": 1
  }'

This returns a data[0].url pointing at cdn.aimlapi.com/..., the upstream POST body to api.aimlapi.com stays OpenAI-shaped ({"model": "openai/gpt-image-2", "n": 1, "quality": "medium", "size": "1536x1024"}, never growing num_images/image_size/output_format), and litellm.log records a spend entry of $0.054

aiml/openai/gpt-image-2 sample output

Linear Issue: LIT-3777

Open in Web Open in Cursor 

Adds aiml/openai/gpt-image-2 to the cost map and teaches AimlImageGenerationConfig
to route OpenAI-style image models through the upstream OpenAI request schema
instead of the AI/ML flux schema. Without this, size, n, and response_format would
be remapped to image_size/num_images/output_format, which the gpt-image-2 endpoint
on api.aimlapi.com does not accept.

Co-authored-by: Mateo Wang <mateo-berri@users.noreply.github.com>
@CLAassistant

CLAassistant commented Jun 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ mateo-berri
❌ cursoragent
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov

codecov Bot commented Jun 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.65517% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...tellm/llms/aiml/image_generation/transformation.py 89.65% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@mateo-berri
mateo-berri marked this pull request as ready for review June 25, 2026 20:41
@greptile-apps

greptile-apps Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds aiml/openai/gpt-image-2 to the AI/ML provider by introducing a model-aware request transformer that routes openai/* models through an OpenAI-schema pass-through while keeping the existing flux-style remapping for all other AI/ML image models.

  • Transformation layer (transformation.py): New _is_openai_style_model prefix check branches serialization: OpenAI-style models pass n, size, response_format, etc. through verbatim; flux-style models continue to remap to num_images, image_size, output_format. The refactored map_openai_params logic is functionally identical to the original for all existing models.
  • Cost map (model_prices_and_context_window.json + backup): Adds aiml/openai/gpt-image-2 with mode: image_generation and output_cost_per_image: 0.054; a notes field documents that this is the medium-quality flat rate.
  • Tests: Pure unit tests in tests/test_litellm/ cover both routing branches, drop-params behavior, and cost lookup; an end-to-end mock test in tests/image_gen_tests/ confirms the upstream request body shape end-to-end.

Confidence Score: 5/5

Safe to merge — the change is isolated to a single provider's transformation layer and adds a new model entry without modifying any shared infrastructure.

The transformation refactor is backward-compatible: the flux-style remapping path is functionally identical to what it replaced, and the new OpenAI-style pass-through path is covered by both unit and integration-level mock tests. The cost map entries are consistent with the rest of the aiml image catalog and both JSON files are kept in sync. No shared or critical-path code is touched.

No files require special attention.

Important Files Changed

Filename Overview
litellm/llms/aiml/image_generation/transformation.py Adds OpenAI-style model branch to AimlImageGenerationConfig: new _is_openai_style_model prefix check routes openai/* models to pass-through serialization while flux-style models keep their existing remapping; logic is correct and backward-compatible.
model_prices_and_context_window.json Adds aiml/openai/gpt-image-2 entry with mode: image_generation, output_cost_per_image: 0.054, and a notes field documenting the medium-quality flat-rate caveat; mirrors the backup file correctly.
litellm/model_prices_and_context_window_backup.json Identical entry to model_prices_and_context_window.json — both files kept in sync as expected.
tests/test_litellm/llms/aiml/image_generation/test_aiml_image_generation_transformation.py New unit test file covering OpenAI-style param surface, flux-style remapping, drop-params behavior, and per-image cost lookup; uses local cost map, no network calls.
tests/image_gen_tests/test_image_generation.py Adds end-to-end mock test for aiml/openai/gpt-image-2 that patches HTTPHandler.post and verifies the upstream request body retains the OpenAI param shape and correct model name.

Reviews (4): Last reviewed commit: "style(aiml): format transformation.py at..." | Re-trigger Greptile

Comment thread model_prices_and_context_window.json Resolved
Documents in the cost-map notes that output_cost_per_image is AI/ML's
published medium-quality rate, billed as a flat per-image price like the
other aiml image entries. Reformats the touched files under the repo's
ruff formatter (migrated from black in #31317).
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai


Generated by Claude Code

LiteLLM only implements an image generation transformer for AIML, so
listing /v1/images/edits overclaimed support. Align with every other
aiml image entry, which lists only /v1/images/generations.
@mateo-berri

Copy link
Copy Markdown
Contributor Author

Good catch on /v1/images/edits. LiteLLM only implements an image generation transformer for AIML, so listing the edits endpoint overclaimed support; I removed it in 6550d97 and the entry now lists only /v1/images/generations, matching every other aiml image model

@greptileai


Generated by Claude Code

The repo formats litellm/ with ruff at line-length 88 (Makefile/CI call
sites), while ruff.toml's global 120 only governs E501/import sorting.
Reformat the transformer to 88 so make format-check / CI lint pass, and
restore the test files to their original layout since tests/ is not part
of the auto-formatted tree.
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai


Generated by Claude Code

@mateo-berri

Copy link
Copy Markdown
Contributor Author

bugbot run


Generated by Claude Code

@cursor cursor Bot 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1694f94. Configure here.

@ghost
ghost merged commit b7f28bd into litellm_internal_staging Jun 25, 2026
76 checks passed
@ghost
ghost deleted the litellm_aiml-gpt-image-2-5b10 branch June 25, 2026 23:41
This pull request was closed.
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