HKUDS/nanobot · error · ImageGenerationError

generated image payload was not valid base64

Error message

generated image payload was not valid base64

What it means

Guard in _b64_image_data_url: a generated-image payload string from a provider response failed strict base64 decoding. This handles untrusted provider output — the provider returned a malformed or non-base64 payload where an image was expected.

Solutions

  1. Retry the generation; the provider may have returned a corrupted payload
  2. Check the provider response for an error field explaining the bad payload
  3. If reproducible, report the malformed response for that provider
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at nanobot/providers/image_generation.py:124 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of HKUDS/nanobot@42f37dc4c0 (2026-08-26). Data as JSON: /api/errors/2ddb5ea3e592aa2d. Report an issue: GitHub.

Appendix: source

Thrown at nanobot/providers/image_generation.py:124

def image_path_to_data_url(path: str | Path) -> str:
    """Convert a local image path to an image data URL."""
    mime, encoded = _read_image_b64(path)
    return f"data:{mime};base64,{encoded}"


def image_path_to_inline_data(path: str | Path) -> dict[str, str]:
    """Convert a local image path to a Gemini ``inlineData`` payload dict."""
    mime, encoded = _read_image_b64(path)
    return {"mimeType": mime, "data": encoded}


def _b64_image_data_url(value: str) -> str:
    encoded = "".join(value.split())
    try:
        raw = base64.b64decode(encoded, validate=True)
    except binascii.Error as exc:
        raise ImageGenerationError("generated image payload was not valid base64") from exc
    mime = detect_image_mime(raw)
    if mime is None:
        raise ImageGenerationError("generated image payload was not a supported image")
    return f"data:{mime};base64,{encoded}"


def _aihubmix_size(aspect_ratio: str | None, image_size: str | None) -> str:
    """Return an OpenAI Images API size string for AIHubMix.

    The WebUI emits compact size hints like ``1K`` for OpenRouter. AIHubMix's
    Images API expects OpenAI-style dimensions or ``auto``, so only pass
    through explicit dimension strings and otherwise derive the closest
    supported orientation from aspect ratio.
    """
    if image_size and "x" in image_size.lower():
        return image_size
    if aspect_ratio in _AIHUBMIX_ASPECT_RATIO_SIZES:
        return _AIHUBMIX_ASPECT_RATIO_SIZES[aspect_ratio]

View on GitHub (pinned to 42f37dc4c0)