{"record":{"id":"931951e2942c8885","repo":"siyuan-note/siyuan","slug":"image-data-is-empty","errorCode":null,"errorMessage":"image data is empty","messagePattern":"image data is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":623,"sourceCode":"\t\terr = fmt.Errorf(\"rerank returned %d indices for %d documents\", len(indices), len(documents))\n\t\treturn\n\t}\n\tseen := make(map[int]bool, len(indices))\n\tfor _, index := range indices {\n\t\tif seen[index] {\n\t\t\terr = fmt.Errorf(\"rerank returned duplicate index %d\", index)\n\t\t\treturn\n\t\t}\n\t\tseen[index] = true\n\t}\n\tmatched = true\n\treturn\n}\n\n// PrepareModelImage 校验并按需缩放图片，尽量保留多模态模型支持的原始格式和图片质量。\nfunc PrepareModelImage(data []byte, maxBytes, maxPixels, maxEdge int) (PreparedImage, error) {\n\tif len(data) == 0 {\n\t\treturn PreparedImage{}, errors.New(\"image data is empty\")\n\t}\n\tif maxBytes > 0 && len(data) > maxBytes {\n\t\treturn PreparedImage{}, fmt.Errorf(\"image exceeds size limit: %d bytes\", maxBytes)\n\t}\n\tmimeType := mimetype.Detect(data).String()\n\tif strings.Contains(mimeType, \"svg\") || bytes.Contains(bytes.ToLower(data[:min(len(data), 512)]), []byte(\"<svg\")) {\n\t\treturn PreparedImage{}, errors.New(\"SVG images are not accepted by multimodal models\")\n\t}\n\tswitch mimeType {\n\tcase \"image/gif\", \"image/jpeg\", \"image/png\", \"image/webp\":\n\tdefault:\n\t\treturn PreparedImage{}, fmt.Errorf(\"unsupported image type: %s\", mimeType)\n\t}\n\tconfig, _, err := image.DecodeConfig(bytes.NewReader(data))\n\tif err != nil {\n\t\treturn PreparedImage{}, errors.New(\"unsupported or invalid image: \" + err.Error())\n\t}\n\tif config.Width < 1 || config.Height < 1 || maxPixels > 0 && int64(config.Width)*int64(config.Height) > int64(maxPixels) {","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/util/openai.go#L605-L641","documentation":"PrepareModelImage's first guard rejects nil/zero-length input before any detection. It means the caller passed an empty asset (failed download, zero-byte file, or nil pointer) to a multimodal model call.","triggerScenarios":"Calling a vision/multimodal model with a block whose image asset is missing or zero bytes; passing the result of a failed read/download that was swallowed.","commonSituations":"Asset path wrong so the read returns empty; network image download failed silently; PDF/image attachment not yet downloaded.","solutions":["Verify the source asset exists and is non-empty before calling.","Check that the upstream download/read error is not being swallowed.","Skip empty attachments instead of forwarding them to the model."],"exampleFix":"// before\nimg, err := util.PrepareModelImage(maybeEmptyBytes, maxB, maxP, maxE)\n\n// after\nif len(raw) == 0 { return errors.New(\"no image content\") }\nimg, err := util.PrepareModelImage(raw, maxB, maxP, maxE)","handlingStrategy":"validation","validationCode":"if len(data) == 0 { return errors.New(\"image is empty\") }\nimg, err := util.PrepareModelImage(data, maxBytes, maxPixels, maxEdge)","typeGuard":"func HasImageContent(data []byte) bool { return len(data) > 0 }","tryCatchPattern":null,"preventionTips":["Check len(data) before preparing","Verify asset file size > 0 before reading"],"tags":["ai","multimodal","image","go","validation"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}