BoundaryML/baml · error

array assignment on non-list value at

Error message

array assignment on non-list value at {:?}

What it means

Once the index of an `arr[i] = v` target is validated, the base value must be a `BamlValueWithMeta::List`. If the base is a map, string, or other kind, the interpreter bails with `array assignment on non-list value`, including the source location from `meta.0`.

Solutions

  1. Confirm the base value is a list before positional assignment
  2. Use map field-assignment syntax (`m["key"] = v`) for maps instead
  3. Parse/convert the value to a list if it arrives as another JSON shape
  4. Add a runtime check (e.g. length or type inspection) before assigning

Example fix

// before
let m = {};
m[0] = "x"; // non-list value
// after
let m = {};
m["0"] = "x"; // map key assignment
Defensive patterns

Strategy: type-guard

Validate before calling

// confirm the base is a list before positional assignment
assert matches!(base, BamlValue::List(_));

Type guard

fn is_list(v: &BamlValue) -> bool { matches!(v, BamlValue::List(_)) }

Try / catch

// host-side
match result {
    Err(e) if e.to_string().contains("non-list value") => {
        // convert base to a list or switch to map-key assignment
    }
    other => other?,
}

Prevention

When it happens

Trigger: `x[i] = v` where `x` evaluates to a Map or any non-list value (e.g. trying positional assignment on a map).

Common situations: Confusing maps and lists in BAML; a variable holding JSON of unexpected shape (object instead of array); refactors changing a variable's type while positional writes remain.

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

Appendix: source

Thrown at engine/baml-compiler/src/thir/interpret.rs:1540

                    .await?,
                )?;

                let idx = match index_value {
                    BamlValueWithMeta::Int(i, _) if i >= 0 => i as usize,
                    _ => bail!(
                        "array assignment requires a non-negative integer index at {:?}",
                        meta.0
                    ),
                };

                match &mut base_value {
                    BamlValueWithMeta::List(items, _) => {
                        if idx >= items.len() {
                            bail!("array assignment index out of bounds");
                        }
                        items[idx] = value_to_assign.clone();
                    }
                    _ => bail!("array assignment on non-list value at {:?}", meta.0),
                }

                value_to_assign = base_value;
                current_expr = base.as_ref();
            }
            _ => return Err(anyhow!("Complex assignment targets not yet supported")),
        }
    }
}

fn lookup(scopes: &[Scope], name: &str) -> Option<BamlValueWithMeta<ExprMetadata>> {
    for s in scopes.iter().rev() {
        if let Some(cell) = s.variables.get(name) {
            return Some(cell.lock().unwrap().clone());
        }
    }
    None
}

View on GitHub (pinned to bd85ce9dee)