{"record":{"id":"d529f35f294a6d27","repo":"siyuan-note/siyuan","slug":"generated-image-is-invalid","errorCode":null,"errorMessage":"generated image is invalid","messagePattern":"generated image is invalid","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":857,"sourceCode":"\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\n}\n\nfunc NewOpenAIImageAdapter(apiKey, apiBaseURL, model string, timeout int, headers ...map[string]string) *OpenAIImageAdapter {\n\tif timeout < 1 {\n\t\ttimeout = 30\n\t}\n\tclient := NewOpenAIClientWithModel(apiKey, apiBaseURL, model, headers...)\n\tif isMiniMaxImageEndpoint(apiBaseURL) {\n\t\tconfig := openai.DefaultConfig(apiKey)\n\t\tconfig.BaseURL = apiBaseURL\n\t\tconfig.HTTPClient = &miniMaxImageTransport{base: newAIProviderHTTPClient(apiBaseURL, headers...)}\n\t\tclient = openai.NewClientWithConfig(config)\n\t}\n\treturn &OpenAIImageAdapter{\n\t\tclient:  client,\n\t\tmodel:   model,","sourceCodeStart":839,"sourceCodeEnd":875,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai.go#L839-L875","documentation":"ValidateGeneratedImage in kernel/util/openai.go throws this when the generated image passes format and byte-size checks but fails structural validation: image.DecodeConfig errors, width or height below 1, dimensions above 16384, or width*height exceeding maxGeneratedImagePixels. The message is constant, so the exact violated condition must be inferred from the image.","triggerScenarios":"Generated bytes are a supported format but DecodeConfig cannot parse them (corrupt/truncated), or dimensions are 0, exceed 16384 on either axis, or the pixel count exceeds maxGeneratedImagePixels.","commonSituations":"Truncated downloads of large generated PNGs; extreme generation sizes requested from the API; mislabeled/patched payloads that pass magic-byte detection but are not decodable images.","solutions":["Re-generate the image — corruption/truncation is usually transient","Verify the requested generation size stays within 16384×16384 and the configured pixel budget","Check whether the file was fully written/downloaded (compare byte counts) before validation","If requesting custom dimensions from the API, clamp them to the supported range first"],"exampleFix":"// before: requesting an unclamped custom size\nreq := imageRequest{Size: fmt.Sprintf(\"%dx%d\", w, h)}\n// after: clamp to the validator's limits\nif w > 16384 { w = 16384 }\nif h > 16384 { h = 16384 }\nreq := imageRequest{Size: fmt.Sprintf(\"%dx%d\", w, h)}","handlingStrategy":"validation","validationCode":"cfg, _, err := image.DecodeConfig(bytes.NewReader(data))\nif err != nil { return err }\nif cfg.Width < 1 || cfg.Height < 1 { return errors.New(\"degenerate generated image\") }\nif cfg.Width > 16384 || cfg.Height > 16384 { return errors.New(\"generated image too large\") }\nif int64(cfg.Width)*int64(cfg.Height) > 4096*4096 { return errors.New(\"pixel budget exceeded\") }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp requested generation dimensions to the 16384-per-axis limit","Verify downloads complete (byte counts) before validation","Run DecodeConfig early to catch corrupt payloads before deeper processing","Keep the pixel budget consistent between request parameters and validation config"],"tags":["image","ai","dimensions","validation","generated-content"],"backgroundTag":"schema-validation-failed","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"}