{"record":{"id":"376e65c64d9ab81a","repo":"BoundaryML/baml","slug":"pdf-is-not-base64-376e65","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/typescript_src/pdf.ts","lineNumber":81,"sourceCode":"  /**\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(): string {\n    if (!this.isUrl()) {\n      throw new Error(\"Pdf is not a URL\");\n    }\n    return this.content;\n  }\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(): [string, string] {\n    if (this.type !== \"base64\") {\n      throw new Error(\"Pdf is not base64\");\n    }\n    return [this.content, this.mediaType || \"\"];\n  }\n\n  /**\n   * Convert the pdf 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/pdf.ts#L63-L99","documentation":"TypeScript client state guard: asBase64() was called on a BamlPdf whose internal type is 'url'. The method must return [base64Data, mediaType]; handing back URL text would corrupt the payload, so the tag check throws. Use asUrl() or convert the representation first.","triggerScenarios":"Calling pdf.asBase64() on a Pdf whose type is 'url' (created via fromUrl), i.e. this.type !== 'base64'.","commonSituations":"Code that needs document bytes (uploading elsewhere, writing to disk, inlining into requests) receiving URL-referenced PDFs from model responses or configuration.","solutions":["Check pdf.type === 'base64' before calling asBase64()","Download the PDF from its URL and base64-encode it when bytes are needed","Use Pdf.fromBase64(...) at construction if downstream consumers require base64"],"exampleFix":"// before\nconst [b64, mt] = pdf.asBase64(); // throws for URL pdf\n// after\nif (pdf.type === 'base64') {\n  const [b64, mt] = pdf.asBase64();\n} else {\n  const res = await fetch(pdf.asUrl());\n  const b64 = Buffer.from(await res.arrayBuffer()).toString('base64');\n}","handlingStrategy":"type-guard","validationCode":"if (pdf.type !== 'base64') { /* fetch URL and encode */ }","typeGuard":"const isBase64Pdf = (p) => p.type === 'base64';","tryCatchPattern":"try { const [b64, mt] = pdf.asBase64(); }\ncatch { const bytes = await (await fetch(pdf.asUrl())).arrayBuffer(); }","preventionTips":["Check type === 'base64' before asBase64()","Centralize PDF byte extraction in one helper","Align construction source with downstream consumption needs"],"tags":["pdf","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"}