From c2b29f118d677733c960916cbd58c6d647eb8760 Mon Sep 17 00:00:00 2001 From: yi-coding Date: Tue, 17 Nov 2026 16:43:05 +0800 Subject: [PATCH] fix(skills): preserve existing images on Gemini decode errors (#8256) * fix(skills): preserve images on Gemini decode failures * docs(skills): document legacy MiniMax output handling --------- Co-authored-by: xiaoyi Co-authored-by: Willem Jiang --- docs/guide/skills-and-tools.md | 6 ++ skills/public/image-generation/SKILL.md | 8 ++ .../image-generation/scripts/generate.py | 3 +- .../skills/test_image_output_preservation.py | 94 +++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tests/skills/test_image_output_preservation.py diff --git a/docs/guide/skills-and-tools.md b/docs/guide/skills-and-tools.md index 717cdddfbb..438af77579 100644 --- a/docs/guide/skills-and-tools.md +++ b/docs/guide/skills-and-tools.md @@ -166,6 +166,12 @@ submitted reference with a missing or unrecognized extension raises a clear erro before API submission. Filenames must match their contents; image bytes are not validated or converted, and provider-specific format restrictions still apply. +The [image-generation skill](../../skills/public/image-generation/SKILL.md#output-handling) +decodes Gemini base64 image data before opening the output file, +preserving an existing image when decoding fails. With the legacy MiniMax +`/v1/image_generation` endpoint, a decode failure can still erase an existing +output image. Use a fresh `--output-file` path to keep the previous image. + The [music-generation skill](../../skills/public/music-generation/SKILL.md#output-handling) preserves existing output when the provider returns malformed, empty or unrecognized audio. The bundled skill-creator trigger evaluator drains CLI output through EOF, so a completed subprocess does not turn a recorded skill trigger into a false negative. diff --git a/skills/public/image-generation/SKILL.md b/skills/public/image-generation/SKILL.md index ec49618a14..7e94222b9f 100644 --- a/skills/public/image-generation/SKILL.md +++ b/skills/public/image-generation/SKILL.md @@ -155,6 +155,14 @@ Read the following template file only when matching the user request. ## Output Handling +Gemini image data is base64-decoded before the output directory or +file is created. If decoding fails, an existing output image is preserved and no +empty output file is left behind. + +The legacy MiniMax `/v1/image_generation` writer opens the destination before +decoding. If decoding fails, it can leave an empty file or erase an existing +image. Use a fresh `--output-file` path when you need to keep the previous image. + After generation: - Images are typically saved in `/mnt/user-data/outputs/` diff --git a/skills/public/image-generation/scripts/generate.py b/skills/public/image-generation/scripts/generate.py index 5acc61615e..8a2c8ea0e9 100644 --- a/skills/public/image-generation/scripts/generate.py +++ b/skills/public/image-generation/scripts/generate.py @@ -240,9 +240,10 @@ def _generate_image_gemini( image_parts = [part for part in response_parts if part.get("inlineData", False)] if len(image_parts) == 1: base64_image = image_parts[0]["inlineData"]["data"] + image_bytes = base64.b64decode(base64_image) _ensure_output_dir(output_file) with open(output_file, "wb") as f: - f.write(base64.b64decode(base64_image)) + f.write(image_bytes) return f"Successfully generated image to {output_file}" raise Exception("Failed to generate image") diff --git a/tests/skills/test_image_output_preservation.py b/tests/skills/test_image_output_preservation.py new file mode 100644 index 0000000000..986eebde5f --- /dev/null +++ b/tests/skills/test_image_output_preservation.py @@ -0,0 +1,94 @@ +"""Provider decode failures must not erase a previous image or create an artifact.""" + +import base64 +import binascii +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from skill_loader import FakeResp, load + +image = load("image-generation") +PNG = base64.b64decode( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYPgPAAEDAQAIicLsAAAAAElFTkSuQmCC" +) + + +def _response(provider, encoded): + if provider == "gemini": + return { + "candidates": [ + { + "content": { + "parts": [ + {"inlineData": {"mimeType": "image/png", "data": encoded}} + ] + } + } + ] + } + return {"data": [{"b64_json": encoded}]} + + +def _generate(monkeypatch, tmp_path, provider, encoded, output): + monkeypatch.setenv("IMAGE_GENERATION_PROVIDER", provider) + for key in ("MINIMAX_API_KEY", "GEMINI_API_KEY", "IMAGE_GENERATION_API_KEY"): + monkeypatch.setenv(key, "offline-fixture") + monkeypatch.setenv("IMAGE_GENERATION_MODEL", "gpt-image-1") + monkeypatch.setattr( + image.requests, + "post", + lambda *args, **kwargs: FakeResp(_response(provider, encoded)), + ) + prompt = tmp_path / "prompt.txt" + prompt.write_text("a blue square", encoding="utf-8") + return image.generate_image(str(prompt), [], str(output)) + + +@pytest.mark.parametrize("provider", ["gemini", "openai"]) +@pytest.mark.parametrize( + "existing", [False, True], ids=["new-output", "existing-output"] +) +@pytest.mark.parametrize( + "encoded", + ["AA", "☃", ["not a string"]], + ids=["bad-padding", "non-ascii", "wrong-type"], +) +def test_decode_failure_preserves_output( + monkeypatch, tmp_path, provider, existing, encoded +): + output = tmp_path / "images" / "result.png" + if existing: + output.parent.mkdir() + output.write_bytes(PNG) + + with pytest.raises((binascii.Error, ValueError, TypeError)): + _generate(monkeypatch, tmp_path, provider, encoded, output) + + if existing: + assert output.read_bytes() == PNG + else: + assert not output.exists() + assert not output.parent.exists() + + +@pytest.mark.parametrize("provider", ["gemini", "openai"]) +@pytest.mark.parametrize( + "existing", [False, True], ids=["new-output", "existing-output"] +) +def test_valid_image_can_be_written_or_replaced( + monkeypatch, tmp_path, provider, existing +): + output = tmp_path / "images" / "result.png" + if existing: + output.parent.mkdir() + output.write_bytes(b"previous image") + + result = _generate( + monkeypatch, tmp_path, provider, base64.b64encode(PNG).decode("ascii"), output + ) + + assert "Successfully generated image" in result + assert output.read_bytes() == PNG