BoundaryML/baml · error

expected value, found function

Error message

expected value, found function

What it means

`expect_value` converts an evaluation result into a plain value, but function values (first-class function/closure references from `EvalValue::Function`) are not usable as values. When an expression that must produce a data value instead evaluates to a function, this error is thrown.

Solutions

  1. Add parentheses to invoke the function: `myFn()` instead of `myFn`
  2. Check that the identifier at that position refers to a value, not a function declaration
  3. If first-class functions are needed, they are not supported as values in this interpreter — restructure the code

Example fix

// before
let result = compute;      // function, not value
// after
let result = compute();
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof candidate === 'function') { throw new Error('expected a value, got a function — did you forget ()?'); }

Type guard

function isValueNotFunction(v) { return typeof v !== 'function'; }

Try / catch

try { let v = expectValue(expr); } catch (e) { if (String(e).includes('expected value, found function')) { /* add () to invoke */ } else { throw e; } }

Prevention

When it happens

Trigger: Using a function name where a value is expected: assigning a function to a variable read as data, passing a function as an argument to something expecting a value, using a function in a binary op, array, or return-value position; calling `expect_value` on the result of an expression like `myFn` without invoking it.

Common situations: Forgetting `()` to invoke a function (`let x = compute;` instead of `compute()`); shadowing a variable with a function name; attempting to store/print a function reference.

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

Appendix: source

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

                evaluate_expr(
                    inner,
                    scopes,
                    thir,
                    run_llm_function,
                    watch_handler,
                    function_name,
                )
                .await?
            }
        })
    })
}

fn expect_value(v: EvalValue) -> Result<BamlValueWithMeta<ExprMetadata>> {
    match v {
        EvalValue::Value(v) => Ok(v),
        EvalValue::Reference(cell) => Ok(cell.lock().unwrap().clone()),
        EvalValue::Function(_, _, _) => bail!("expected value, found function"),
    }
}

fn evaluate_binary_op(
    operator: &crate::hir::BinaryOperator,
    left_val: &BamlValueWithMeta<ExprMetadata>,
    right_val: &BamlValueWithMeta<ExprMetadata>,
    meta: &ExprMetadata,
) -> Result<BamlValueWithMeta<ExprMetadata>> {
    use crate::hir::BinaryOperator;
    Ok(match operator {
        // Arithmetic operations
        BinaryOperator::Add => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                BamlValueWithMeta::Int(a + b, meta.clone())
            }
            (BamlValueWithMeta::Float(a, _), BamlValueWithMeta::Float(b, _)) => {
                BamlValueWithMeta::Float(a + b, meta.clone())

View on GitHub (pinned to bd85ce9dee)