microsoft/semantic-kernel · error · ServiceInvalidExecutionSettingsError

Invalid image size: {size.width}x{size.height}.

Error message

Invalid image size: {size.width}x{size.height}.

What it means

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.

Source

Thrown at python/semantic_kernel/connectors/ai/open_ai/prompt_execution_settings/open_ai_text_to_image_execution_settings.py:73

                extension_data = data["extension_data"]
                if (
                    isinstance(extension_data, dict)
                    and "size" not in extension_data
                    and "width" in extension_data
                    and "height" in extension_data
                ):
                    data["extension_data"]["size"] = ImageSize(
                        width=extension_data["width"], height=extension_data["height"]
                    )
        return data

    @model_validator(mode="after")
    def check_size(self) -> "OpenAITextToImageExecutionSettings":
        """Check that the requested image size is valid."""
        size = self.size or self.extension_data.get("size")

        if size is not None and (size.width, size.height) not in VALID_IMAGE_SIZES:
            raise ServiceInvalidExecutionSettingsError(f"Invalid image size: {size.width}x{size.height}.")

        return self

    def prepare_settings_dict(self, **kwargs) -> dict[str, Any]:
        """Prepare the settings dictionary for the OpenAI API."""
        settings_dict = super().prepare_settings_dict(**kwargs)

        if self.size is not None:
            settings_dict["size"] = str(self.size)

        return settings_dict

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Use one of the five valid size tuples: (256,256), (512,512), (1024,1024), (1792,1024), (1024,1792).
  2. For DALL-E 3 specifically, use only 1024x1024, 1792x1024, or 1024x1792.
  3. Check VALID_IMAGE_SIZES before setting — or omit size and let the service use its default.

Example fix

// before
settings = OpenAITextToImageExecutionSettings(size=ImageSize(width=800, height=600))
// after
settings = OpenAITextToImageExecutionSettings(size=ImageSize(width=1024, height=1024))
Defensive patterns

Strategy: validation

Validate before calling

VALID_IMAGE_SIZES = {(256, 256), (512, 512), (1024, 1024), (1792, 1024), (1024, 1792)}

def validate_image_size(width: int, height: int) -> None:
    if (width, height) not in VALID_IMAGE_SIZES:
        raise ValueError(
            f'Invalid image size: {width}x{height}. Valid sizes: {sorted(VALID_IMAGE_SIZES)}'
        )

Type guard

def is_valid_image_size(width: int, height: int) -> bool:
    return (width, height) in VALID_IMAGE_SIZES

Try / catch

from semantic_kernel.exceptions import ServiceInvalidExecutionSettingsError

try:
    settings = OpenAITextToImageExecutionSettings(size=ImageSize(width=w, height=h))
except ServiceInvalidExecutionSettingsError as e:
    # snap to nearest valid size
    settings = OpenAITextToImageExecutionSettings(size=ImageSize(width=1024, height=1024))

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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