microsoft/semantic-kernel · error · KernelException

The generated image has no valid content.

Error message

The generated image has no valid content.

What it means

After a successful image-generation HTTP response, the connector checks for both ImageUri and ImageBytes on the returned object. If both are null, it throws because there is no content to return as an ImageContent/Blob. This is a defensive check against an empty or malformed generation result.

Source

Thrown at dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.TextToImage.cs:59

        // The model is not required by the OpenAI API and defaults to the DALL-E 2 server-side - https://platform.openai.com/docs/api-reference/images/create#images-create-model.
        // However, considering that the model is required by the OpenAI SDK and the ModelId property is optional, it defaults to gpt-image-1 in the line below.
        targetModel = string.IsNullOrEmpty(targetModel) ? "gpt-image-1" : targetModel!;

        ClientResult<GeneratedImage> response = await RunRequestAsync(() => this.Client!.GetImageClient(targetModel).GenerateImageAsync(prompt, imageOptions, cancellationToken)).ConfigureAwait(false);
        var generatedImage = response.Value;

        if (generatedImage.ImageUri is not null)
        {
            return generatedImage.ImageUri.ToString();
        }

        if (generatedImage.ImageBytes is not null)
        {
            return $"data:image/png;base64,{Convert.ToBase64String(generatedImage.ImageBytes.ToArray())}";
        }

        throw new KernelException("The generated image has no valid content.");
    }

    /// <summary>
    /// Generates an image with the provided configuration.
    /// </summary>
    /// <param name="targetModel">Model identifier</param>
    /// <param name="input">The input text content to generate the image</param>
    /// <param name="executionSettings">Execution settings for the image generation</param>
    /// <param name="kernel">Kernel instance</param>
    /// <param name="cancellationToken">Cancellation token</param>
    /// <returns>List of image generated contents</returns>
    internal async Task<IReadOnlyList<ImageContent>> GetImageContentsAsync(
        string targetModel,
        TextContent input,
        PromptExecutionSettings? executionSettings = null,
        Kernel? kernel = null,
        CancellationToken cancellationToken = default)
    {

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Retry the request once — transient content-filter or backend issues can produce empty results.
  2. Check your requested response format; try switching between 'url' and 'b64_json' to match the deployment's capability.
  3. Simplify the prompt to avoid potential content-filter triggers.
  4. Inspect the raw API response body to confirm whether the fields are absent or present-but-empty.
  5. Verify the model deployment supports the DALL-E / image-generation endpoint you are calling.

Example fix

// before — format may not be supported by the deployment
var settings = new OpenAITextToImageExecutionSettings { ResponseFormat = "b64_json" };

// after — let the API choose, or switch to url
var settings = new OpenAITextToImageExecutionSettings();
Defensive patterns

Strategy: retry

Validate before calling

// No pre-call validation can prevent a server-side empty result.
// Instead, validate the response format setting matches deployment capabilities.
if (settings.ResponseFormat is string rf && rf.Equals("b64_json", StringComparison.OrdinalIgnoreCase))
{
    // Ensure deployment supports base64 responses; otherwise use url
}

Try / catch

for (int attempt = 0; attempt < 3; attempt++)
{
    try { return await imageService.GetImageContentsAsync(prompt, settings, kernel); }
    catch (KernelException ex) when (ex.Message.Contains("no valid content") && attempt < 2)
    {
        await Task.Delay(TimeSpan.FromSeconds(2 * (attempt + 1)));
    }
}
throw; // exhausted retries

Prevention

When it happens

Trigger: The image-generation API returns HTTP 200 with a response body that contains neither a URL nor base64 image data. Occurs when the requested response format doesn't match what the model returned, the API changed its response shape, or a content-filter blocked the image but still returned 200.

Common situations: Requesting response format 'b64_json' from a deployment/model that only returns URLs (or vice-versa). Azure OpenAI content filtering rejecting the prompt but returning an empty image object. API version skew between the SDK and the deployed model.

Related errors


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