{"record":{"id":"999198674166761a","repo":"BoundaryML/baml","slug":"file-provided-for-pdf-input-is-not-a-pdf-detected-mime-type","errorCode":null,"errorMessage":"File provided for PDF input is not a PDF. Detected mime type: '{}'. Only application/pdf is allowed.","messagePattern":"File provided for PDF input is not a PDF\\. Detected mime type: '(.+?)'\\. Only application/pdf is allowed\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-runtime/src/internal/llm_client/traits/mod.rs","lineNumber":618,"sourceCode":"            let mut mime_type = part.mime_type.clone();\n\n            if mime_type.is_none() {\n                if let Some(ext) = media_file.extension() {\n                    mime_type = Some(format!(\"{}/{}\", part.media_type, ext));\n                }\n            }\n\n            if mime_type.is_none() {\n                if let Some(t) = infer::get(&bytes) {\n                    mime_type = Some(t.mime_type().to_string());\n                }\n            }\n\n            // ENFORCEMENT: For PDF, the mime type must be application/pdf\n            if part.media_type == BamlMediaType::Pdf {\n                match &mime_type {\n                    Some(mt) if mt != \"application/pdf\" => {\n                        anyhow::bail!(\n                            \"File provided for PDF input is not a PDF. Detected mime type: '{}'. Only application/pdf is allowed.\",\n                            mt\n                        );\n                    }\n                    None => {\n                        anyhow::bail!(\n                            \"Could not determine mime type for PDF input. Only application/pdf is allowed.\"\n                        );\n                    }\n                    _ => {}\n                }\n            }\n\n            Ok(BamlMedia::base64(\n                part.media_type,\n                if render_settings.as_shell_commands {\n                    format!(\n                        \"$(base64 '{}')\",","sourceCodeStart":600,"sourceCodeEnd":636,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/internal/llm_client/traits/mod.rs#L600-L636","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: renamed Word doc\nimage_url: baml://report.docx  // typed as Pdf\n\n// after: real PDF export\n// $ pandoc report.docx -o report.pdf\nimage_url: baml://report.pdf","handlingStrategy":"validation","validationCode":"// Sniff the PDF header before passing it as a Pdf media part\nconst buf = Buffer.from(await fetch(url).then(r => r.arrayBuffer()));\nif (!buf.subarray(0, 5).toString('latin1').startsWith('%PDF-')) {\n  throw new Error('File is not a real PDF (missing %PDF- header)');\n}","typeGuard":"function isRealPdf(buf) { return buf && buf.length > 4 && buf.subarray(0, 5).toString('latin1') === '%PDF-'; }","tryCatchPattern":"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'); }","preventionTips":["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"],"tags":["baml","media","pdf","validation"],"backgroundTag":"invalid-argument-value","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"}