{"record":{"id":"fb36789093c1cc7a","repo":"BoundaryML/baml","slug":"could-not-determine-mime-type-for-pdf-input-only-application","errorCode":null,"errorMessage":"Could not determine mime type for PDF input. Only application/pdf is allowed.","messagePattern":"Could not determine mime type for PDF input\\. 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":624,"sourceCode":"            }\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 '{}')\",\n                        media_path\n                            .strip_prefix(\"file://\")\n                            .unwrap_or(media_path.as_str())\n                    )\n                } else {\n                    BASE64_STANDARD.encode(&bytes)","sourceCodeStart":606,"sourceCodeEnd":642,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-runtime/src/internal/llm_client/traits/mod.rs#L606-L642","documentation":"BAML only accepts PDFs whose MIME type is application/pdf for PDF media inputs. When a base64 data URL or file is supplied for a pdf input but the MIME type cannot be inferred at all, process_media bails with this error rather than sending an untyped payload to the LLM provider.","triggerScenarios":"A media value for a pdf-typed BAML parameter lacks a data: URL prefix with a mime type and no decodable magic bytes, so infer returns None (as_base64 yields no mime type).","commonSituations":"Passing a raw base64 string without a data:application/pdf;base64, prefix; uploading a scanned/corrupted PDF whose magic bytes were stripped; renaming a non-PDF file to .pdf and sending its base64.","solutions":["Prefix the media value with a data URL: data:application/pdf;base64,<payload>","Verify the file is actually a valid PDF (starts with %PDF-) and not corrupted","If using a URL instead of base64, ensure the server serves Content-Type application/pdf so inference succeeds","Check you declared the parameter type as pdf in the BAML schema and are not reusing an image value"],"exampleFix":"// before\nclient.messages(media_content=b64_pdf_string)\n// after\nclient.messages(media_content=\"data:application/pdf;base64,\" + b64_pdf_string)","handlingStrategy":"validation","validationCode":"function isPdfInput(media) {\n  const isPdfDataUrl = /^data:application\\/pdf;base64,/.test(media);\n  const decoded = Buffer.from(media.replace(/^data:\\w+\\/\\w+;base64,/, ''), 'base64');\n  const hasPdfMagic = decoded.subarray(0, 5).toString() === '%PDF-';\n  return isPdfDataUrl || hasPdfMagic;\n}\nif (!isPdfInput(mediaValue)) throw new Error('Input must be a valid application/pdf data URL or PDF bytes');","typeGuard":"const isPdfDataUrl = (s) => typeof s === 'string' && /^data:application\\/pdf;base64,[A-Za-z0-9+/=]+$/.test(s);","tryCatchPattern":null,"preventionTips":["Always wrap raw base64 PDFs in a data:application/pdf;base64, prefix","Validate the %PDF- magic bytes before sending","Don't reuse image-typed media variables for PDF parameters"],"tags":["media","mime-type","pdf","baml"],"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-14T05:17:10.506Z"}