{"record":{"id":"b21186ee8508e8b9","repo":"siyuan-note/siyuan","slug":"generated-image-exceeds-size-limit","errorCode":null,"errorMessage":"generated image exceeds size limit","messagePattern":"generated image exceeds size limit","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":701,"sourceCode":"\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}\n\tmimeType = mimetype.Detect(data).String()\n\tswitch mimeType {\n\tcase \"image/png\":\n\t\textension = \".png\"\n\tcase \"image/jpeg\":\n\t\textension = \".jpg\"\n\tcase \"image/webp\":\n\t\textension = \".webp\"\n\tdefault:\n\t\treturn \"\", \"\", fmt.Errorf(\"unsupported generated image type: %s\", mimeType)\n\t}\n\tconfig, _, decodeErr := image.DecodeConfig(bytes.NewReader(data))\n\tif decodeErr != nil || config.Width < 1 || config.Height < 1 || config.Width > 16384 || config.Height > 16384 ||\n\t\tint64(config.Width)*int64(config.Height) > maxGeneratedImagePixels {\n\t\treturn \"\", \"\", errors.New(\"generated image is invalid\")\n\t}\n\treturn mimeType, extension, nil","sourceCodeStart":683,"sourceCodeEnd":719,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/util/openai.go#L683-L719","documentation":"Thrown by ValidateGeneratedImage (kernel/util/openai.go:701) when len(data) exceeds maxGeneratedImageBytes, which is 50 * 1024 * 1024 (openai.go:51). It is a hard ceiling on a single generated image to protect kernel memory and disk. The check fires after the empty-bytes guard and before MIME detection.","triggerScenarios":"Calling ValidateGeneratedImage with a buffer > 50 MiB. Typically reached after downloading a URL-served image or decoding a large base64 PNG returned by dall-e or a compatible image model.","commonSituations":"Requesting very large size/quality (e.g. 1024x1024 high-quality PNG, or 4K via newer models); model returning an uncompressed PNG; misconfigured proxy that re-encodes at higher bitrate; adversarial/buggy provider inflating output.","solutions":["Lower the requested Size / Quality in GenerateImageRequest so the model emits a smaller payload.","Pre-check the buffer length before calling ValidateGeneratedImage and reject early with a domain-clear message.","If the model genuinely must emit large images, re-encode/shrink (jpeg quality, downscale) on your side before validation.","Verify no middleware (proxy, gateway) is re-encoding the response to a larger format."],"exampleFix":"// before\nmimeType, ext, err := ValidateGeneratedImage(data)\n\n// after\nconst limit = 50 * 1024 * 1024\nif len(data) > limit {\n    return GeneratedImage{}, fmt.Errorf(\"image payload %d bytes exceeds %d limit; lower size/quality\", len(data), limit)\n}\nmimeType, ext, err := ValidateGeneratedImage(data)","handlingStrategy":"validation","validationCode":"const imageLimit = 50 * 1024 * 1024 // matches maxGeneratedImageBytes\nif len(data) > imageLimit {\n    return fmt.Errorf(\"image %d bytes over %d limit; reduce size/quality\", len(data), imageLimit)\n}","typeGuard":null,"tryCatchPattern":"if _, _, err := ValidateGeneratedImage(data); err != nil {\n    if strings.Contains(err.Error(), \"exceeds size limit\") {\n        // lower Size/Quality, or downscale data, then retry\n    }\n    return err\n}","preventionTips":["Default to moderate Size values (e.g. 1024x1024) unless the model truly needs larger.","Pre-check len(data) against 50 MiB before calling ValidateGeneratedImage for a clearer error.","If you control the source encoder, cap its output bitrate."],"tags":["openai","image-generation","validation","size-limit","go"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}