gemini image models fixes in /images/generations and /images/edits: usage logging, spend calculation, passing size params - #28275
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR fixes usage logging, spend calculation, and parameter handling for Gemini image models on the
Confidence Score: 5/5Safe to merge — all changes are additive fixes confined to Gemini image model paths with no impact on other providers. The bug fixes are narrow and well-scoped: a wrong endpoint condition for Imagen, missing token-breakdown fields in spend calculation, and missing size-to-config mapping. All three fixes are verified by the PR's own proof-of-spend table and by the new parametrised unit tests that cover every documented Gemini size combination. The shared helpers extracted by the refactor are straightforward and don't alter any existing public contracts. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/gemini/common_utils.py | Adds the size-mapping lookup table, shared param-mapping helpers, and get_gemini_image_generation_config; logic is correct and well-tested. |
| litellm/llms/gemini/image_usage_transformation.py | New shared utility that transforms Gemini usageMetadata into ImageUsage; correctly handles present/absent candidatesTokensDetails and reconciles any token-count gap into text tokens. |
| litellm/llms/gemini/image_generation/transformation.py | Replaces bespoke param-mapping and usage-transform code with shared helpers; fixes the 'gemini' in model endpoint condition with is_gemini_image_model() to correctly route Imagen models. |
| litellm/llms/gemini/image_edit/transformation.py | Now supports n and imageConfig params, delegates to shared helpers, and populates model_response.usage from usageMetadata for correct spend calculation. |
| litellm/llms/gemini/image_edit/cost_calculator.py | Delegates entirely to the image-generation cost calculator; eliminates the now-incorrect flat per-image calculation. |
| litellm/litellm_core_utils/llm_cost_calc/utils.py | Updated to read completion_tokens_details/output_tokens_details from usage when present instead of always treating all completion tokens as image tokens. |
| litellm/utils.py | Adds imageConfig as a recognised optional param for image generation so it flows through the standard param-extraction pipeline. |
| tests/test_litellm/llms/gemini/test_gemini_image_generation_transformation.py | New test file; covers size mapping, imageConfig precedence, Imagen vs. Gemini routing, usage token details, and fallback behaviour. All mocked, no real network calls. |
| tests/test_litellm/llms/gemini/test_cost_calculator.py | Adds cost-calculator tests that verify token-based pricing, output-token-detail splits, and flat-image fallback; uses LITELLM_LOCAL_MODEL_COST_MAP to avoid network calls. |
| tests/llm_translation/test_gemini.py | Adds exhaustive parametrised tests for all 56+ documented size/ratio/imageSize combinations plus snap-to-nearest and passthrough cases. All use mocked HTTP. |
| litellm/types/images/main.py | Adds imageConfig to ImageEditOptionalRequestParams so the new param is recognised in the type system. |
| litellm/types/llms/gemini.py | Adds imageSize field to GeminiImageGenerationParameters to match the new param the API now accepts. |
| litellm/types/llms/openai.py | Adds imageConfig to OpenAIImageGenerationOptionalParams literal union. |
| tests/test_litellm/llms/gemini/image_edit/test_gemini_image_edit_transformation.py | Extended with tests for n, imageConfig passthrough, JSON-string parsing, imageSize stripping for 2.5-flash, and usage metadata extraction. |
| tests/proxy_unit_tests/test_proxy_server.py | Minor addition: imageConfig added to the test_img_gen mock fixture to confirm the param flows through the proxy layer. |
Reviews (3): Last reviewed commit: "Clarify Gemini candidate count precedenc..." | Re-trigger Greptile
|
🤖 litellm-agent: This PR is currently BLOCKED from merge. Score: 3/5 ❌ Why blocked:
Details: Score docked for: 1 PR-related CI failure (This PR will be auto-closed as it lacks a screenshot for proof of fix. Please include one in the PR description. Add the Fix the issues above and push an update — the bot will re-review automatically.
|
|
@greptile can you re-review? All concerns were addressed, can you update your review comment? |
|
🤖 litellm-agent: This PR is currently BLOCKED from merge. Score: 3/5 ❌ Why blocked:
Details: Score docked for: 1 PR-related CI failure (This PR will be auto-closed as it lacks a screenshot for proof of fix. Please include one in the PR description. Add the Fix the issues above and push an update — the bot will re-review automatically.
|
48f4b9e to
5b300a9
Compare
3b62727 to
1723145
Compare
1723145 to
b3ce706
Compare
b3ce706 to
61a51a1
Compare
Relevant issues
Fixes [Bug]: Cannot generate 2K images with Gemini 3.1 Flash Image Preview (stuck at 1K) - extra_body is stripped #24621
Fixes incorrect usage and spend logging for /images/generations and /images/edits when using Gemini Nano Banana models: image_tokens were not captured; Previously /images/edits incorrectly applied "cost per image" calculation (wrong for nano banana), /images/generations simply ignored the cost of image generation and only billed for input tokens.
imagen model (gemini/imagen-4.0-generate-001) did not work via /images/generations due to misapplied branch condition looking for "gemini" in model name (bug accidentally discovered during testing, quick fix).
Prior PRs were incomplete:
@Chesars FYI
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/test_litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand will receiv a Confidence Score of at least 4/5 before requesting a maintainer reviewProof of Fix
Gemini
/v1/images/generations— OpenAIsizeGemini
/v1/images/generations— nativeimageConfigImagen
/v1/images/generationsGemini
/v1/images/edits— multipart image upload +imageConfigSpend log rows:
Screenshot
Type
🐛 Bug Fix
Changes
For Gemini
/v1/images/generationsand/v1/images/editsendpoints:sizefor Gemini image generation by mapping it to GeminigenerationConfig.imageConfig. Use the documented Gemini image aspect ratio and resolution table for exact matches, then snap unsupported dimensions to the closest known aspect ratio/resolution.imageConfigas-is, if provided in the bodyimageConfigentirely forsize=autoor unparseable sizes so Gemini defaults remain in control.aspectRatioand omitimageSize.