{"record":{"id":"8341d548e8c310ea","repo":"plandex-ai/plandex","slug":"failed-to-decode-base64-image-data-w","errorCode":null,"errorMessage":"failed to decode base64 image data: %w","messagePattern":"failed to decode base64 image data: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/shared/images.go","lineNumber":27,"sourceCode":"\t\"log\"\n\t\"math\"\n\t\"path/filepath\"\n\t\"strings\"\n\n\t\"github.com/sashabaranov/go-openai\"\n\n\t_ \"image/gif\"\n\t_ \"image/jpeg\"\n\t_ \"image/png\"\n\n\t_ \"golang.org/x/image/webp\"\n)\n\nfunc GetImageTokens(base64Image string, detail openai.ImageURLDetail) (int, error) {\n\timageData, err := base64.StdEncoding.DecodeString(base64Image)\n\tif err != nil {\n\t\tlog.Println(\"failed to decode base64 image data:\", err)\n\t\treturn 0, fmt.Errorf(\"failed to decode base64 image data: %w\", err)\n\t}\n\n\treturn GetImageTokensFromHeader(bytes.NewReader(imageData), detail, int64(len(imageData)))\n}\n\nfunc GetImageTokensFromHeader(reader io.Reader, detail openai.ImageURLDetail, maxBytes int64) (int, error) {\n\treader = io.LimitReader(reader, maxBytes)\n\timg, _, err := image.DecodeConfig(reader)\n\tif err != nil {\n\t\tlog.Println(\"failed to decode image config:\", err)\n\t\treturn 0, fmt.Errorf(\"failed to decode image config: %w\", err)\n\t}\n\n\twidth, height := img.Width, img.Height\n\n\tanthropicTokens := getAnthropicImageTokens(width, height)\n\tgoogleTokens := getGoogleImageTokens(width, height)\n\topenaiTokens := getOpenAIImageTokens(width, height, detail)","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/shared/images.go#L9-L45","documentation":"GetImageTokens first decodes its base64Image argument with base64.StdEncoding.DecodeString; if that fails (the string is not valid standard-alphabet base64) it wraps the error with this message. The function only accepts raw, unpadded-data standard base64 payloads, not data URIs.","triggerScenarios":"Calling shared.GetImageTokens with a data URI prefix like \"data:image/png;base64,\" still attached; whitespace/newlines inside the base64 string; URL-safe base64 (- and _) instead of standard (+ and /); a truncated or empty string.","commonSituations":"Storing images with data-URI prefixes in DB/JSON and passing them straight to GetImageTokens; base64 produced by URL-safe encoders (e.g. some JWT/web tooling); payloads mangled by line-wrapping in email or config files.","solutions":["Strip the \"data:image/...;base64,\" prefix before calling GetImageTokens.","Trim whitespace and newlines from the payload.","Convert URL-safe base64 to standard by replacing '-' with '+' and '_' with '/'.","Validate the string decodes (e.g. base64.StdEncoding.DecodeString in a check) before passing it in."],"exampleFix":"// before\ntokens, err := shared.GetImageTokens(dataURI, openai.ImageURLDetailAuto) // \"data:image/png;base64,iVBOR...\"\n// after\npayload := dataURI\nif i := strings.Index(payload, \",\"); i != -1 && strings.HasPrefix(payload, \"data:\") {\n    payload = payload[i+1:]\n}\npayload = strings.TrimSpace(payload)\ntokens, err := shared.GetImageTokens(payload, openai.ImageURLDetailAuto)","handlingStrategy":"validation","validationCode":"func isValidBase64Image(s string) bool {\n    s = strings.TrimSpace(s)\n    if i := strings.Index(s, \",\"); i != -1 && strings.HasPrefix(s, \"data:\") {\n        s = s[i+1:]\n    }\n    _, err := base64.StdEncoding.DecodeString(s)\n    return err == nil\n}","typeGuard":"func isDataURL(s string) bool {\n    return strings.HasPrefix(s, \"data:\")\n}","tryCatchPattern":"tokens, err := shared.GetImageTokens(payload, openai.ImageURLDetailAuto)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to decode base64 image data\") {\n        return fmt.Errorf(\"image payload is not valid standard base64: %w\", err)\n    }\n    return err\n}","preventionTips":["Strip data-URI prefixes at ingestion time, before storing images.","Standardize on base64.StdEncoding for all stored image payloads.","Trim whitespace/newlines from payloads before decoding.","Validate base64 with a decode round-trip in upload handlers."],"tags":["base64","encoding","go","image"],"backgroundTag":"invalid-base64","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}