{"record":{"id":"156e3475f2350550","repo":"microsoft/semantic-kernel","slug":"failed-to-generate-image-ex","errorCode":null,"errorMessage":"Failed to generate image: {ex}","messagePattern":"Failed to generate image: (.+?)","errorType":"exception","errorClass":"ServiceResponseException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/open_ai/services/open_ai_handler.py","lineNumber":132,"sourceCode":"\n            self.store_usage(response)\n            return [x.embedding for x in response.data]\n        except Exception as ex:\n            raise ServiceResponseException(\n                f\"{type(self)} service failed to generate embeddings\",\n                ex,\n            ) from ex\n\n    async def _send_text_to_image_request(self, settings: OpenAITextToImageExecutionSettings) -> ImagesResponse:\n        \"\"\"Send a request to the OpenAI text to image endpoint.\"\"\"\n        try:\n            response: ImagesResponse = await self.client.images.generate(\n                **settings.prepare_settings_dict(),\n            )\n            self.store_usage(response)\n            return response\n        except Exception as ex:\n            raise ServiceResponseException(f\"Failed to generate image: {ex}\") from ex\n\n    async def _send_image_edit_request(\n        self,\n        image: list[FileTypes],\n        settings: OpenAITextToImageExecutionSettings,\n        mask: FileTypes | Omit = omit,\n    ) -> ImagesResponse:\n        \"\"\"Send a request to the OpenAI image edit endpoint.\n\n        Args:\n            image: List of image files to edit. Accepts file paths or bytes.\n            settings: Image edit execution settings.\n            mask: Optional mask image. Accepts file path or bytes.\n\n        Returns:\n            ImagesResponse: The response from the image edit API.\n        \"\"\"\n        try:","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/open_ai/services/open_ai_handler.py#L114-L150","documentation":"Raised as ServiceResponseException in _send_text_to_image_request when any exception occurs during client.images.generate. Unlike other handlers, this error message interpolates the exception directly into the string rather than passing it as the __cause__ positional arg (though `from ex` still chains it). The wrapped error reveals the image-generation failure reason.","triggerScenarios":"Calling text-to-image generation with invalid parameters: unsupported image model, prompt that triggers content filter, invalid size/quality parameters, rate limit, or network failure during client.images.generate.","commonSituations":"Using DALL-E with an unsupported size parameter; prompt flagged by content moderation; model name misspelled; rate limit from too many image generation requests; quota exhausted for image API.","solutions":["Read the interpolated exception message (f'Failed to generate image: {ex}') for the specific API error","Verify the model name (e.g., dall-e-3) and size/quality parameters are valid for that model","For content-filter rejections, rephrase the prompt","For rate limits, reduce request frequency or implement backoff"],"exampleFix":"# before\nsettings = OpenAITextToImageExecutionSettings(ai_model_id='dall-e-3', size='1024x1024')\n# after — also validate the prompt is not empty\nif not prompt.strip():\n    raise ValueError('Prompt cannot be empty for image generation')\nsettings = OpenAITextToImageExecutionSettings(ai_model_id='dall-e-3', size='1024x1024')","handlingStrategy":"try-catch","validationCode":"VALID_SIZES_DALLE3 = {'1024x1024', '1792x1024', '1024x1792'}\nif settings.size and settings.size not in VALID_SIZES_DALLE3:\n    raise ValueError(f'Invalid size {settings.size}; valid: {VALID_SIZES_DALLE3}')","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions import ServiceResponseException\n\ntry:\n    response = await service._send_text_to_image_request(settings)\nexcept ServiceResponseException as e:\n    logger.error('Image generation failed: %s', e)\n    raise","preventionTips":["Validate image parameters (size, quality, model) against the model spec before calling","Pre-screen prompts for prohibited content to avoid content-filter rejections"],"tags":["openai","text-to-image","dall-e","catch-all"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}