siyuan-note/siyuan · error

generated image is invalid

Error message

generated image is invalid

What it means

ValidateGeneratedImage in kernel/util/openai.go throws this when the generated image passes format and byte-size checks but fails structural validation: image.DecodeConfig errors, width or height below 1, dimensions above 16384, or width*height exceeding maxGeneratedImagePixels. The message is constant, so the exact violated condition must be inferred from the image.

Solutions

  1. Re-generate the image — corruption/truncation is usually transient
  2. Verify the requested generation size stays within 16384×16384 and the configured pixel budget
  3. Check whether the file was fully written/downloaded (compare byte counts) before validation
  4. If requesting custom dimensions from the API, clamp them to the supported range first

Example fix

// before: requesting an unclamped custom size
req := imageRequest{Size: fmt.Sprintf("%dx%d", w, h)}
// after: clamp to the validator's limits
if w > 16384 { w = 16384 }
if h > 16384 { h = 16384 }
req := imageRequest{Size: fmt.Sprintf("%dx%d", w, h)}
Defensive patterns

Strategy: validation

Validate before calling

cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
if err != nil { return err }
if cfg.Width < 1 || cfg.Height < 1 { return errors.New("degenerate generated image") }
if cfg.Width > 16384 || cfg.Height > 16384 { return errors.New("generated image too large") }
if int64(cfg.Width)*int64(cfg.Height) > 4096*4096 { return errors.New("pixel budget exceeded") }

Prevention

When it happens

Trigger: Generated bytes are a supported format but DecodeConfig cannot parse them (corrupt/truncated), or dimensions are 0, exceed 16384 on either axis, or the pixel count exceeds maxGeneratedImagePixels.

Common situations: Truncated downloads of large generated PNGs; extreme generation sizes requested from the API; mislabeled/patched payloads that pass magic-byte detection but are not decodable images.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/d529f35f294a6d27. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/openai.go:857

	}
	if len(data) > maxGeneratedImageBytes {
		return "", "", errors.New("generated image exceeds size limit")
	}
	mimeType = mimetype.Detect(data).String()
	switch mimeType {
	case "image/png":
		extension = ".png"
	case "image/jpeg":
		extension = ".jpg"
	case "image/webp":
		extension = ".webp"
	default:
		return "", "", fmt.Errorf("unsupported generated image type: %s", mimeType)
	}
	config, _, decodeErr := image.DecodeConfig(bytes.NewReader(data))
	if decodeErr != nil || config.Width < 1 || config.Height < 1 || config.Width > 16384 || config.Height > 16384 ||
		int64(config.Width)*int64(config.Height) > maxGeneratedImagePixels {
		return "", "", errors.New("generated image is invalid")
	}
	return mimeType, extension, nil
}

func NewOpenAIImageAdapter(apiKey, apiBaseURL, model string, timeout int, headers ...map[string]string) *OpenAIImageAdapter {
	if timeout < 1 {
		timeout = 30
	}
	client := NewOpenAIClientWithModel(apiKey, apiBaseURL, model, headers...)
	if isMiniMaxImageEndpoint(apiBaseURL) {
		config := openai.DefaultConfig(apiKey)
		config.BaseURL = apiBaseURL
		config.HTTPClient = &miniMaxImageTransport{base: newAIProviderHTTPClient(apiBaseURL, headers...)}
		client = openai.NewClientWithConfig(config)
	}
	return &OpenAIImageAdapter{
		client:  client,
		model:   model,

View on GitHub (pinned to 9f775e8a12)