{"record":{"id":"5e856b9c0f4a9798","repo":"BoundaryML/baml","slug":"image-is-not-base64-5e856b","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/typescript_src/image.ts","lineNumber":81,"sourceCode":"  /**\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(): string {\n    if (!this.isUrl()) {\n      throw new Error('Image is not a URL');\n    }\n    return this.content;\n  }\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(): [string, string] {\n    if (this.type !== 'base64') {\n      throw new Error('Image is not base64');\n    }\n    return [this.content, this.mediaType || ''];\n  }\n\n  /**\n   * Convert the image to a JSON representation\n   */\n  toJSON(): { url: string } | { base64: string; media_type: string } {\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}\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_typescript/typescript_src/image.ts#L63-L99","documentation":"BAML's Image.asBase64() returns [base64Data, mediaType] only when the image content is stored as base64. URL-backed images have no inline payload, so the method throws rather than returning empty/invalid data.","triggerScenarios":"Calling image.asBase64() on an Image whose type is 'url' (created via fromUrl), i.e. this.type !== 'base64'.","commonSituations":"Pipelines that inline images into API payloads (needing base64) but receive URL-referenced images from model responses or user configuration.","solutions":["Check image.type === 'base64' before calling asBase64()","Fetch the image from its URL and encode to base64 when bytes are needed","Use Image.fromBase64(...) at construction time if downstream code requires base64"],"exampleFix":"// before\nconst [b64, mt] = image.asBase64(); // throws for URL image\n// after\nif (image.type === 'base64') {\n  const [b64, mt] = image.asBase64();\n} else {\n  const res = await fetch(image.asUrl());\n  const b64 = Buffer.from(await res.arrayBuffer()).toString('base64');\n}","handlingStrategy":"type-guard","validationCode":"if (image.type !== 'base64') { /* fetch URL and encode */ }","typeGuard":"const isBase64Image = (i) => i.type === 'base64';","tryCatchPattern":"try { const [b64, mt] = image.asBase64(); }\ncatch { const bytes = await (await fetch(image.asUrl())).arrayBuffer(); }","preventionTips":["Check type === 'base64' before asBase64()","Fetch-and-encode helper for URL images","Document expected storage type at API boundaries"],"tags":["image","media","base64"],"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-14T11:17:12.474Z"}