siyuan-note/siyuan · error
prepared image exceeds size limit
Error message
prepared image exceeds size limit: %d bytes
What it means
Thrown at kernel/util/openai.go:824 after the image is converted to JPEG at quality 92: if maxBytes is positive and the encoded JPEG still exceeds that byte budget, preparation fails with this message. The %d is the byte limit from configuration, not the actual encoded size.
Solutions
- Increase the configured byte limit for AI image input
- Lower the resize limits (maxEdge / maxPixels) so images shrink more aggressively before encoding
- Pre-compress the image externally (e.g. quality 60 JPEG) and resend
- Check what maxBytes value appears in the message and compare against the actual file size
Example fix
// before: no downscale, tight byte cap img, err := prepareImage(bigPhoto, "image/jpeg", 0, 0, 100*1024) // after: force downscale so re-encode fits the cap img, err := prepareImage(bigPhoto, "image/jpeg", 2048*2048, 1568, 100*1024)
Defensive patterns
Strategy: validation
Validate before calling
cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))
if maxBytes > 0 {
est := cfg.Width * cfg.Height * 3 / 10 // rough JPEG bytes estimate
if est > maxBytes { return errors.New("image will exceed byte limit; downscale first") }
} Prevention
- Set maxBytes realistically (e.g. 4-5 MB for vision APIs) rather than very small values
- Always pair a byte cap with maxEdge/maxPixels so images actually shrink before encoding
- Pre-compress large photos externally before sending
- Check the limit value printed in the message against your config expectations
When it happens
Trigger: maxBytes is set and even the quality-92 JPEG re-encode of the (possibly resized) image is larger than maxBytes — typically huge photos where the pixel budget or max-edge resize was disabled or set too high.
Common situations: Users configure a very small byte limit (e.g. 100 KB) while sending large camera photos; callers pass maxBytes=0 for other paths but a strict limit here; resizing limits (maxEdge/maxPixels) set to 0 disable downsampling so nothing shrinks the payload.
Understand the failure class
Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.
Related errors
- generated image exceeds size limit
- image exceeds pixel limit
- BASE64_IMAGE_SIZE_LIMIT
- decode image failed
- encode image failed
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/58bd30911a538665.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/openai.go:824
if err = png.Encode(&output, decoded); err != nil {
return PreparedImage{}, errors.New("encode image failed: " + err.Error())
}
if maxBytes <= 0 || output.Len() <= maxBytes {
return PreparedImage{
Data: output.Bytes(),
MIMEType: "image/png",
Width: bounds.Dx(),
Height: bounds.Dy(),
SourceSize: len(data),
}, nil
}
}
var output bytes.Buffer
if err = jpeg.Encode(&output, decoded, &jpeg.Options{Quality: 92}); err != nil {
return PreparedImage{}, errors.New("encode image failed: " + err.Error())
}
if maxBytes > 0 && output.Len() > maxBytes {
return PreparedImage{}, fmt.Errorf("prepared image exceeds size limit: %d bytes", maxBytes)
}
return PreparedImage{
Data: output.Bytes(),
MIMEType: "image/jpeg",
Width: bounds.Dx(),
Height: bounds.Dy(),
SourceSize: len(data),
}, nil
}
// ValidateGeneratedImage 校验生成图片的格式、尺寸和体积。
func ValidateGeneratedImage(data []byte) (mimeType, extension string, err error) {
if len(data) == 0 {
return "", "", errors.New("generated image is empty")
}
if len(data) > maxGeneratedImageBytes {
return "", "", errors.New("generated image exceeds size limit")
}View on GitHub (pinned to 9f775e8a12)