From 6db343529938823ec765a40d6c36efbf136b2c19 Mon Sep 17 00:00:00 2001 From: shrey kharbanda Date: Wed, 23 Sep 2026 06:38:32 +0000 Subject: [PATCH] fix(azure_ai): price reference pixels from the resolved deployment model info --- .../image_generation/cost_calculator.py | 21 +++++------- litellm/types/utils.py | 1 + .../test_azure_ai_flux2_image_generation.py | 34 +++++++++++++++++++ 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/litellm/llms/azure_ai/image_generation/cost_calculator.py b/litellm/llms/azure_ai/image_generation/cost_calculator.py index 4e0c0ec0cb9..372b5a77f61 100644 --- a/litellm/llms/azure_ai/image_generation/cost_calculator.py +++ b/litellm/llms/azure_ai/image_generation/cost_calculator.py @@ -1,5 +1,5 @@ from collections.abc import Mapping -from typing import Any, Final, cast +from typing import Any, Final import litellm from litellm.litellm_core_utils.llm_cost_calc.utils import ( @@ -10,21 +10,21 @@ from litellm.litellm_core_utils.llm_cost_calc.utils import ( from litellm.types.utils import ImageResponse, ModelInfo -def _input_cost_per_pixel(resolved: ModelInfo) -> float: - deployment_price: Final = _get_cost_per_unit(resolved, "input_cost_per_pixel", default_value=None) +def _pixel_rate(resolved: ModelInfo, cost_key: str) -> float: + deployment_price: Final = _get_cost_per_unit(resolved, cost_key, default_value=None) if deployment_price is not None: return deployment_price model_cost_key: Final = resolved.get("key") shared_entry: Final = litellm.model_cost.get(model_cost_key) if model_cost_key is not None else None if shared_entry is None: return 0.0 - return shared_entry.get("input_cost_per_pixel") or 0.0 + return shared_entry.get(cost_key) or 0.0 -def _reference_cost(model_cost: Mapping[str, object], image_response: ImageResponse) -> float: +def _reference_cost(resolved: ModelInfo, image_response: ImageResponse) -> float: pixels: Final = image_response._hidden_params.get("reference_pixels") - rate: Final = model_cost.get("input_cost_per_reference_pixel") or 0.0 - return float(rate) * pixels if isinstance(pixels, int) and isinstance(rate, (int, float)) else 0.0 + rate: Final = _pixel_rate(resolved, "input_cost_per_reference_pixel") + return rate * pixels if isinstance(pixels, int) else 0.0 def cost_calculator( @@ -57,11 +57,8 @@ def cost_calculator( from litellm.cost_calculator import default_image_cost_calculator num_images: Final = n if n is not None else len(image_response.data or ()) - model_cost: Final = cast( # cast-ok: litellm.model_cost is an untyped shared dict - Mapping[str, object], litellm.model_cost.get(_model_info.get("key") or model) or {} - ) output_cost_per_image: Final[float] = _model_info.get("output_cost_per_image") or 0.0 - input_cost_per_pixel: Final[float] = _input_cost_per_pixel(_model_info) + input_cost_per_pixel: Final[float] = _pixel_rate(_model_info, "input_cost_per_pixel") width: Final = optional_params.get("width") if optional_params else None height: Final = optional_params.get("height") if optional_params else None pixel_size: Final = ( @@ -82,6 +79,6 @@ def cost_calculator( if input_cost_per_pixel else 0.0 ) - return generated_cost + _reference_cost(model_cost, image_response) + return generated_cost + _reference_cost(_model_info, image_response) raise ValueError(f"image_response must be of type ImageResponse got type={type(image_response)}") diff --git a/litellm/types/utils.py b/litellm/types/utils.py index dfc98a9d89d..3943f4c8e53 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -3702,6 +3702,7 @@ class CustomPricingLiteLLMParams(MirroredPricingParams): output_cost_per_image_1024: float | None = None output_cost_per_image_1536: float | None = None input_cost_per_pixel: float | None = None + input_cost_per_reference_pixel: float | None = None output_cost_per_pixel: float | None = None # Include all ModelInfoBase fields as optional diff --git a/tests/test_litellm/llms/azure_ai/image_generation/test_azure_ai_flux2_image_generation.py b/tests/test_litellm/llms/azure_ai/image_generation/test_azure_ai_flux2_image_generation.py index 9f6e98766f2..7095ef643ce 100644 --- a/tests/test_litellm/llms/azure_ai/image_generation/test_azure_ai_flux2_image_generation.py +++ b/tests/test_litellm/llms/azure_ai/image_generation/test_azure_ai_flux2_image_generation.py @@ -302,6 +302,40 @@ def test_flux2_flex_cost_prefers_deployment_input_cost_per_pixel() -> None: assert cost == pytest.approx(2e-07 * 2048 * 1024 * 2) +def test_flux2_flex_reference_cost_prefers_deployment_rate() -> None: + response: Final = ImageResponse( + data=[ImageObject(b64_json="aW1n")], hidden_params={"reference_pixels": 1048576}, size="1024x1024" + ) + + cost: Final = CostCalculatorUtils.route_image_generation_cost_calculator( + model="FLUX.2-flex", + completion_response=response, + custom_llm_provider="azure_ai", + size="1024x1024", + call_type="image_edit", + model_info={"input_cost_per_pixel": 2e-07, "input_cost_per_reference_pixel": 3e-07}, + ) + + assert cost == pytest.approx(2e-07 * 1048576 + 3e-07 * 1048576) + + +def test_unlisted_azure_ai_model_bills_deployment_reference_rate() -> None: + response: Final = ImageResponse( + data=[ImageObject(b64_json="aW1n")], hidden_params={"reference_pixels": 1048576}, size="1024x1024" + ) + + cost: Final = CostCalculatorUtils.route_image_generation_cost_calculator( + model="unlisted-flux-deployment", + completion_response=response, + custom_llm_provider="azure_ai", + size="1024x1024", + call_type="image_edit", + model_info={"input_cost_per_pixel": 1e-07, "input_cost_per_reference_pixel": 1e-07}, + ) + + assert cost == pytest.approx(1e-07 * 1048576 * 2) + + def test_unlisted_azure_ai_model_bills_deployment_input_cost_per_pixel() -> None: response: Final = ImageResponse(data=[ImageObject(b64_json="aW1n"), ImageObject(b64_json="aW1n")])