refactor: replace blackforest package with custom async BFL client
Some checks failed
Continuous Integration / Build Package (push) Successful in 41s
Continuous Integration / Lint, Check & Test (push) Failing after 59s

Implement bulkgen/providers/bfl.py with a fully async httpx-based client
that supports all current and future BFL models (including flux-2-*).
Remove the blackforest dependency and simplify the image provider by
eliminating the asyncio.to_thread wrapper.
This commit is contained in:
Konstantin Fickel 2026-02-14 16:44:36 +01:00
parent fd09d127f2
commit cf73511876
Signed by: kfickel
GPG key ID: A793722F9933C1A5
5 changed files with 179 additions and 89 deletions

View file

@ -13,6 +13,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from bulkgen.config import TargetConfig
from bulkgen.providers.bfl import BFLResult
from bulkgen.providers.image import ImageProvider
from bulkgen.providers.image import (
_encode_image_b64 as encode_image_b64, # pyright: ignore[reportPrivateUsage]
@ -22,10 +23,11 @@ from bulkgen.providers.text import TextProvider
def _make_bfl_mocks(
image_bytes: bytes,
) -> tuple[MagicMock, MagicMock]:
"""Return (mock_result, mock_http) for BFL image generation tests."""
mock_result = MagicMock()
mock_result.result = {"sample": "https://example.com/img.png"}
) -> tuple[BFLResult, MagicMock]:
"""Return (bfl_result, mock_http) for BFL image generation tests."""
bfl_result = BFLResult(
task_id="test-task-id", sample_url="https://example.com/img.png"
)
mock_response = MagicMock()
mock_response.content = image_bytes
@ -36,7 +38,7 @@ def _make_bfl_mocks(
mock_http.__aenter__ = AsyncMock(return_value=mock_http)
mock_http.__aexit__ = AsyncMock(return_value=False)
return mock_result, mock_http
return bfl_result, mock_http
def _make_mistral_mock(response: MagicMock) -> AsyncMock:
@ -68,14 +70,13 @@ class TestImageProvider:
self, project_dir: Path, image_bytes: bytes
) -> None:
target_config = TargetConfig(prompt="A red square")
mock_result, mock_http = _make_bfl_mocks(image_bytes)
bfl_result, mock_http = _make_bfl_mocks(image_bytes)
with (
patch("bulkgen.providers.image.BFLClient") as mock_cls,
patch("bulkgen.providers.image.isinstance", return_value=True),
patch("bulkgen.providers.image.httpx.AsyncClient") as mock_http_cls,
):
mock_cls.return_value.generate.return_value = mock_result
mock_cls.return_value.generate = AsyncMock(return_value=bfl_result)
mock_http_cls.return_value = mock_http
provider = ImageProvider(api_key="test-key")
@ -95,15 +96,14 @@ class TestImageProvider:
self, project_dir: Path, image_bytes: bytes
) -> None:
target_config = TargetConfig(prompt="A banner", width=1920, height=480)
mock_result, mock_http = _make_bfl_mocks(image_bytes)
bfl_result, mock_http = _make_bfl_mocks(image_bytes)
with (
patch("bulkgen.providers.image.BFLClient") as mock_cls,
patch("bulkgen.providers.image.isinstance", return_value=True),
patch("bulkgen.providers.image.httpx.AsyncClient") as mock_http_cls,
):
client_instance = mock_cls.return_value
client_instance.generate.return_value = mock_result
mock_generate = AsyncMock(return_value=bfl_result)
mock_cls.return_value.generate = mock_generate
mock_http_cls.return_value = mock_http
provider = ImageProvider(api_key="test-key")
@ -115,7 +115,7 @@ class TestImageProvider:
project_dir=project_dir,
)
call_args = client_instance.generate.call_args
call_args = mock_generate.call_args
inputs = call_args[0][1]
assert inputs["width"] == 1920
assert inputs["height"] == 480
@ -127,15 +127,14 @@ class TestImageProvider:
_ = ref_path.write_bytes(b"reference image data")
target_config = TargetConfig(prompt="Like this", reference_image="ref.png")
mock_result, mock_http = _make_bfl_mocks(image_bytes)
bfl_result, mock_http = _make_bfl_mocks(image_bytes)
with (
patch("bulkgen.providers.image.BFLClient") as mock_cls,
patch("bulkgen.providers.image.isinstance", return_value=True),
patch("bulkgen.providers.image.httpx.AsyncClient") as mock_http_cls,
):
client_instance = mock_cls.return_value
client_instance.generate.return_value = mock_result
mock_generate = AsyncMock(return_value=bfl_result)
mock_cls.return_value.generate = mock_generate
mock_http_cls.return_value = mock_http
provider = ImageProvider(api_key="test-key")
@ -147,29 +146,28 @@ class TestImageProvider:
project_dir=project_dir,
)
call_args = client_instance.generate.call_args
call_args = mock_generate.call_args
inputs = call_args[0][1]
assert "image_prompt" in inputs
assert inputs["image_prompt"] == encode_image_b64(ref_path)
async def test_image_no_sample_url_raises(self, project_dir: Path) -> None:
target_config = TargetConfig(prompt="x")
mock_result = MagicMock()
mock_result.result = {}
with (
patch("bulkgen.providers.image.BFLClient") as mock_cls,
patch("bulkgen.providers.image.isinstance", return_value=True),
):
mock_cls.return_value.generate.return_value = mock_result
with patch("bulkgen.providers.image.BFLClient") as mock_cls:
from bulkgen.providers.bfl import BFLError
mock_cls.return_value.generate = AsyncMock(
side_effect=BFLError("BFL task test ready but no sample URL: {}")
)
provider = ImageProvider(api_key="test-key")
with pytest.raises(RuntimeError, match="did not return an image URL"):
with pytest.raises(BFLError, match="no sample URL"):
await provider.generate(
target_name="fail.png",
target_config=target_config,
resolved_prompt="x",
resolved_model="flux-pro",
resolved_model="flux-pro-1.1",
project_dir=project_dir,
)