microsoft/semantic-kernel · error · ServiceResponseException
Failed to generate image.
Error message
Failed to generate image.
What it means
Raised by the deprecated generate_image method after the OpenAI images API returns. It checks that response.data exists and that its first element carries either a url or b64_json; if data is empty/None or the first image has neither field, it throws ServiceResponseException. This signals the upstream API returned an unusable payload.
Source
Thrown at python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_to_image_base.py:72
if settings.size and not settings.size.height:
settings.size.height = height
if not settings.size and width and height:
settings.size = ImageSize(width=width, height=height)
if not settings.prompt:
settings.prompt = description
if not settings.prompt:
raise ServiceInvalidRequestError("Prompt is required.")
if not settings.ai_model_id:
settings.ai_model_id = self.ai_model_id
response = await self._send_request(settings)
assert isinstance(response, ImagesResponse) # nosec
if not response.data or not (response.data[0].url or response.data[0].b64_json):
raise ServiceResponseException("Failed to generate image.")
return response.data[0].url or response.data[0].b64_json # type: ignore[return-value]
async def generate_images(
self,
prompt: str,
settings: PromptExecutionSettings | None = None,
**kwargs: Any,
) -> list[str]:
"""Generate one or more images from text. Returns URLs or base64-encoded images.
Args:
prompt: Description of the image(s) to generate.
settings: Execution settings for the prompt.
kwargs: Additional arguments, check the openai images.generate documentation for the supported arguments.
Returns:
list[str]: Image URLs or base64-encoded images.View on GitHub (pinned to c028a0c7dc)
Solutions
- Retry the request with a less policy-sensitive prompt
- Set response_format explicitly in settings (e.g. url or b64_json) to match what you expect to read back
- Verify you are hitting the real OpenAI endpoint and not a proxy that altered the response
- Migrate to generate_images, which has a more defensive data check and returns a list
Defensive patterns
Strategy: try-catch
Try / catch
from semantic_kernel.exceptions.service_exceptions import ServiceResponseException
try:
img = await service.generate_image(description=desc)
except ServiceResponseException as e:
if "Failed to generate image" in str(e):
# upstream returned empty/ unusable payload; retry or fall back
...
raise Prevention
- Migrate to generate_images for a more robust response check
- Set response_format explicitly on settings
- Avoid prompts likely to trip content filters
When it happens
Trigger: The OpenAI image endpoint returned a 200 but with an empty data array, or response.data[0] has neither url nor b64_json (e.g. a content-filtered or partially-flagged response, or an unexpected response shape from a custom/proxy endpoint).
Common situations: Content policy filtered the generation so no image payload was returned; a gateway/proxy stripped fields; an unsupported response_format was requested; intermittent upstream issue.
Related errors
- Prompt is required.
- No valid image data found in response.
- Failed to edit image.
- The OpenAI text to image model ID is required.
- Provide either 'image_paths' or 'image_files', and only one.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/79a4c40d172eb644.
Report an issue: GitHub.