BoundaryML/baml · error · Error

Pdf is not base64

Error message

Pdf is not base64

What it means

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.

Solutions

  1. Guard with pdf.isBase64() (or check type) before calling asBase64().
  2. Use asUrl() when isUrl() is true, or convert via toBase64() which works for both.
  3. Normalize inputs at construction time so downstream code sees one representation.

Example fix

// before
const [b64, mediaType] = pdf.asBase64();
// after
if (pdf.type === 'base64') { const [b64, mediaType] = pdf.asBase64(); } else { const b64 = pdf.toBase64(); }
Defensive patterns

Strategy: type-guard

Type guard

function pdfHasBase64(pdf: BamlPdf): boolean { return pdf.type === 'base64'; }

Try / catch

try {
  const [b64, mediaType] = pdf.asBase64();
} catch (e) {
  if (e.message === 'Pdf is not base64') {
    const url = pdf.asUrl();
  } else throw e;
}

Prevention

When it happens

Trigger: Calling pdf.asBase64() on a BamlPdf built via BamlPdf.fromUrl(...) or otherwise with type 'url'.

Common situations: Handling a mix of URL- and base64-backed PDFs (uploads vs links) and calling asBase64() uniformly; deserialized payloads whose origin is unknown.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/ae8bb4248deecc3e. Report an issue: GitHub.

Appendix: source

Thrown at engine/language_client_typescript/pdf.js:79

    }
    /**
     * Get the URL of the pdf if it's stored as a URL
     * @throws Error if the pdf is not stored as a URL
     */
    asUrl() {
        if (!this.isUrl()) {
            throw new Error("Pdf is not a URL");
        }
        return this.content;
    }
    /**
     * Get the base64 data and media type if the pdf is stored as base64
     * @returns [base64Data, mediaType]
     * @throws Error if the pdf is not stored as base64
     */
    asBase64() {
        if (this.type !== "base64") {
            throw new Error("Pdf is not base64");
        }
        return [this.content, this.mediaType || ""];
    }
    /**
     * Convert the pdf to a JSON representation
     */
    toJSON() {
        if (this.type === "url") {
            return { url: this.content };
        }
        return {
            base64: this.content,
            media_type: this.mediaType || "",
        };
    }
}
exports.BamlPdf = BamlPdf;

View on GitHub (pinned to bd85ce9dee)