{"record":{"id":"aebc3098ba5a965f","repo":"siyuan-note/siyuan","slug":"decode-image-failed","errorCode":null,"errorMessage":"decode image failed: ","messagePattern":"decode image failed: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":738,"sourceCode":"\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) {\n\t\treturn PreparedImage{}, fmt.Errorf(\"image exceeds pixel limit: %d\", maxPixels)\n\t}\n\n\tdecoded, err := imaging.Decode(bytes.NewReader(data), imaging.AutoOrientation(true))\n\tif err != nil {\n\t\treturn PreparedImage{}, errors.New(\"decode image failed: \" + err.Error())\n\t}\n\tbounds := decoded.Bounds()\n\tneedsResize := maxEdge > 0 && (bounds.Dx() > maxEdge || bounds.Dy() > maxEdge)\n\tif !needsResize && bounds.Dx() == config.Width && bounds.Dy() == config.Height && mimeType != \"image/gif\" {\n\t\treturn PreparedImage{\n\t\t\tData:       data,\n\t\t\tMIMEType:   mimeType,\n\t\t\tWidth:      bounds.Dx(),\n\t\t\tHeight:     bounds.Dy(),\n\t\t\tSourceSize: len(data),\n\t\t}, nil\n\t}\n\tif needsResize {\n\t\tdecoded = imaging.Fit(decoded, maxEdge, maxEdge, imaging.Lanczos)\n\t}\n\tbounds = decoded.Bounds()\n\tif mimeType == \"image/png\" || mimeType == \"image/webp\" {\n\t\tvar output bytes.Buffer","sourceCodeStart":720,"sourceCodeEnd":756,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/afa823b6b4e4f183511e0bc0a3be93caa94c7c97/kernel/util/openai.go#L720-L756","documentation":"Thrown by util.PrepareModelImage (kernel/util/openai.go:738) when full-pixel decoding via imaging.Decode (with AutoOrientation) fails even though image.DecodeConfig succeeded on the same bytes. That means the header parsed but the pixel data or EXIF orientation chunk is unreadable, or the body is truncated after a valid header. The imaging error text is appended.","triggerScenarios":"File truncated mid-body (valid header, cut IDAT/JPEG scan); corrupt pixel stream; EXIF orientation tag malformed so AutoOrientation fails; rare decoder disagreement between header parse and full decode.","commonSituations":"Partial uploads/syncs, assets written while still being encoded by another process, images that open in lenient viewers but fail strict Go decoders.","solutions":["Re-export or re-download the source image (the bytes are corrupt past the header)","Round-trip through a tolerant encoder (magick in.jpg out.jpg) to rebuild the pixel stream","In batch flows, catch this error and skip the asset instead of aborting","If reproducible with one file, verify with an external decoder to confirm the asset itself is bad"],"exampleFix":"// before\nprepared, err := util.PrepareModelImage(data, maxBytes, maxPixels, maxEdge)\nif err != nil { return err }\n\n// after\nprepared, err := util.PrepareModelImage(data, maxBytes, maxPixels, maxEdge)\nif err != nil && strings.HasPrefix(err.Error(), \"decode image failed\") {\n    data = reencodeExternally(data) // rebuild pixel stream\n    prepared, err = util.PrepareModelImage(data, maxBytes, maxPixels, maxEdge)\n}","handlingStrategy":"validation","validationCode":"if _, err := imaging.Decode(bytes.NewReader(data), imaging.AutoOrientation(true)); err != nil {\n    // full decode will fail too: re-export the source file now\n    data = reencodeExternally(data)\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.HasPrefix(err.Error(), \"decode image failed:\") {\n    // header parsed but pixels are corrupt: re-export or skip; retrying unchanged cannot help\n}","preventionTips":["Avoid reading assets while another process is still writing them","Re-sync corrupted assets from the original source","Skip undecodable assets per-image so one bad file does not abort batch analysis"],"tags":["go","image","corruption","decode","multimodal"],"backgroundTag":"image-decode-failed","analyzedSha":"afa823b6b4e4f183511e0bc0a3be93caa94c7c97","analyzedAt":"2026-08-18T17:04:10.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}