{"record":{"id":"7f09eb89e1fa98c9","repo":"siyuan-note/siyuan","slug":"image-exceeds-size-limit-d-bytes","errorCode":null,"errorMessage":"image exceeds size limit: %d bytes","messagePattern":"image exceeds size limit: (.+?) bytes","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":766,"sourceCode":"\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) {\n\t\treturn PreparedImage{}, fmt.Errorf(\"image exceeds pixel limit: %d\", maxPixels)\n\t}\n","sourceCodeStart":748,"sourceCodeEnd":784,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai.go#L748-L784","documentation":"PrepareModelImage enforces an optional maxBytes cap and returns this error when the raw image is larger than allowed. The limit exists because multimodal providers reject or degrade oversized uploads; scaling happens later only for pixel/edge limits, so byte-size must pass this gate first. The error names the configured limit in bytes.","triggerScenarios":"Calling PrepareModelImage with data longer than maxBytes — e.g. attaching a multi-megabyte photo/scan to an AI chat when the configured size limit is smaller.","commonSituations":"Pasting screenshots from high-DPI displays; inserting camera photos; assets created before a stricter size limit was configured; provider-specific upload caps (commonly ~5-20MB).","solutions":["Compress or downscale the image before attaching (export as JPEG/WebP at lower quality or dimensions)","Raise the configured maxBytes limit if your provider accepts larger uploads","Convert PNG screenshots to JPEG — photographs in PNG are often 5-10x larger than needed","Crop unused regions of screenshots before inserting into the document"],"exampleFix":"// before\nprepared, err := PrepareModelImage(hugePNG, 5*1024*1024, maxPixels, maxEdge) // 12MB PNG\n// after\njpegData := encodeJPEG(downscale(hugePNG, 1600, 0.8)) // ~400KB\nprepared, err := PrepareModelImage(jpegData, 5*1024*1024, maxPixels, maxEdge)","handlingStrategy":"validation","validationCode":"if maxBytes > 0 && len(data) > maxBytes {\n    data = compressImage(data, maxBytes) // downscale/re-encode before calling\n}","typeGuard":null,"tryCatchPattern":"prepared, err := PrepareModelImage(data, maxBytes, maxPixels, maxEdge)\nif err != nil && strings.Contains(err.Error(), \"size limit\") {\n    prepared, err = PrepareModelImage(recompress(data, maxBytes/2), maxBytes, maxPixels, maxEdge)\n}","preventionTips":["Convert screenshots to JPEG/WebP instead of storing large PNGs","Know your provider's upload cap and set maxBytes just under it","Downscale high-DPI captures to ~1600px on the long edge before inserting"],"tags":["image","size-limit","multimodal"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}