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 <xyi74976-del@users.noreply.github.com>
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
yi-coding 2026-11-17 16:43:05 +08:00 committed by GitHub
parent c87e238ddd
commit c2b29f118d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 110 additions and 1 deletions

View File

@ -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.

View File

@ -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/`

View File

@ -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")

View File

@ -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