{"record":{"id":"6e0eb72aef5e9dfb","repo":"can1357/oh-my-pi","slug":"mime-type-is-required-when-providing-raw-base64-da","errorCode":null,"errorMessage":"mime_type is required when providing raw base64 data.","messagePattern":"mime_type is required when providing raw base64 data\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/tools/image-gen.ts","lineNumber":386,"sourceCode":"}\n\nfunction resolveOpenRouterModel(model: string): string {\n\treturn model.includes(\"/\") ? model : `google/${model}`;\n}\n\nfunction toDataUrl(image: InlineImageData): string {\n\treturn `data:${image.mimeType};base64,${image.data}`;\n}\n\nasync function loadImageFromUrl(\n\timageUrl: string,\n\tfetchImpl: FetchImpl,\n\tsignal?: AbortSignal,\n): Promise<InlineImageData> {\n\tif (imageUrl.startsWith(\"data:\")) {\n\t\tconst normalized = normalizeDataUrl(imageUrl.trim());\n\t\tif (!normalized.mimeType) {\n\t\t\tthrow new Error(\"mime_type is required when providing raw base64 data.\");\n\t\t}\n\t\tif (!normalized.data) {\n\t\t\tthrow new Error(\"Image data is empty.\");\n\t\t}\n\t\treturn { data: normalized.data, mimeType: normalized.mimeType };\n\t}\n\n\tconst response = await fetchImpl(imageUrl, { signal });\n\tif (!response.ok) {\n\t\tconst rawText = await response.text();\n\t\tthrow new Error(`Image download failed (${response.status}): ${rawText}`);\n\t}\n\tconst contentType = response.headers.get(\"content-type\")?.split(\";\")[0];\n\tif (!contentType?.startsWith(\"image/\")) {\n\t\tthrow new Error(`Unsupported image type from URL: ${imageUrl}`);\n\t}\n\tconst buffer = await response.bytes();\n\treturn { data: buffer.toBase64(), mimeType: contentType };","sourceCodeStart":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/tools/image-gen.ts#L368-L404","documentation":"When the image URL is a data: URL, the tool parses it with normalizeDataUrl; a data: URL that carries raw base64 payload without a mime type (e.g. 'data:;base64,...' or malformed header) leaves mimeType undefined. Since providers need an explicit media type to send the inline image, the tool refuses rather than guessing. This protects the API request from being rejected for a missing/invalid media type.","triggerScenarios":"Passing a data: URL to image generation input images where the mime portion is empty or unparseable — e.g. `data:;base64,<payload>`, `data:image;base64,...` (missing subtype), or a truncated URL that lost its type prefix.","commonSituations":"Hand-built data URLs in scripts or notebooks; copy-pasted URLs where the mime type was stripped; output from encoders that omit the media type; template interpolation that dropped the `image/png,` segment.","solutions":["Include an explicit mime type in the data URL: `data:image/png;base64,<payload>`","If using a generic type, use a supported one like image/png, image/jpeg, image/webp, or image/gif","Decode and re-encode the payload with a helper that emits the mime header","Use a file path input instead of a data: URL so the tool sniffs the type from file bytes"],"exampleFix":"// before\nconst url = \"data:;base64,iVBORw0KGgoAAA...\";\n// after\nconst url = \"data:image/png;base64,iVBORw0KGgoAAA...\";","handlingStrategy":"validation","validationCode":"function validateDataUrl(url: string): boolean {\n\tconst m = /^data:([\\w.+-]+\\/[\\w.+-]+)?(;base64)?,/.exec(url);\n\treturn !!m && !!m[1]; // mime type must be present\n}","typeGuard":"function hasDataUrlMimeType(url: string): boolean {\n\tconst m = /^data:([^;,\\s]+)/.exec(url.trim());\n\treturn m !== null && m[1].includes(\"/\");\n}","tryCatchPattern":null,"preventionTips":["Always build data URLs with an explicit `data:image/png;base64,` prefix via a helper","Never hand-concatenate data URL strings in templates","Run validateDataUrl on every data URL before passing it to the tool","Prefer file-path inputs when the source is a local file"],"tags":["validation","data-url","mime-type","image-generation"],"backgroundTag":"missing-mime-type","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}