{"record":{"id":"996640cad064227b","repo":"siyuan-note/siyuan","slug":"unsupported-generated-image-type-s","errorCode":null,"errorMessage":"unsupported generated image type: %s","messagePattern":"unsupported generated image type: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":852,"sourceCode":"\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\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...)}","sourceCodeStart":834,"sourceCodeEnd":870,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai.go#L834-L870","documentation":"ValidateGeneratedImage in kernel/util/openai.go throws this when mimetype.Detect classifies the generated bytes as something other than image/png, image/jpeg, or image/webp. The detected MIME string is interpolated into the message, so it usually reveals what was actually received (e.g. text/html or application/json).","triggerScenarios":"The byte payload from the image API has magic bytes that do not match png/jpeg/webp — most commonly an HTML error page, a JSON error envelope, or SVG/HEIC/AVIF output stored as bytes.","commonSituations":"The API returned an error page instead of an image (auth failure, rate limit) and the caller passed the body straight to validation; the provider switched to an unsupported format (AVIF/SVG); a download URL redirected to an HTML login page.","solutions":["Read the detected type in the message — if it is text/html or application/json, inspect that body for the real API error (auth, quota, invalid request)","Confirm the model/endpoint actually returns png/jpeg/webp and switch the output_format setting if it defaults to another format","Re-check the API key and request parameters; error payloads often masquerade as image data","Capture the response body and retry after fixing the request"],"exampleFix":"// before: validating error bodies as images\nimgData, _ := io.ReadAll(httpResp.Body)\nmime, ext, err := ValidateGeneratedImage(imgData)\n// after: check HTTP status before treating body as an image\nif httpResp.StatusCode != 200 {\n    return fmt.Errorf(\"image API error: %s\", httpResp.Status)\n}\nimgData, _ := io.ReadAll(httpResp.Body)\nmime, ext, err := ValidateGeneratedImage(imgData)","handlingStrategy":"validation","validationCode":"head, _ := io.ReadAll(io.LimitReader(r, 512))\nkind := mimetype.Detect(head).String()\nif kind != \"image/png\" && kind != \"image/jpeg\" && kind != \"image/webp\" {\n    return fmt.Errorf(\"expected image, got %s — inspect body for API error\", kind)\n}","typeGuard":null,"tryCatchPattern":"mime, ext, err := util.ValidateGeneratedImage(data)\nif err != nil && strings.HasPrefix(err.Error(), \"unsupported generated image type:\") {\n    // dump the first bytes — likely an HTML/JSON error payload\n    log.Printf(\"payload head: %q\", data[:min(256, len(data))])\n    return err\n}","preventionTips":["Always check HTTP status before treating a response body as image bytes","Sniff magic bytes before saving generated images","Pin/verify the API's output format setting (avoid AVIF/SVG defaults)","Inspect non-image payloads for the real upstream error (auth, quota)"],"tags":["image","ai","unsupported-format","content-type"],"backgroundTag":"unsupported-enum-value","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"}