Skip to content

The agent that grows with you

fix(image_gen): cap local Krea style references by total size so multi-file requests fit the gateway limit

Mariano Nicolini87f3fa8881a3
diff --git a/plugins/image_gen/krea/__init__.py b/plugins/image_gen/krea/__init__.py
index 295b25054e6..1b96f5a7813 100644
--- a/plugins/image_gen/krea/__init__.py
+++ b/plugins/image_gen/krea/__init__.py
@@ -62,7 +62,8 @@ DEFAULT_RESOLUTION = "1K" # only resolution Krea currently supports
_DEFAULT_STYLE_REFERENCE_STRENGTH = 0.6
_MAX_STYLE_REFERENCES = 10
_REMOTE_REFERENCE_PREFIXES = ("http://", "https://", "data:")
-# Base64 grows a file by a third and the managed gateway rejects bodies over about 4.5 MB.
+# Base64 grows a file by a third and the managed gateway rejects bodies over about 4.5 MB,
+# so all local references together stay under this.
_MAX_LOCAL_REFERENCE_BYTES = 3 * 1024 * 1024
_VALID_CREATIVITY = {"raw", "low", "medium", "high"}
@@ -320,6 +321,7 @@ def _inline_local_style_refs(
from agent.file_safety import raise_if_read_blocked
inlined: List[Any] = []
+ total_bytes = 0
for ref in style_refs:
source = ref.get("url") if isinstance(ref, dict) else ref
if not isinstance(source, str) or source.lower().startswith(_REMOTE_REFERENCE_PREFIXES):
@@ -328,9 +330,10 @@ def _inline_local_style_refs(
path = Path(os.path.expanduser(source))
if not path.is_file():
return [], fail(f"Style reference image not found: {source}", "invalid_image_url")
- if path.stat().st_size > _MAX_LOCAL_REFERENCE_BYTES:
+ total_bytes += path.stat().st_size
+ if total_bytes > _MAX_LOCAL_REFERENCE_BYTES:
return [], fail(
- f"Style reference image {source} is over 3 MB; resize it or pass a public URL",
+ "Local style reference images total over 3 MB; resize them or pass public URLs",
"source_too_large")
raise_if_read_blocked(str(path))
mime = mimetypes.guess_type(path.name)[0] or "image/png"
diff --git a/tests/plugins/image_gen/test_krea_provider.py b/tests/plugins/image_gen/test_krea_provider.py
index c187617b602..fe6095b38c6 100644
--- a/tests/plugins/image_gen/test_krea_provider.py
+++ b/tests/plugins/image_gen/test_krea_provider.py
@@ -294,6 +294,22 @@ class TestGenerate:
assert result["error_type"] == "source_too_large"
mock_post.assert_not_called()
+ def test_local_style_references_are_limited_in_total_not_per_file(self, tmp_path, monkeypatch):
+ from plugins.image_gen import krea
+
+ monkeypatch.setattr(krea, "_MAX_LOCAL_REFERENCE_BYTES", 10)
+ first = tmp_path / "a.png"
+ second = tmp_path / "b.png"
+ first.write_bytes(b"\x89PNG\r\n")
+ second.write_bytes(b"\x89PNG\r\n")
+
+ with patch("plugins.image_gen.krea.requests.post") as mock_post:
+ result = krea.KreaImageGenProvider().generate(
+ prompt="test", reference_image_urls=[str(first), str(second)])
+
+ assert result["error_type"] == "source_too_large"
+ mock_post.assert_not_called()
+
def test_missing_local_style_reference_is_refused_before_submit(self):
from plugins.image_gen.krea import KreaImageGenProvider
diff --git a/website/docs/user-guide/features/image-generation.md b/website/docs/user-guide/features/image-generation.md
index 88bd66d099e..2a33552c650 100644
--- a/website/docs/user-guide/features/image-generation.md
+++ b/website/docs/user-guide/features/image-generation.md
@@ -290,7 +290,7 @@ Two inputs drive the edit:
| **FAL.ai** (edit-capable models below) | ✓ | up to 16 (per model) | routes to the model's `/edit` endpoint |
| **OpenAI** (GPT Image 2 / 2.5 Flare / Sunburst) | ✓ | up to 16 | `images.edit()` |
| **xAI** (Grok Imagine) | ✓ | 1 | `/v1/images/edits` (`grok-imagine-image-quality`) |
-| **Krea** (`Krea 2`) | ✓ | up to 10 | reference-guided generation (`image_style_references`); local files up to 3 MB are embedded in the request |
+| **Krea** (`Krea 2`) | ✓ | up to 10 | reference-guided generation (`image_style_references`); local files are embedded in the request, up to 3 MB in total |
| **OpenAI (Codex auth)** | ✓ | up to 16 | `POST /backend-api/codex/images/edits` with inline `images[]` data URLs (remote URLs are fetched client-side) |
| **OpenRouter** (Image API models) | ✓ | up to 14–16 (per model) | `input_references` on `POST /images/generations`; chat-served models use `image_url` content parts (up to 3) |