BoundaryML/baml · error · anyhow::Error

Could not infer child type

Error message

Could not infer child type

What it means

When distributing types over a List value, BAML needs the element type of the expected TypeIR via `item_type`. If the expected type has no list element (not a `T[]`/list, and no resolvable alias), the child cannot be typed and this error is raised.

Source

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

                let mapped_fields: BamlMap<String, BamlValueWithMeta<(T, TypeIR)>> = pairs
                    .into_iter()
                    .map(|(key, val)| {
                        let sub_value =
                            self.distribute_type_with_meta(val, annotation_value_type.clone())?;

                        Ok((key, sub_value))
                    })
                    .collect::<anyhow::Result<BamlMap<String, BamlValueWithMeta<(T, TypeIR)>>>>()?;

                Ok(BamlValueWithMeta::Map(mapped_fields, (meta, field_type)))
            }

            BamlValueWithMeta::List(items, meta) => {
                let new_items = items
                    .into_iter()
                    .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)
            }

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Declare the parameter as a list type (e.g. `string[]`) in the .baml schema so item_type resolves
  2. If the value should be a single item, wrap/unwrap it on the caller side so the shapes match
  3. Check for unresolved or recursive type aliases in the .baml file that prevent item_type from seeing the element type
  4. Read the preceding 'Could not unify ...' context or the field_type to see which declared type rejected the array

Example fix

// before (.baml)
function Tag(items: string) -> bool { ... }
// caller sends items: ["a", "b"]

// after (.baml)
function Tag(items: string[]) -> bool { ... }
Defensive patterns

Strategy: validation

Validate before calling

function validateListArg(value: unknown, declaredType: string): boolean {
  return Array.isArray(value) && /^.*\[\]$|^list\s*</.test(declaredType);
}

Type guard

const isList = (v: unknown): v is unknown[] => Array.isArray(v);

Prevention

When it happens

Trigger: distribute_type_with_meta on BamlValueWithMeta::List where field_type is not a list/array type — e.g. passing an array for a `string` parameter, or an alias that fails to resolve to a list.

Common situations: Passing a JSON array where a scalar or class was declared; recursive/alias types that the resolver could not expand into a list; schema refactors that changed `string[]` to `string`.

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/3b7cc2a935ac1861. Report an issue: GitHub.