BoundaryML/baml · error

Please specify a media type for this {}; we could not infer

Error message

Please specify a media type for this {}; we could not infer one

What it means

BamlMedia::mime_type_as_ok returns the media's MIME type; if it was never set, only PDFs get a default (application/pdf). For any other media type the library cannot infer a MIME type and asks the developer to specify one explicitly. The library refuses to guess Content-Type for non-PDF media.

Source

Thrown at engine/baml-lib/baml-types/src/media.rs:56

    pub content: BamlMediaContent,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BamlMediaContent {
    File(MediaFile),
    Url(MediaUrl),
    Base64(MediaBase64),
}

impl BamlMedia {
    pub fn mime_type_as_ok(&self) -> Result<String> {
        match &self.mime_type {
            Some(mt) => Ok(mt.clone()),
            None => {
                if self.media_type == BamlMediaType::Pdf {
                    Ok("application/pdf".to_string())
                } else {
                    Err(anyhow::anyhow!(
                        "Please specify a media type for this {}; we could not infer one",
                        self.media_type
                    ))
                }
            }
        }
    }
    pub fn file(
        media_type: BamlMediaType,
        baml_path: PathBuf,
        relpath: String,
        mime_type: Option<String>,
    ) -> BamlMedia {
        Self {
            media_type,
            mime_type,
            content: BamlMediaContent::File(MediaFile {
                span_path: baml_path,

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Set the media's mime_type explicitly when constructing BamlMedia (e.g. "image/png", "audio/wav").
  2. Derive it from the file extension or HTTP Content-Type header before constructing the media value.
  3. If the media is a PDF, note the library already defaults to application/pdf — no action needed.

Example fix

// before
let media = BamlMedia::media(BamlMediaType::Image, url, None);
let mt = media.mime_type_as_ok()?;
// after
let media = BamlMedia::media(BamlMediaType::Image, url, Some("image/png".to_string()));
let mt = media.mime_type_as_ok()?;
Defensive patterns

Strategy: validation

Validate before calling

if media.mime_type.is_none() && media.media_type != BamlMediaType::Pdf { return Err(anyhow!("must set mime_type for non-PDF media")); }

Type guard

fn has_mime(m: &BamlMedia) -> bool { m.mime_type.is_some() || m.media_type == BamlMediaType::Pdf }

Try / catch

let mt = media.mime_type_as_ok().map_err(|e| anyhow!("set mime_type: {}", e))?;

Prevention

When it happens

Trigger: Constructing a BamlMedia (image/audio/video) from bytes or a URL without setting mime_type, then calling mime_type_as_ok; passing media whose Content-Type was unavailable at construction time.

Common situations: Loading images or audio from raw bytes where no Content-Type header exists; file uploads where the client stripped the extension; older BAML versions/schemas that didn't record media type.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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