{"record":{"id":"0efafaf074859f0d","repo":"BoundaryML/baml","slug":"pdf-is-not-a-url","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/pdf.js","lineNumber":68,"sourceCode":"     */\n    static async fromUrlAsync(url) {\n        const response = await fetch(url);\n        const blob = await response.blob();\n        return BamlPdf.fromBlob(blob);\n    }\n    /**\n     * Check if the pdf is stored as a URL\n     */\n    isUrl() {\n        return this.type === \"url\";\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() {\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() {","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_typescript/pdf.js#L50-L86","documentation":"BamlPdf.asUrl() only returns this.content when the PDF was constructed from a URL. If the instance holds base64 data instead, the accessor throws 'Pdf is not a URL' to prevent silently misusing base64 bytes as a URL.","triggerScenarios":"Calling pdf.asUrl() on a BamlPdf created via BamlPdf.fromBase64(...) (type !== 'url'), i.e. whenever isUrl() returns false.","commonSituations":"Receiving a BamlPdf from a function result or user upload that is base64-encoded and blindly calling asUrl(); assuming all Pdf objects carry a remote URL.","solutions":["Check pdf.isUrl() before calling asUrl(), and fall back to asBase64() otherwise.","Use toBase64() or asBase64() when you need the binary content regardless of source form.","If a URL is required, construct the Pdf from a URL (BamlPdf.fromUrl) at the source."],"exampleFix":"// before\nconst url = pdf.asUrl();\n// after\nif (pdf.isUrl()) { const url = pdf.asUrl(); } else { const [b64, mediaType] = pdf.asBase64(); }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function pdfHasUrl(pdf: BamlPdf): boolean { return pdf.isUrl(); }","tryCatchPattern":"try {\n  const url = pdf.asUrl();\n} catch (e) {\n  if (e.message === 'Pdf is not a URL') {\n    const [b64, mediaType] = pdf.asBase64();\n    // handle base64 path\n  } else throw e;\n}","preventionTips":["Always branch on isUrl()/type before choosing an accessor.","Normalize PDFs to one representation (e.g. toBase64) early in your pipeline."],"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"}