LykosAI/StabilityMatrix · error · ImageGenerationException
Image generation failed
Error message
Image generation failed
What it means
When the provider's GenerateAsync call returns a response with IsSuccess == false, the service persists the (failed-generation-updated) conversation and throws ImageGenerationException carrying the provider's ErrorMessage (defaulting to 'Image generation failed' when the provider gave none), plus DetailJson and ErrorCode from the response. It means the backend image generation itself failed — not a local argument problem — and the exception is intended for the caller/UI to surface.
Solutions
- Inspect the thrown ImageGenerationException.ErrorCode and DetailJson — they contain the provider's actual error code and detail payload.
- Check the provider API key / login status and renew it if expired (many errors here are auth failures).
- Retry after a delay if ErrorCode indicates rate limiting or a transient provider outage.
- Validate prompt and attached images against the provider's content policy; remove or rephrase rejected content.
- Review providerOptions — remove or correct options unsupported by this provider.
Example fix
// before
try
{
await chatService.SendMessageAsync(convId, providerId, prompt);
}
catch (ImageGenerationException) { } // swallowed, no diagnosis
// after
try
{
await chatService.SendMessageAsync(convId, providerId, prompt);
}
catch (ImageGenerationException ex)
{
logger.LogError(ex, "Generation failed: code={Code} detail={Detail}", ex.ErrorCode, ex.DetailJson);
if (IsTransient(ex.ErrorCode))
{
await Task.Delay(TimeSpan.FromSeconds(5));
await chatService.RetryGenerationAsync(convId, providerId);
}
} Defensive patterns
Strategy: try-catch
Validate before calling
// Validate inputs before calling the provider
if (string.IsNullOrWhiteSpace(textPrompt) && (imagePaths is null || imagePaths.Count == 0))
throw new ArgumentException("Provide a text prompt and/or at least one image");
// Ensure an API key / login is configured for the provider
if (!providerService.IsApiKeyConfigured(providerId))
throw new InvalidOperationException($"No API key configured for provider {providerId}"); Type guard
bool GenerationSucceeded(ImageGenerationResponse r) => r is { IsSuccess: true, Images.Count: > 0 }; Try / catch
try
{
await chatService.SendMessageAsync(conversationId, providerId, prompt, imagePaths);
}
catch (ImageGenerationException ex)
{
switch (ex.ErrorCode)
{
case "rate_limited": await Task.Delay(TimeSpan.FromSeconds(10)); goto retry;
case "auth_failed": await promptForReloginAsync(); break;
default: ui.ShowNotification($"Generation failed: {ex.Message}\n{ex.DetailJson}"); break;
}
} Prevention
- Keep provider API keys valid and refreshed
- Pre-validate prompts/images against provider content policies
- Only pass providerOptions keys documented for the chosen provider
- Inspect ErrorCode/DetailJson on every failure to classify transient vs permanent errors
- Back off and retry on rate-limit style error codes
When it happens
Trigger: Calling SendMessageAsync (or RetryGenerationAsync) where provider.GenerateAsync returns an unsuccessful response: provider-side errors such as invalid API key, safety/content policy rejection, model unavailable, rate limiting, malformed provider options, or empty prompt with no images — with response.ErrorMessage null so the fallback message is thrown.
Common situations: Expired or missing API key; prompt rejected by safety filters; provider outage or rate limit; providerOptions dict containing invalid keys/values for the chosen provider; model deprecation on the backend.
Related errors
- GetUserAccount did not contain an id
- CivArchive list page was missing pageProps
- Relative URL is required
- CivArchive detail page was missing pageProps
- CivArchive detail page was missing model data
AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12).
Data as JSON: /api/errors/a9fb9a737b8506f8.
Report an issue: GitHub.
Appendix: source
Thrown at StabilityMatrix.Core/Services/ImageGeneration/ImageGenerationChatService.cs:711
);
var response = await provider.GenerateAsync(request, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccess)
{
logger.LogError("Image generation failed: {ErrorMessage}", response.ErrorMessage);
// Don't save error messages to the database - let the caller handle the error via UI
// Update conversation timestamp and provider
var errorUpdatedConversation = conversation with
{
ProviderId = providerId,
UpdatedAt = DateTime.UtcNow,
};
await database.Conversations.UpdateAsync(errorUpdatedConversation).ConfigureAwait(false);
// Throw exception so caller can handle it appropriately (show notification, etc.)
throw new ImageGenerationException(response.ErrorMessage ?? "Image generation failed")
{
DetailJson = response.ErrorDetailJson,
ErrorCode = response.ErrorCode,
};
}
// Save generated images
List<string>? savedImagePaths = null;
if (response.Images?.Count > 0)
{
progress?.Report(
new ImageGenerationProgress(
ProviderId: providerId,
PromptId: null,
Value: null,
Maximum: null,
RunningNode: null,
Stage: "Saving image(s)..."View on GitHub (pinned to af93d6ef57)