{"record":{"id":"c892dff399d22a11","repo":"BoundaryML/baml","slug":"image-is-not-base64","errorCode":null,"errorMessage":"Image is not base64","messagePattern":"Image is not base64","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"engine/language_client_typescript/image.js","lineNumber":79,"sourceCode":"    }\n    /**\n     * Get the URL of the image if it's stored as a URL\n     * @throws Error if the image is not stored as a URL\n     */\n    asUrl() {\n        if (!this.isUrl()) {\n            throw new Error('Image is not a URL');\n        }\n        return this.content;\n    }\n    /**\n     * Get the base64 data and media type if the image is stored as base64\n     * @returns [base64Data, mediaType]\n     * @throws Error if the image is not stored as base64\n     */\n    asBase64() {\n        if (this.type !== 'base64') {\n            throw new Error('Image is not base64');\n        }\n        return [this.content, this.mediaType || ''];\n    }\n    /**\n     * Convert the image to a JSON representation\n     */\n    toJSON() {\n        if (this.type === 'url') {\n            return { url: this.content };\n        }\n        return {\n            base64: this.content,\n            media_type: this.mediaType || '',\n        };\n    }\n}\nexports.BamlImage = BamlImage;\n","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_typescript/image.js#L61-L97","documentation":"JavaScript client state guard: asBase64() was called on a BamlImage whose internal type is 'url'. The method contract is to return [base64Data, mediaType]; serving URL text there would silently corrupt downstream payloads, so the wrong-tag check throws instead. Use asUrl(), or fetch and convert the image to base64 first.","triggerScenarios":"Calling image.asBase64() on an Image instance constructed from a URL (type !== 'base64').","commonSituations":"Needing raw base64 bytes for an API that requires inline data but holding a remote URL image; confusing which constructor produced the instance.","solutions":["Check this.type === 'base64' (or equivalent) before calling asBase64().","Use asUrl() for URL-stored images, or fetch the URL and encode to base64 yourself.","Construct with Image.fromBase64 if base64 access is required."],"exampleFix":"// before\nconst [data, mediaType] = image.asBase64(); // throws for URL image\n// after\nconst [data, mediaType] = image.type === 'base64' ? image.asBase64() : [null, null];","handlingStrategy":"type-guard","validationCode":"if (image.type === 'base64') { /* safe to call asBase64 */ }","typeGuard":"function isBase64Image(image) { return image.type === 'base64'; }","tryCatchPattern":"try {\n  const [data, mt] = image.asBase64();\n} catch (e) {\n  if (e.message === 'Image is not base64') { /* use asUrl or fetch+encode */ }\n  else throw e;\n}","preventionTips":["Check the `type` field before calling asBase64().","Fetch URL images and encode to base64 when inline data is required."],"tags":["typescript","image","media","state-error"],"backgroundTag":"incompatible-source-type","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}