|
| 1 | +import pytest |
| 2 | + |
| 3 | +import litellm |
| 4 | +from litellm.llms.fal_ai.cost_calculator import cost_calculator |
| 5 | +from litellm.llms.fal_ai.image_generation import ( |
| 6 | + FalAIGPTImage2Config, |
| 7 | + FalAINanoBananaConfig, |
| 8 | + get_fal_ai_image_generation_config, |
| 9 | +) |
| 10 | +from litellm.types.utils import ImageObject, ImageResponse |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "model", |
| 15 | + [ |
| 16 | + "openai/gpt-image-2", |
| 17 | + "gpt-image-2", |
| 18 | + "openai/gpt-image-2/edit", |
| 19 | + ], |
| 20 | +) |
| 21 | +def test_gpt_image_2_config_selected(model): |
| 22 | + assert isinstance(get_fal_ai_image_generation_config(model), FalAIGPTImage2Config) |
| 23 | + |
| 24 | + |
| 25 | +def test_nano_banana_still_routes_to_nano_banana_config(): |
| 26 | + assert isinstance( |
| 27 | + get_fal_ai_image_generation_config("fal-ai/nano-banana"), |
| 28 | + FalAINanoBananaConfig, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize( |
| 33 | + "model,expected_url", |
| 34 | + [ |
| 35 | + ("openai/gpt-image-2", "https://fal.run/openai/gpt-image-2"), |
| 36 | + ("gpt-image-2", "https://fal.run/openai/gpt-image-2"), |
| 37 | + ("openai/gpt-image-2/edit", "https://fal.run/openai/gpt-image-2/edit"), |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_get_complete_url_derives_endpoint_from_model(model, expected_url): |
| 41 | + url = FalAIGPTImage2Config().get_complete_url( |
| 42 | + api_base=None, |
| 43 | + api_key="test-key", |
| 44 | + model=model, |
| 45 | + optional_params={}, |
| 46 | + litellm_params={}, |
| 47 | + ) |
| 48 | + assert url == expected_url |
| 49 | + |
| 50 | + |
| 51 | +def test_get_complete_url_respects_api_base_override(): |
| 52 | + url = FalAIGPTImage2Config().get_complete_url( |
| 53 | + api_base="https://proxy.internal/", |
| 54 | + api_key="test-key", |
| 55 | + model="openai/gpt-image-2", |
| 56 | + optional_params={}, |
| 57 | + litellm_params={}, |
| 58 | + ) |
| 59 | + assert url == "https://proxy.internal/openai/gpt-image-2" |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "non_default_params,expected", |
| 64 | + [ |
| 65 | + ({"n": 3}, {"num_images": 3}), |
| 66 | + ({"size": "1024x1536"}, {"image_size": {"width": 1024, "height": 1536}}), |
| 67 | + ({"size": "auto"}, {"image_size": "auto"}), |
| 68 | + ({"quality": "medium"}, {"quality": "medium"}), |
| 69 | + ({"quality": "hd"}, {"quality": "high"}), |
| 70 | + ({"quality": "standard"}, {"quality": "medium"}), |
| 71 | + ({"quality": "nonsense"}, {"quality": "auto"}), |
| 72 | + ({"output_format": "webp"}, {"output_format": "webp"}), |
| 73 | + ({"response_format": "url"}, {}), |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_map_openai_params(non_default_params, expected): |
| 77 | + assert ( |
| 78 | + FalAIGPTImage2Config().map_openai_params( |
| 79 | + non_default_params=non_default_params, |
| 80 | + optional_params={}, |
| 81 | + model="openai/gpt-image-2", |
| 82 | + drop_params=False, |
| 83 | + ) |
| 84 | + == expected |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def test_map_openai_params_keeps_explicit_provider_params(): |
| 89 | + mapped = FalAIGPTImage2Config().map_openai_params( |
| 90 | + non_default_params={"n": 4, "size": "1024x1024"}, |
| 91 | + optional_params={"num_images": 1, "image_size": "square_hd"}, |
| 92 | + model="openai/gpt-image-2", |
| 93 | + drop_params=False, |
| 94 | + ) |
| 95 | + assert mapped == {"num_images": 1, "image_size": "square_hd"} |
| 96 | + |
| 97 | + |
| 98 | +def test_map_openai_params_raises_on_unsupported_param(): |
| 99 | + with pytest.raises(ValueError, match="style"): |
| 100 | + FalAIGPTImage2Config().map_openai_params( |
| 101 | + non_default_params={"style": "vivid"}, |
| 102 | + optional_params={}, |
| 103 | + model="openai/gpt-image-2", |
| 104 | + drop_params=False, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def test_map_openai_params_drops_unsupported_param(): |
| 109 | + assert ( |
| 110 | + FalAIGPTImage2Config().map_openai_params( |
| 111 | + non_default_params={"style": "vivid"}, |
| 112 | + optional_params={}, |
| 113 | + model="openai/gpt-image-2", |
| 114 | + drop_params=True, |
| 115 | + ) |
| 116 | + == {} |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +def test_transform_image_generation_request(): |
| 121 | + assert FalAIGPTImage2Config().transform_image_generation_request( |
| 122 | + model="openai/gpt-image-2", |
| 123 | + prompt="a red bicycle", |
| 124 | + optional_params={"quality": "high", "num_images": 2}, |
| 125 | + litellm_params={}, |
| 126 | + headers={}, |
| 127 | + ) == {"prompt": "a red bicycle", "quality": "high", "num_images": 2} |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize( |
| 131 | + "model", |
| 132 | + [ |
| 133 | + "openai/gpt-image-2", |
| 134 | + "gpt-image-2", |
| 135 | + "openai/gpt-image-2/edit", |
| 136 | + ], |
| 137 | +) |
| 138 | +def test_cost_calculator_uses_registry_price(model, monkeypatch: pytest.MonkeyPatch): |
| 139 | + monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url="")) |
| 140 | + response = ImageResponse( |
| 141 | + data=[ |
| 142 | + ImageObject(url="https://v3b.fal.media/files/b/one.png"), |
| 143 | + ImageObject(url="https://v3b.fal.media/files/b/two.png"), |
| 144 | + ] |
| 145 | + ) |
| 146 | + assert cost_calculator(model=model, image_response=response) == pytest.approx(0.29) |
0 commit comments