{"record":{"id":"58bd30911a538665","repo":"siyuan-note/siyuan","slug":"prepared-image-exceeds-size-limit-d-bytes","errorCode":null,"errorMessage":"prepared image exceeds size limit: %d bytes","messagePattern":"prepared image exceeds size limit: (.+?) bytes","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":824,"sourceCode":"\t\tif err = png.Encode(&output, decoded); err != nil {\n\t\t\treturn PreparedImage{}, errors.New(\"encode image failed: \" + err.Error())\n\t\t}\n\t\tif maxBytes <= 0 || output.Len() <= maxBytes {\n\t\t\treturn PreparedImage{\n\t\t\t\tData:       output.Bytes(),\n\t\t\t\tMIMEType:   \"image/png\",\n\t\t\t\tWidth:      bounds.Dx(),\n\t\t\t\tHeight:     bounds.Dy(),\n\t\t\t\tSourceSize: len(data),\n\t\t\t}, nil\n\t\t}\n\t}\n\tvar output bytes.Buffer\n\tif err = jpeg.Encode(&output, decoded, &jpeg.Options{Quality: 92}); err != nil {\n\t\treturn PreparedImage{}, errors.New(\"encode image failed: \" + err.Error())\n\t}\n\tif maxBytes > 0 && output.Len() > maxBytes {\n\t\treturn PreparedImage{}, fmt.Errorf(\"prepared image exceeds size limit: %d bytes\", maxBytes)\n\t}\n\treturn PreparedImage{\n\t\tData:       output.Bytes(),\n\t\tMIMEType:   \"image/jpeg\",\n\t\tWidth:      bounds.Dx(),\n\t\tHeight:     bounds.Dy(),\n\t\tSourceSize: len(data),\n\t}, nil\n}\n\n// ValidateGeneratedImage 校验生成图片的格式、尺寸和体积。\nfunc ValidateGeneratedImage(data []byte) (mimeType, extension string, err error) {\n\tif len(data) == 0 {\n\t\treturn \"\", \"\", errors.New(\"generated image is empty\")\n\t}\n\tif len(data) > maxGeneratedImageBytes {\n\t\treturn \"\", \"\", errors.New(\"generated image exceeds size limit\")\n\t}","sourceCodeStart":806,"sourceCodeEnd":842,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai.go#L806-L842","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before: no downscale, tight byte cap\nimg, err := prepareImage(bigPhoto, \"image/jpeg\", 0, 0, 100*1024)\n// after: force downscale so re-encode fits the cap\nimg, err := prepareImage(bigPhoto, \"image/jpeg\", 2048*2048, 1568, 100*1024)","handlingStrategy":"validation","validationCode":"cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))\nif maxBytes > 0 {\n    est := cfg.Width * cfg.Height * 3 / 10 // rough JPEG bytes estimate\n    if est > maxBytes { return errors.New(\"image will exceed byte limit; downscale first\") }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["image","size-limit","jpeg","ai"],"backgroundTag":"payload-too-large","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"}