mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-13 08:52:02 +00:00
fix(azure_ai): preserve fallback for empty FLUX.2 image responses
This commit is contained in:
parent
16c6989390
commit
723a9fe542
2 changed files with 17 additions and 1 deletions
|
|
@ -50,13 +50,15 @@ def record_megapixel_usage(response: object, response_body: bytes | Mapping[str,
|
|||
|
||||
|
||||
def _megapixel_cost(model_info: ModelInfo, image_response: ImageResponse) -> float | None:
|
||||
images: Final = len(image_response.data or ())
|
||||
if not images:
|
||||
return None
|
||||
try:
|
||||
prices: Final = _MegapixelPrices.model_validate(model_info)
|
||||
usage: Final = _MegapixelUsage.model_validate(image_response.hidden_params.get(_MEGAPIXEL_USAGE_HIDDEN_PARAM))
|
||||
except ValidationError:
|
||||
return None
|
||||
first_rate: Final = prices.output_cost_per_first_megapixel
|
||||
images: Final = max(len(image_response.data or ()), 1)
|
||||
output_mp_per_image: Final = usage.output_mp / images
|
||||
return images * (
|
||||
(prices.output_cost_per_megapixel if first_rate is None else first_rate)
|
||||
|
|
|
|||
|
|
@ -334,6 +334,20 @@ def test_flux2_pro_generation_bills_first_megapixel_per_returned_image(
|
|||
)
|
||||
|
||||
|
||||
@respx.mock
|
||||
def test_flux2_pro_generation_without_returned_images_has_no_cost() -> None:
|
||||
respx.post(url__regex=_flux2_route("flux-2-pro")).mock(
|
||||
return_value=httpx.Response(200, json=_metered_body(3.0, 1.5, num_images=0))
|
||||
)
|
||||
|
||||
response: Final = litellm.image_generation(
|
||||
model="azure_ai/flux.2-pro", prompt="A red fox", n=4, api_base=FOUNDRY_BASE, api_key="test-key"
|
||||
)
|
||||
|
||||
assert response.data == []
|
||||
assert response._hidden_params["response_cost"] == 0.0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_flux2_pro_generation_bills_the_reported_megapixels(
|
||||
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue