{"record":{"id":"7e7ec959f7954ec6","repo":"wavetermdev/waveterm","slug":"invalid-data-url-format","errorCode":null,"errorMessage":"invalid data URL format","messagePattern":"invalid data URL format","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/aiusechat/anthropic/anthropic-convertmessage.go","lineNumber":378,"sourceCode":"\t\treturn nil, errors.New(\"file part missing mediaType\")\n\t}\n\n\t// Validate URL protocol - only allow data:, http:, https:\n\tif !strings.HasPrefix(p.URL, \"data:\") &&\n\t\t!strings.HasPrefix(p.URL, \"http://\") &&\n\t\t!strings.HasPrefix(p.URL, \"https://\") {\n\t\treturn nil, fmt.Errorf(\"unsupported URL protocol in file part: %s\", p.URL)\n\t}\n\n\t// Branch on mediaType first to determine block type and constraints\n\tswitch {\n\tcase strings.HasPrefix(p.MediaType, \"image/\"):\n\t\t// image/* (jpeg, png, gif, webp) → Anthropic image block\n\t\tif strings.HasPrefix(p.URL, \"data:\") {\n\t\t\t// Data URL → base64 source\n\t\t\tparts := strings.SplitN(p.URL, \",\", 2)\n\t\t\tif len(parts) != 2 {\n\t\t\t\treturn nil, errors.New(\"invalid data URL format\")\n\t\t\t}\n\t\t\treturn &anthropicMessageContentBlock{\n\t\t\t\tType: \"image\",\n\t\t\t\tSource: &anthropicSource{\n\t\t\t\t\tType:      \"base64\",\n\t\t\t\t\tData:      parts[1],\n\t\t\t\t\tMediaType: p.MediaType,\n\t\t\t\t},\n\t\t\t}, nil\n\t\t} else {\n\t\t\t// HTTP/HTTPS URL → url source (no media_type for image URLs)\n\t\t\treturn &anthropicMessageContentBlock{\n\t\t\t\tType: \"image\",\n\t\t\t\tSource: &anthropicSource{\n\t\t\t\t\tType: \"url\",\n\t\t\t\t\tURL:  p.URL,\n\t\t\t\t},\n\t\t\t}, nil","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/aiusechat/anthropic/anthropic-convertmessage.go#L360-L396","documentation":"For image/* media types, a data: URL must contain a comma separating the \"data:mediatype;base64\" header from the payload; SplitN(URL, \",\", 2) must yield 2 pieces. A data URL without a comma is malformed, so conversion fails with 'invalid data URL format' while building the Anthropic image block.","triggerScenarios":"Passing a file part with MediaType like \"image/png\" and URL starting with \"data:\" but missing the comma — e.g. \"data:image/png\" or \"data:image/png;base64\" with no \",<data>\" suffix.","commonSituations":"String-concatenation bugs building data URLs (forgot the comma); truncated URLs from clipboard/DB storage; URL-encoding or a middleware stripping part of the data URL.","solutions":["Build the data URL as \"data:<mediatype>;base64,<payload>\" including the comma.","Validate with a regex like ^data:[^,]+,[\\s\\S]+$ before sending.","If you only have raw bytes, base64-encode them and construct the full data URL client-side."],"exampleFix":"// before\nurl := \"data:image/png;base64\" // missing comma + data\n\n// after\nurl := \"data:image/png;base64,\" + base64.StdEncoding.EncodeToString(pngBytes)","handlingStrategy":"validation","validationCode":"var dataURLRe = regexp.MustCompile(`^data:[^,]+,[\\s\\S]+$`)\nif strings.HasPrefix(p.URL, \"data:\") && !dataURLRe.MatchString(p.URL) {\n    return fmt.Errorf(\"malformed data URL for image part\")\n}","typeGuard":"func isWellFormedImageDataURL(url string) bool {\n    return strings.HasPrefix(url, \"data:image/\") && strings.Contains(url, \",\")\n}","tryCatchPattern":"block, err := convertFileUIMessagePart(part)\nif err != nil && strings.Contains(err.Error(), \"invalid data URL format\") {\n    return fmt.Errorf(\"re-encode the image as data:<type>;base64,<data>: %w\", err)\n}","preventionTips":["Always build data URLs with a helper like \"data:\" + mt + \";base64,\" + b64.","Never hand-concatenate data URL strings inline.","Check that stored data URLs still contain a comma after retrieval."],"tags":["validation","data-url","message-conversion"],"backgroundTag":"malformed-data-url","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}