{"record":{"id":"1a8c7fece6c640a8","repo":"BoundaryML/baml","slug":"pdf-is-not-a-url-1a8c7f","errorCode":null,"errorMessage":"Pdf is not a URL","messagePattern":"Pdf is not a URL","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"engine/language_client_typescript/typescript_src/pdf.ts","lineNumber":69,"sourceCode":"    const response = await fetch(url);\n    const blob = await response.blob();\n    return BamlPdf.fromBlob(blob);\n  }\n\n  /**\n   * Check if the pdf is stored as a URL\n   */\n  isUrl(): boolean {\n    return this.type === \"url\";\n  }\n\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(): 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","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_typescript/typescript_src/pdf.ts#L51-L87","documentation":"TypeScript client state guard: asUrl() was called on a BamlPdf whose internal type is 'base64', not 'url'. BamlPdf is a tagged union of the two representations; the accessor for the wrong tag throws rather than return base64 payload bytes where a URL is expected.","triggerScenarios":"Calling pdf.asUrl() on a Pdf constructed from base64 (fromBase64) or another non-URL source, where isUrl() is false.","commonSituations":"Displaying or sharing PDF links while the document was inlined as base64 (common when passing file bytes to LLMs); mixing URL-loaded and byte-loaded documents in the same code path.","solutions":["Guard with pdf.isUrl() before calling asUrl()","Construct the Pdf from a URL (Pdf.fromUrl(...)) when URL access is required","Branch on storage type and handle the base64 case with asBase64()"],"exampleFix":"// before\nconst url = pdf.asUrl(); // throws for base64 pdf\n// after\nif (pdf.isUrl()) {\n  const url = pdf.asUrl();\n} else {\n  const [b64, mediaType] = pdf.asBase64();\n}","handlingStrategy":"type-guard","validationCode":"if (!pdf.isUrl()) { /* handle base64 branch */ }","typeGuard":"const isUrlPdf = (p) => p.isUrl();","tryCatchPattern":"try { const url = pdf.asUrl(); }\ncatch { const [b64, mt] = pdf.asBase64(); }","preventionTips":["Check isUrl() before asUrl()","Branch on storage type in shared document-handling code","Prefer URL construction when downstream code needs links"],"tags":["pdf","media","base64","url"],"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"}