BoundaryML/baml · error
File provided for PDF input is not a PDF. Detected mime type
Error message
File provided for PDF input is not a PDF. Detected mime type: '{}'. Only application/pdf is allowed. What it means
For media parts of type `Pdf`, BAML sniffs the file's MIME type and enforces that it is exactly `application/pdf`. If the detected type differs (or is undetectable), this error explains that only real PDFs are accepted for PDF-typed inputs.
Source
Thrown at engine/baml-runtime/src/internal/llm_client/traits/mod.rs:618
let mut mime_type = part.mime_type.clone();
if mime_type.is_none() {
if let Some(ext) = media_file.extension() {
mime_type = Some(format!("{}/{}", part.media_type, ext));
}
}
if mime_type.is_none() {
if let Some(t) = infer::get(&bytes) {
mime_type = Some(t.mime_type().to_string());
}
}
// ENFORCEMENT: For PDF, the mime type must be application/pdf
if part.media_type == BamlMediaType::Pdf {
match &mime_type {
Some(mt) if mt != "application/pdf" => {
anyhow::bail!(
"File provided for PDF input is not a PDF. Detected mime type: '{}'. Only application/pdf is allowed.",
mt
);
}
None => {
anyhow::bail!(
"Could not determine mime type for PDF input. Only application/pdf is allowed."
);
}
_ => {}
}
}
Ok(BamlMedia::base64(
part.media_type,
if render_settings.as_shell_commands {
format!(
"$(base64 '{}')",View on GitHub (pinned to bd85ce9dee)
Solutions
- Verify the file opens in a PDF viewer and starts with the `%PDF-` header; re-export a genuine PDF if not.
- Check the URL actually returns the PDF (correct status, content-type application/pdf) rather than an HTML error page.
- Remove the fake extension or change the declared `BamlMediaType` to match the real file type (e.g. Image).
- Re-download the file if it was truncated/corrupted mid-transfer.
Example fix
// before: renamed Word doc image_url: baml://report.docx // typed as Pdf // after: real PDF export // $ pandoc report.docx -o report.pdf image_url: baml://report.pdf
Defensive patterns
Strategy: validation
Validate before calling
// Sniff the PDF header before passing it as a Pdf media part
const buf = Buffer.from(await fetch(url).then(r => r.arrayBuffer()));
if (!buf.subarray(0, 5).toString('latin1').startsWith('%PDF-')) {
throw new Error('File is not a real PDF (missing %PDF- header)');
} Type guard
function isRealPdf(buf) { return buf && buf.length > 4 && buf.subarray(0, 5).toString('latin1') === '%PDF-'; } Try / catch
try { await b.F({ pdf: mediaPart }); } catch (e) { if (String(e).includes('not a PDF')) console.error('Re-export the file as a real PDF'); } Prevention
- Verify `%PDF-` magic bytes before uploading files typed as Pdf
- Don't rename other documents to .pdf; export/convert them properly
- Confirm URLs return HTTP 200 with content-type application/pdf, not HTML error pages
When it happens
Trigger: Passing a file (via `baml://` path or media_url) declared as `Pdf` whose bytes sniff as a different type — e.g. a .docx, image, or HTML page renamed to .pdf; also thrown when the MIME type could not be detected at all.
Common situations: Renaming a Word doc or screenshot to `.pdf`, serving a PDF behind a URL that returns an HTML error page (404 page saved as pdf), or corrupted/truncated PDF downloads.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Could not determine mime type for PDF input. Only applicatio
- baml.errors.InvalidArgument
- assertion failed
- baml.media.image.from_url expects 1 argument, got {} at {:?}
- baml.media.image.from_url expects a string argument at {:?}
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/999198674166761a.
Report an issue: GitHub.