{"record":{"id":"ae8bb4248deecc3e","repo":"BoundaryML/baml","slug":"pdf-is-not-base64","errorCode":null,"errorMessage":"Pdf is not base64","messagePattern":"Pdf is not base64","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"engine/language_client_typescript/pdf.js","lineNumber":79,"sourceCode":"    }\n    /**\n     * Get the URL of the pdf if it's stored as a URL\n     * @throws Error if the pdf is not stored as a URL\n     */\n    asUrl() {\n        if (!this.isUrl()) {\n            throw new Error(\"Pdf is not a URL\");\n        }\n        return this.content;\n    }\n    /**\n     * Get the base64 data and media type if the pdf is stored as base64\n     * @returns [base64Data, mediaType]\n     * @throws Error if the pdf is not stored as base64\n     */\n    asBase64() {\n        if (this.type !== \"base64\") {\n            throw new Error(\"Pdf is not base64\");\n        }\n        return [this.content, this.mediaType || \"\"];\n    }\n    /**\n     * Convert the pdf 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.BamlPdf = BamlPdf;\n","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_typescript/pdf.js#L61-L97","documentation":"JavaScript client state guard: asBase64() was called on a BamlPdf whose internal type is 'url', not 'base64'. The tagged-union check throws rather than returning the URL string in place of base64 data. Use asUrl() for URL-stored PDFs, or convert to base64 before calling.","triggerScenarios":"Calling pdf.asBase64() on a BamlPdf built via BamlPdf.fromUrl(...) or otherwise with type 'url'.","commonSituations":"Handling a mix of URL- and base64-backed PDFs (uploads vs links) and calling asBase64() uniformly; deserialized payloads whose origin is unknown.","solutions":["Guard with pdf.isBase64() (or check type) before calling asBase64().","Use asUrl() when isUrl() is true, or convert via toBase64() which works for both.","Normalize inputs at construction time so downstream code sees one representation."],"exampleFix":"// before\nconst [b64, mediaType] = pdf.asBase64();\n// after\nif (pdf.type === 'base64') { const [b64, mediaType] = pdf.asBase64(); } else { const b64 = pdf.toBase64(); }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function pdfHasBase64(pdf: BamlPdf): boolean { return pdf.type === 'base64'; }","tryCatchPattern":"try {\n  const [b64, mediaType] = pdf.asBase64();\n} catch (e) {\n  if (e.message === 'Pdf is not base64') {\n    const url = pdf.asUrl();\n  } else throw e;\n}","preventionTips":["Check pdf.type (or isBase64) before calling asBase64().","Use toBase64() when you need base64 regardless of the backing representation."],"tags":["typescript","pdf","media","invalid-state"],"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"}