BoundaryML/baml · error · anyhow::Error

Class not found for instance: {:?}

Error message

Class not found for instance: {:?}

What it means

Converting a VmObject::Instance into a Value, the BAML VM looks up the instance's class in the object pool; if the slot at instance.class is not a Class, from_vm_object bails with "Class not found for instance". This is an internal VM invariant violation — instances should always point at a valid class.

Source

Thrown at engine/baml-vm/src/test.rs:101

            VmObject::String(s) => Ok(Object::String(s.clone())),

            VmObject::Array(arr) => arr
                .iter()
                .map(|v| Value::from_vm_value(v, vm))
                .collect::<anyhow::Result<Vec<_>>>()
                .map(Object::Array),

            VmObject::Map(map) => map
                .iter()
                .map(|(key, value)| {
                    Value::from_vm_value(value, vm).map(|value| (key.clone(), value))
                })
                .collect::<anyhow::Result<BamlMap<String, Value>>>()
                .map(Object::Map),

            VmObject::Instance(instance) => {
                let VmObject::Class(vm_class) = &vm.objects[instance.class] else {
                    anyhow::bail!("Class not found for instance: {:?}", instance);
                };

                let mut fields = BamlMap::new();

                for (i, value) in instance.fields.iter().enumerate() {
                    let value = Value::from_vm_value(value, vm)?;
                    fields.insert(vm_class.field_names[i].clone(), value);
                }

                Ok(Object::Instance(Instance {
                    class: vm_class.name.clone(),
                    fields,
                }))
            }

            VmObject::Variant(variant) => {
                let VmObject::Enum(vm_enum) = &vm.objects[variant.enm] else {
                    anyhow::bail!("Enum not found for variant: {:?}", variant);

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Regenerate the BAML client (baml-cli generate) so generated code matches the runtime
  2. Clean build artifacts and reinstall matching baml CLI/runtime versions
  3. Reduce the failing test to the class definition involved and check for syntax/type issues
  4. Report to BAML if reproducible on matching versions — it indicates a VM bug
Defensive patterns

Strategy: try-catch

Validate before calling

// verify version alignment before running tests
const rt = require('@boundaryml/baml/package.json').version;
if (rt !== expectedCliVersion) throw new Error(`baml runtime ${rt} != CLI ${expectedCliVersion}; regenerate client`);

Try / catch

match BamlValue::from_vm_object(instance, vm) {
  Ok(v) => v,
  Err(e) if e.to_string().contains("Class not found for instance") => {
    // regenerate client / flag VM corruption
    anyhow::bail!("stale or corrupted BAML VM state: {e}");
  }
  Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling BamlValue::from_vm_object (or Value::from_vm_value paths) on an Instance whose class index is dangling/corrupted in vm.objects — typically from a compiler/runtime mismatch or a VM bug during test execution.

Common situations: Version mismatch between generated BAML client code and the baml-vm/runtime; corrupted or stale compiled artifacts; running tests against sources recompiled mid-execution.

Related errors


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