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
- Declare the parameter with the correct media type, e.g. `image` or `audio`, in the .baml function signature
- Ensure the media kind in the value matches the declared media type (image file for `image`, audio for `audio`)
- If both kinds are allowed, use a union such as `image | audio`
- 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
- Match media kind (image/audio/pdf) to the declared .baml type
- Use unions (`image | audio`) when multiple kinds are valid
- Never pass media objects to string-typed params; pass URLs/base64 strings instead
- Keep generated client media wrappers in sync with .baml
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
- Could not unify Float with {:?}
- Could not unify Bool with {:?}
- Could not unify map with {field_type:?}
- Could not infer child type
- Could not unify Enum {} with {:?}
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/99fc55af56beb63d.
Report an issue: GitHub.