microsoft/semantic-kernel · error · ServiceResponseException

Failed to edit image.

Error message

Failed to edit image.

What it means

Raised by edit_image after the image-edit request returns. It validates that response is truthy, response.data exists, and response.data is a list; if any fail it throws ServiceResponseException. This catches a missing or malformed edit response before iteration.

Source

Thrown at python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_to_image_base.py:233

        if image_paths is not None:
            images = [Path(p) for p in image_paths]
        elif image_files is not None:
            images = list(image_files)

        mask: FileTypes | Omit = omit
        if mask_path is not None:
            mask = Path(mask_path)
        elif mask_file is not None:
            mask = mask_file

        response: ImagesResponse = await self._send_image_edit_request(
            image=images,
            mask=mask,
            settings=settings,
        )

        if not response or not response.data or not isinstance(response.data, list):
            raise ServiceResponseException("Failed to edit image.")

        results: list[str] = []
        for img in response.data:
            b64_json: str | None = getattr(img, "b64_json", None)
            url: str | None = getattr(img, "url", None)
            if b64_json:
                results.append(b64_json)
            elif url:
                results.append(url)
        if not results:
            raise ServiceResponseException("No valid image data found in response.")
        return results

    def get_prompt_execution_settings_class(self) -> type[PromptExecutionSettings]:
        """Get the request settings class."""
        return OpenAITextToImageExecutionSettings

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Confirm the model/deployment supports image edits (e.g. gpt-image-1) and is deployed
  2. Verify the endpoint is the real OpenAI/compatible edit API
  3. Log the raw response object to inspect the actual payload shape
  4. Retry once to rule out a transient fault
Defensive patterns

Strategy: try-catch

Try / catch

from semantic_kernel.exceptions.service_exceptions import ServiceResponseException

try:
    res = await service.edit_image(prompt=p, image_paths=paths)
except ServiceResponseException as e:
    if "Failed to edit image" in str(e):
        # response falsy or data not a list; inspect endpoint/deployment
        ...
    raise

Prevention

When it happens

Trigger: The edit endpoint returned None (request failure handled upstream into a falsy value), response.data is None, or response.data is not a list type. Common with proxy/compatible endpoints returning a different schema, or when the edit API returns an error-shaped 2xx.

Common situations: Non-OpenAI-compatible edit endpoint returning a non-standard body; the edit model isn't enabled for the deployment; a gateway returned an error envelope that still parsed.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/b21765ef6e063f0c. Report an issue: GitHub.