BoundaryML/baml · error · anyhow::Error

Could not unify Media with {:?}

Error message

Could not unify Media with {:?}

What it means

A Media value (image/audio/pdf etc.) could not be unified with the expected TypeIR: `is_subtype(media_primitive, field_type)` failed in distribute_type_with_meta, so the pass bails instead of tagging the value. The declared type must accept the specific media kind.

Source

Thrown at engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs:333

                    .map(|i| {
                        item_type(self, &field_type)
                            .ok_or(anyhow::anyhow!("Could not infer child type"))
                            .and_then(|item_type| self.distribute_type_with_meta(i, item_type))
                    })
                    .collect::<Result<Vec<_>>>()?;
                Ok(BamlValueWithMeta::List(new_items, (meta, field_type)))
            }

            BamlValueWithMeta::Media(m, meta)
                if self.is_subtype(
                    &TypeIR::Primitive(TypeValue::Media(m.media_type), Default::default()),
                    &field_type,
                ) =>
            {
                Ok(BamlValueWithMeta::Media(m, (meta, field_type)))
            }
            BamlValueWithMeta::Media(_, _) => {
                anyhow::bail!("Could not unify Media with {:?}", field_type)
            }

            BamlValueWithMeta::Enum(name, val, meta) => {
                if self.is_subtype(
                    &TypeIR::Enum {
                        name: name.clone(),
                        dynamic: false,
                        meta: Default::default(),
                    },
                    &field_type,
                ) {
                    Ok(BamlValueWithMeta::Enum(name, val, (meta, field_type)))
                } else {
                    anyhow::bail!("Could not unify Enum {} with {:?}", name, field_type)
                }
            }

            BamlValueWithMeta::Class(name, fields, meta) => {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Declare the parameter with the correct media type, e.g. `image` or `audio`, in the .baml function signature
  2. Ensure the media kind in the value matches the declared media type (image file for `image`, audio for `audio`)
  3. If both kinds are allowed, use a union such as `image | audio`
  4. If the parameter should be plain text, convert the media to a string/URL before calling

Example fix

// before (.baml)
function Describe(pic: string) -> string { ... }
// caller attaches an image media value

// after (.baml)
function Describe(pic: image) -> string { ... }
Defensive patterns

Strategy: validation

Validate before calling

function validateMediaArg(media: { kind: string }, declaredType: string): boolean {
  return declaredType === media.kind || declaredType.split('|').map(s => s.trim()).includes(media.kind);
}

Type guard

const isImageMedia = (m: { kind: string }): m is { kind: 'image' } => m.kind === 'image';

Prevention

When it happens

Trigger: distribute_type over BamlValueWithMeta::Media when field_type is `string`, `image` vs `audio` mismatch, or any non-media type — e.g. passing an image where the parameter is declared string, or an audio where image is expected.

Common situations: Attaching a base64/file media object to a prompt parameter declared as `string`; swapping input media kinds (image -> audio) without updating the .baml signature; older schemas where media params were strings.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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