BoundaryML/baml · error

environment context corrupted

Error message

environment context corrupted

What it means

After locating `__env_vars__`, the interpreter requires it to hold a `BamlValueWithMeta::Map`. If the stored value is any other kind, it bails with `environment context corrupted`, indicating the host injected the env marker with the wrong value shape — an internal integration contract violation rather than bad user BAML code.

Source

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

                                thir,
                                run_llm_function,
                                watch_handler,
                                function_name,
                            )
                            .await?,
                        )?;

                        let key = match key_val {
                            BamlValueWithMeta::String(value, _) => value,
                            _ => bail!("env.get argument must be a string"),
                        };

                        let env_map = lookup(scopes, "__env_vars__")
                            .ok_or_else(|| anyhow!("environment context missing"))?;

                        let map = match env_map {
                            BamlValueWithMeta::Map(ref entries, _) => entries,
                            _ => bail!("environment context corrupted"),
                        };

                        if let Some(value) = map.get(&key) {
                            return Ok(EvalValue::Value(value.clone()));
                        } else {
                            bail!("Environment variable '{}' not found", key);
                        }
                    }
                }

                let callee = evaluate_expr(
                    func,
                    scopes,
                    thir,
                    run_llm_function,
                    watch_handler,
                    function_name,
                )

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Inject `__env_vars__` specifically as a Map<String, BamlValue> of env entries
  2. Fix the host stub/test to use the map constructor for the env snapshot
  3. Check the baml-compiler version's expected env-context shape and align the injector
  4. Avoid manually overriding `__env_vars__` if a runtime helper exists for it

Example fix

// host-side before
scope.declare("__env_vars__", BamlValue::String("A=1")); // wrong shape
// after
let env: HashMap<String, BamlValue> =
    std::env::vars().map(|(k, v)| (k, BamlValue::String(v))).collect();
scope.declare("__env_vars__", BamlValue::Map(env));
Defensive patterns

Strategy: type-guard

Validate before calling

// host-side: ensure the injected env snapshot is a Map
assert matches!(scope.lookup("__env_vars__"), Some(BamlValue::Map(_)));

Type guard

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

Try / catch

match result {
    Err(e) if e.to_string().contains("environment context corrupted") => {
        // re-inject __env_vars__ as a proper Map
    }
    other => other?,
}

Prevention

When it happens

Trigger: Host code declares `__env_vars__` with a List/String/other non-Map value, then interpreted BAML calls `env.get`.

Common situations: Custom runners storing the env snapshot with the wrong constructor; refactors changing BamlValue shapes without updating env injection; tests stubbing `__env_vars__` incorrectly.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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