BoundaryML/baml · error

array assignment requires a non-negative integer index at

Error message

array assignment requires a non-negative integer index at {:?}

What it means

For array-element assignment targets `arr[i] = v`, the interpreter evaluates the index expression and requires it to be a non-negative integer (`Int(i)` with `i >= 0`). Any other value type (string, float, negative int) causes this bail, which includes the expression's source span via `meta.0`.

Solutions

  1. Ensure the index expression evaluates to a non-negative integer
  2. Cast/round float results to integers before indexing
  3. Use map syntax with string keys if you need key-based assignment
  4. Add a guard comparing the computed index against 0 before assignment

Example fix

// before
let i = -1;
arr[i] = 5; // requires non-negative integer index
// after
let i = 0;
if (i >= 0) { arr[i] = 5; }
Defensive patterns

Strategy: validation

Validate before calling

// before arr[i] = v, ensure i is a non-negative integer
assert i >= 0;

Type guard

fn is_valid_index(v: &BamlValue) -> bool {
    matches!(v, BamlValue::Int(i) if *i >= 0)
}

Prevention

When it happens

Trigger: `arr[i] = v` where `i` evaluates to a string, float, bool, or a negative integer.

Common situations: Using a string key on an array (map-style indexing); computing the index with a float-producing expression; off-by-one producing a negative index from a subtraction.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

                        function_name,
                    )
                    .await?,
                )?;
                let index_value = expect_value(
                    evaluate_expr(
                        index,
                        scopes,
                        thir,
                        run_llm_function,
                        watch_handler,
                        function_name,
                    )
                    .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();
            }

View on GitHub (pinned to bd85ce9dee)