From 2c02583c2b19f72b3904481c9d219e325b35ef10 Mon Sep 17 00:00:00 2001 From: rob-maron <132852777+rob-maron@users.noreply.github.com> Date: Wed, 24 Jun 2026 16:00:53 -0400 Subject: [PATCH] fix shape --- plugins/image_gen/krea/__init__.py | 20 +++++++++++-- tests/plugins/image_gen/test_krea_provider.py | 29 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/plugins/image_gen/krea/__init__.py b/plugins/image_gen/krea/__init__.py index 02565d237c1..d7b260667aa 100644 --- a/plugins/image_gen/krea/__init__.py +++ b/plugins/image_gen/krea/__init__.py @@ -86,6 +86,11 @@ _ASPECT_MAP = { # Only resolution Krea currently supports. DEFAULT_RESOLUTION = "1K" +# Krea's image_style_references entries are objects ({"url", "strength"}), not +# bare URL strings. When the caller supplies a URL without an explicit strength +# we apply Krea's recommended starting value. Range per Krea docs is -2..2. +_DEFAULT_STYLE_REFERENCE_STRENGTH = 0.6 + # Valid creativity levels per Krea docs. Default is "medium". _VALID_CREATIVITY = {"raw", "low", "medium", "high"} @@ -402,8 +407,19 @@ class KreaImageGenProvider(ImageGenProvider): if style_refs: # Reference-guided generation (image-to-image style transfer). - # Krea caps at 10 refs per request (already clamped above). - payload["image_style_references"] = style_refs + # Krea requires each entry to be an object ({"url", "strength"}), + # NOT a bare URL string — a string yields a 422 "Expected object, + # received string". Convert URL strings to the object form and pass + # already-object refs through verbatim (clamped to 10 above). + normalized_refs: List[Any] = [] + for ref in style_refs: + if isinstance(ref, str): + normalized_refs.append( + {"url": ref, "strength": _DEFAULT_STYLE_REFERENCE_STRENGTH} + ) + else: + normalized_refs.append(ref) + payload["image_style_references"] = normalized_refs moodboards = kwargs.get("moodboards") if isinstance(moodboards, list) and moodboards: diff --git a/tests/plugins/image_gen/test_krea_provider.py b/tests/plugins/image_gen/test_krea_provider.py index 4f7b7919e51..597b4ebbe64 100644 --- a/tests/plugins/image_gen/test_krea_provider.py +++ b/tests/plugins/image_gen/test_krea_provider.py @@ -309,6 +309,35 @@ class TestGenerate: assert len(payload["image_style_references"]) == 10 # capped at 10 assert payload["creativity"] == "high" + def test_string_style_references_converted_to_objects(self): + """Krea requires {url, strength} objects; bare URL strings must be + converted (a string yields a 422 'Expected object, received string').""" + from plugins.image_gen.krea import KreaImageGenProvider + + submit = _submit_response() + poll = _poll_response(_completed_job()) + + with patch("plugins.image_gen.krea.requests.post", return_value=submit) as mock_post, \ + patch("plugins.image_gen.krea.requests.get", return_value=poll), \ + patch( + "plugins.image_gen.krea.save_url_image", + return_value=Path("/tmp/x.png"), + ), \ + patch("plugins.image_gen.krea.time.sleep"): + KreaImageGenProvider().generate( + prompt="test", + image_style_references=[ + "https://x.com/a.png", + {"url": "https://x.com/b.png", "strength": 1.2}, + ], + ) + + payload = mock_post.call_args.kwargs["json"] + assert payload["image_style_references"] == [ + {"url": "https://x.com/a.png", "strength": 0.6}, + {"url": "https://x.com/b.png", "strength": 1.2}, + ] + def test_unknown_kwargs_ignored(self): """Forward-compat: unknown kwargs must not break generate().""" from plugins.image_gen.krea import KreaImageGenProvider