From af6056eca1c83da994dac86b0cf33462c2207f13 Mon Sep 17 00:00:00 2001 From: Kerry Lu Date: Sun, 15 Nov 2026 11:28:53 -0800 Subject: [PATCH] fix(fal_ai): send versioned seedream and flux-pro image models to the endpoint they name (#51668) * fix(fal_ai): send versioned seedream and flux-pro image models to the endpoint they name * fix(fal_ai): reject dot segments in the model path and tighten the versioned endpoint tests * test(fal_ai): match the dot segment error message --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../bytedance_transformation.py | 1 + .../flux_pro_v11_ultra_transformation.py | 1 + .../fal_ai/image_generation/transformation.py | 14 ++++- .../providers/test_fal_ai_image_wire.py | 29 +++++++++ .../test_fal_ai_image_generation.py | 59 +++++++++++++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) diff --git a/litellm/llms/fal_ai/image_generation/bytedance_transformation.py b/litellm/llms/fal_ai/image_generation/bytedance_transformation.py index de7b3c9b958..50401241133 100644 --- a/litellm/llms/fal_ai/image_generation/bytedance_transformation.py +++ b/litellm/llms/fal_ai/image_generation/bytedance_transformation.py @@ -92,6 +92,7 @@ class FalAIBytedanceSeedreamV3Config(FalAIBytedanceBaseConfig): """ IMAGE_GENERATION_ENDPOINT: str = "fal-ai/bytedance/seedream/v3/text-to-image" + MODEL_FAMILY_ROOTS: tuple[str, ...] = ("fal-ai/bytedance/seedream/", "bytedance/seedream/") class FalAIBytedanceDreaminaV31Config(FalAIBytedanceBaseConfig): diff --git a/litellm/llms/fal_ai/image_generation/flux_pro_v11_ultra_transformation.py b/litellm/llms/fal_ai/image_generation/flux_pro_v11_ultra_transformation.py index fc215e3d94e..344f9ca088f 100644 --- a/litellm/llms/fal_ai/image_generation/flux_pro_v11_ultra_transformation.py +++ b/litellm/llms/fal_ai/image_generation/flux_pro_v11_ultra_transformation.py @@ -34,6 +34,7 @@ class FalAIFluxProV11UltraConfig(FalAIBaseConfig): """ IMAGE_GENERATION_ENDPOINT: str = "fal-ai/flux-pro/v1.1-ultra" + MODEL_FAMILY_ROOTS: tuple[str, ...] = ("fal-ai/flux-pro/",) def get_supported_openai_params(self, model: str) -> list[OpenAIImageGenerationOptionalParams]: """ diff --git a/litellm/llms/fal_ai/image_generation/transformation.py b/litellm/llms/fal_ai/image_generation/transformation.py index 79913d5c651..cbb3e8848c2 100644 --- a/litellm/llms/fal_ai/image_generation/transformation.py +++ b/litellm/llms/fal_ai/image_generation/transformation.py @@ -5,7 +5,7 @@ import httpx from pydantic import ConfigDict, TypeAdapter from typing_extensions import ReadOnly, TypedDict -from litellm.litellm_core_utils.url_utils import join_api_base +from litellm.litellm_core_utils.url_utils import encode_url_path_segments, join_api_base from litellm.llms.base_llm.image_generation.transformation import ( BaseImageGenerationConfig, ) @@ -71,6 +71,7 @@ class FalAIBaseConfig(BaseImageGenerationConfig): DEFAULT_BASE_URL: str = "https://fal.run" IMAGE_GENERATION_ENDPOINT: str = "" + MODEL_FAMILY_ROOTS: tuple[str, ...] = () def get_complete_url( self, @@ -89,7 +90,16 @@ class FalAIBaseConfig(BaseImageGenerationConfig): base_url: Final = api_base or get_secret_str("FAL_AI_API_BASE") or self.DEFAULT_BASE_URL if not self.IMAGE_GENERATION_ENDPOINT: return base_url.rstrip("/") - return join_api_base(base_url, self.IMAGE_GENERATION_ENDPOINT) + return join_api_base(base_url, self._endpoint_for_model(model)) + + def _endpoint_for_model(self, model: str) -> str: + path: Final = encode_url_path_segments(model.strip("/"), field_name="model") + names_default_endpoint: Final = path.lower().removeprefix( + "fal-ai/" + ) == self.IMAGE_GENERATION_ENDPOINT.removeprefix("fal-ai/") + if names_default_endpoint or not path.lower().startswith(self.MODEL_FAMILY_ROOTS): + return self.IMAGE_GENERATION_ENDPOINT + return path def validate_environment( self, diff --git a/tests/integration/providers/test_fal_ai_image_wire.py b/tests/integration/providers/test_fal_ai_image_wire.py index a573aff8d03..39d3ddae9b6 100644 --- a/tests/integration/providers/test_fal_ai_image_wire.py +++ b/tests/integration/providers/test_fal_ai_image_wire.py @@ -228,6 +228,35 @@ def test_fal_flux_dev_generation_targets_dev_endpoint_and_charges_per_image(gate assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/fal-ai/flux/dev")] +@pytest.mark.parametrize( + "fal_model", + [ + "bytedance/seedream/v5.5/text-to-image", + "fal-ai/bytedance/seedream/v4.5/text-to-image", + "fal-ai/bytedance/seedream/v3/text-to-image", + "fal-ai/flux-pro/kontext/text-to-image", + "fal-ai/flux-pro/v1.1-ultra-finetuned", + "fal-ai/flux-pro/v1.1", + ], +) +def test_fal_versioned_image_model_posts_to_its_own_endpoint_and_charges_its_own_row( + gateway: Gateway, fal_model: str +) -> None: + prompt: Final = f"{_PROMPT} {fal_model}" + + def respond(request: Request) -> Reply: + assert _JSON_OBJECT.validate_json(request.body) == {"prompt": prompt} + return Reply(body=_image_response(((f"{wire_url}/files/versioned.png", 1024, 1024),), prompt)) + + with wire_server(respond) as wire, gateway.scenario() as scenario: + wire_url: Final = wire.url + model: Final = scenario.model(model=f"fal_ai/{fal_model}", api_base=wire.url, api_key="synthetic-fal-key") + response: Final = gateway.request("POST", "/v1/images/generations", {"model": model, "prompt": prompt}) + assert response.status_code == 200, response.text + assert [(request.method, request.target) for request in wire.drain()] == [("POST", f"/{fal_model}")] + assert _response_cost(response) == _approx(_catalog_cost(f"fal_ai/{fal_model}")) + + @pytest.mark.covers("other.provider_wire.fal_ai.image_edit_json_data_urls_and_keyed_pricing") def test_fal_gpt_image_25_edit_inlines_upload_as_data_url_and_charges_keyed_row(gateway: Gateway) -> None: def respond(request: Request) -> Reply: diff --git a/tests/unit/llms/fal_ai/image_generation/test_fal_ai_image_generation.py b/tests/unit/llms/fal_ai/image_generation/test_fal_ai_image_generation.py index 00c0a75890d..bfa51f742a2 100644 --- a/tests/unit/llms/fal_ai/image_generation/test_fal_ai_image_generation.py +++ b/tests/unit/llms/fal_ai/image_generation/test_fal_ai_image_generation.py @@ -1,8 +1,10 @@ +from typing import Final from unittest.mock import MagicMock, patch import pytest from litellm import aimage_generation +from litellm.llms.fal_ai.image_generation import get_fal_ai_image_generation_config from tests._vcr_conftest_common import install_live_call_probe, record_vcr_outcome @@ -96,8 +98,65 @@ async def test_fal_ai_image_generation_basic(model, expected_endpoint): print(f"Validated request body: {captured_json_data}") +@pytest.mark.parametrize( + "model,expected_url", + [ + ("bytedance/seedream/v5.5/text-to-image", "https://fal.run/bytedance/seedream/v5.5/text-to-image"), + ("bytedance/seedream/v5/lite/text-to-image", "https://fal.run/bytedance/seedream/v5/lite/text-to-image"), + ( + "fal-ai/bytedance/seedream/v4.5/text-to-image", + "https://fal.run/fal-ai/bytedance/seedream/v4.5/text-to-image", + ), + ("fal-ai/bytedance/seedream/v3/text-to-image", "https://fal.run/fal-ai/bytedance/seedream/v3/text-to-image"), + ("bytedance/seedream/v3/text-to-image", "https://fal.run/fal-ai/bytedance/seedream/v3/text-to-image"), + ("bytedance/seedream", "https://fal.run/fal-ai/bytedance/seedream/v3/text-to-image"), + ( + "fal-ai/bytedance/dreamina/v3.1/text-to-image", + "https://fal.run/fal-ai/bytedance/dreamina/v3.1/text-to-image", + ), + ("fal-ai/flux-pro/v1/vto", "https://fal.run/fal-ai/flux-pro/v1/vto"), + ("fal-ai/flux-pro/kontext/text-to-image", "https://fal.run/fal-ai/flux-pro/kontext/text-to-image"), + ("fal-ai/flux-pro/v1.1-ultra-finetuned", "https://fal.run/fal-ai/flux-pro/v1.1-ultra-finetuned"), + ("fal-ai/flux-pro/v1.1-ultra", "https://fal.run/fal-ai/flux-pro/v1.1-ultra"), + ("flux-pro/v1.1-ultra", "https://fal.run/fal-ai/flux-pro/v1.1-ultra"), + ("fal-ai/flux-pro/v1.1", "https://fal.run/fal-ai/flux-pro/v1.1"), + ("flux-pro/v1.1", "https://fal.run/fal-ai/flux-pro/v1.1"), + ("fal-ai/flux-pro", "https://fal.run/fal-ai/flux-pro/v1.1"), + ("flux-pro", "https://fal.run/fal-ai/flux-pro/v1.1"), + ("fal-ai/flux/schnell", "https://fal.run/fal-ai/flux/schnell"), + ("fal-ai/flux/dev", "https://fal.run/fal-ai/flux/dev"), + ], +) +def test_fal_ai_seedream_and_flux_pro_models_post_to_the_endpoint_they_name(model: str, expected_url: str) -> None: + config: Final = get_fal_ai_image_generation_config(model) + + assert ( + config.get_complete_url( + api_base="https://fal.run", api_key=None, model=model, optional_params={}, litellm_params={} + ) + == expected_url + ) + + @pytest.fixture(autouse=True) def _vcr_outcome_gate(request, vcr): install_live_call_probe(request, vcr) yield record_vcr_outcome(request, vcr) + + +@pytest.mark.parametrize( + "model", + [ + "fal-ai/flux-pro/../../bytedance/seedream/v5.5/text-to-image", + "fal-ai/flux-pro/./v1/vto", + "fal-ai/bytedance/seedream/..", + ], +) +def test_fal_ai_model_path_with_dot_segments_is_rejected_before_any_request(model: str) -> None: + config: Final = get_fal_ai_image_generation_config(model) + + with pytest.raises(ValueError, match="dot path segment"): + config.get_complete_url( + api_base="https://fal.run", api_key=None, model=model, optional_params={}, litellm_params={} + )