{"record":{"id":"7a9119bd14aacee9","repo":"microsoft/semantic-kernel","slug":"invalid-image-size-size-width-x-size-height","errorCode":null,"errorMessage":"Invalid image size: {size.width}x{size.height}.","messagePattern":"Invalid image size: (.+?)x(.+?)\\.","errorType":"validation","errorClass":"ServiceInvalidExecutionSettingsError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/open_ai/prompt_execution_settings/open_ai_text_to_image_execution_settings.py","lineNumber":73,"sourceCode":"                extension_data = data[\"extension_data\"]\n                if (\n                    isinstance(extension_data, dict)\n                    and \"size\" not in extension_data\n                    and \"width\" in extension_data\n                    and \"height\" in extension_data\n                ):\n                    data[\"extension_data\"][\"size\"] = ImageSize(\n                        width=extension_data[\"width\"], height=extension_data[\"height\"]\n                    )\n        return data\n\n    @model_validator(mode=\"after\")\n    def check_size(self) -> \"OpenAITextToImageExecutionSettings\":\n        \"\"\"Check that the requested image size is valid.\"\"\"\n        size = self.size or self.extension_data.get(\"size\")\n\n        if size is not None and (size.width, size.height) not in VALID_IMAGE_SIZES:\n            raise ServiceInvalidExecutionSettingsError(f\"Invalid image size: {size.width}x{size.height}.\")\n\n        return self\n\n    def prepare_settings_dict(self, **kwargs) -> dict[str, Any]:\n        \"\"\"Prepare the settings dictionary for the OpenAI API.\"\"\"\n        settings_dict = super().prepare_settings_dict(**kwargs)\n\n        if self.size is not None:\n            settings_dict[\"size\"] = str(self.size)\n\n        return settings_dict\n","sourceCodeStart":55,"sourceCodeEnd":85,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/open_ai/prompt_execution_settings/open_ai_text_to_image_execution_settings.py#L55-L85","documentation":"The OpenAITextToImageExecutionSettings validator checks that the requested (width, height) pair is in VALID_IMAGE_SIZES: (256,256), (512,512), (1024,1024), (1792,1024), (1024,1792). Any other dimension tuple raises ServiceInvalidExecutionSettingsError. The size can come from the ImageSize field or from extension_data.","triggerScenarios":"Setting OpenAITextToImageExecutionSettings(size=ImageSize(width=800, height=600)) or passing width/height via extension_data that don't match a supported size. The model_validator runs after instantiation.","commonSituations":"Assuming arbitrary dimensions are supported (like a generic image API); copy-pasting a size from a different provider (e.g. Stable Diffusion's 768x768); changing size for DALL-E 3 which only supports 1024x1024, 1792x1024, 1024x1792.","solutions":["Use one of the five valid size tuples: (256,256), (512,512), (1024,1024), (1792,1024), (1024,1792).","For DALL-E 3 specifically, use only 1024x1024, 1792x1024, or 1024x1792.","Check VALID_IMAGE_SIZES before setting — or omit size and let the service use its default."],"exampleFix":"// before\nsettings = OpenAITextToImageExecutionSettings(size=ImageSize(width=800, height=600))\n// after\nsettings = OpenAITextToImageExecutionSettings(size=ImageSize(width=1024, height=1024))","handlingStrategy":"validation","validationCode":"VALID_IMAGE_SIZES = {(256, 256), (512, 512), (1024, 1024), (1792, 1024), (1024, 1792)}\n\ndef validate_image_size(width: int, height: int) -> None:\n    if (width, height) not in VALID_IMAGE_SIZES:\n        raise ValueError(\n            f'Invalid image size: {width}x{height}. Valid sizes: {sorted(VALID_IMAGE_SIZES)}'\n        )","typeGuard":"def is_valid_image_size(width: int, height: int) -> bool:\n    return (width, height) in VALID_IMAGE_SIZES","tryCatchPattern":"from semantic_kernel.exceptions import ServiceInvalidExecutionSettingsError\n\ntry:\n    settings = OpenAITextToImageExecutionSettings(size=ImageSize(width=w, height=h))\nexcept ServiceInvalidExecutionSettingsError as e:\n    # snap to nearest valid size\n    settings = OpenAITextToImageExecutionSettings(size=ImageSize(width=1024, height=1024))","preventionTips":["Hard-code the five valid size tuples in your UI/config layer.","For DALL-E 3, restrict choices to 1024x1024, 1792x1024, 1024x1792.","Validate dimensions before constructing settings, not after."],"tags":["openai","text-to-image","dalle","image-size","execution-settings","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}