siyuan-note/siyuan · error

image data is empty

Error message

image data is empty

What it means

PrepareModelImage in kernel/util/openai.go rejects empty image byte slices up front. Multimodal model upload paths call this to validate and scale images before sending; zero-length data can never be a valid image. The error is returned immediately as PreparedImage{} with a descriptive message.

Solutions

  1. Check the source asset exists and has non-zero size before invoking the image pipeline
  2. Re-download or re-copy the asset; an empty file on disk is itself corrupted
  3. Verify the path resolution that produced the []byte — a wrong path often yields an empty read without failing loudly

Example fix

// before
data, _ := os.ReadFile(assetPath)
prepared, err := PrepareModelImage(data, maxBytes, maxPixels, maxEdge)
// after
data, err := os.ReadFile(assetPath)
if err != nil || len(data) == 0 {
    return fmt.Errorf("asset %s is missing or empty", assetPath)
}
prepared, err := PrepareModelImage(data, maxBytes, maxPixels, maxEdge)
Defensive patterns

Strategy: validation

Validate before calling

if data == nil || len(data) == 0 { return errors.New("image asset is empty; re-check the file") }

Type guard

func hasImageBytes(b []byte) bool { return len(b) > 0 }

Try / catch

prepared, err := PrepareModelImage(data, maxBytes, maxPixels, maxEdge)
if err != nil && strings.Contains(err.Error(), "empty") {
    return reFetchAssetAndRetry()
}

Prevention

When it happens

Trigger: Calling PrepareModelImage (or the multimodal attach flow that uses it) with a nil or empty []byte — e.g. an asset file that failed to read, an empty download, or a zero-length attachment in an AI chat.

Common situations: Asset file deleted from disk after being referenced in the chat; interrupted download cached as an empty file; reading an asset with the wrong path; clipboard paste producing an empty buffer.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at kernel/util/openai.go:763

		err = fmt.Errorf("rerank returned %d indices for %d documents", len(indices), len(documents))
		return
	}
	seen := make(map[int]bool, len(indices))
	for _, index := range indices {
		if seen[index] {
			err = fmt.Errorf("rerank returned duplicate index %d", index)
			return
		}
		seen[index] = true
	}
	matched = true
	return
}

// PrepareModelImage 校验并按需缩放图片,尽量保留多模态模型支持的原始格式和图片质量。
func PrepareModelImage(data []byte, maxBytes, maxPixels, maxEdge int) (PreparedImage, error) {
	if len(data) == 0 {
		return PreparedImage{}, errors.New("image data is empty")
	}
	if maxBytes > 0 && len(data) > maxBytes {
		return PreparedImage{}, fmt.Errorf("image exceeds size limit: %d bytes", maxBytes)
	}
	mimeType := mimetype.Detect(data).String()
	if strings.Contains(mimeType, "svg") || bytes.Contains(bytes.ToLower(data[:min(len(data), 512)]), []byte("<svg")) {
		return PreparedImage{}, errors.New("SVG images are not accepted by multimodal models")
	}
	switch mimeType {
	case "image/gif", "image/jpeg", "image/png", "image/webp":
	default:
		return PreparedImage{}, fmt.Errorf("unsupported image type: %s", mimeType)
	}
	config, _, err := image.DecodeConfig(bytes.NewReader(data))
	if err != nil {
		return PreparedImage{}, errors.New("unsupported or invalid image: " + err.Error())
	}
	if config.Width < 1 || config.Height < 1 || maxPixels > 0 && int64(config.Width)*int64(config.Height) > int64(maxPixels) {

View on GitHub (pinned to 9f775e8a12)