BoundaryML/baml · error

environment context missing

Error message

environment context missing

What it means

The interpreter reads environment variables from a reserved `__env_vars__` scope entry that the host must seed before running interpreted code. If `lookup(scopes, "__env_vars__")` returns `None`, it bails with `environment context missing` — meaning `env.get` was called but no environment snapshot was injected.

Source

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

                        let key_val = expect_value(
                            evaluate_expr(
                                &args[0],
                                scopes,
                                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,

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Seed the scope with `__env_vars__` as a Map of environment strings before evaluation
  2. Use the standard BAML runtime entry points that inject the environment context automatically
  3. If `env.get` is unnecessary, remove the call to avoid the env-context dependency
  4. Update the host integration to match the current baml-compiler scope setup API

Example fix

// host-side (Rust) before
let scopes = vec![Scope::new()]; // no env injected
// after
let mut 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: try-catch

Validate before calling

// host-side: verify env context exists before running BAML that uses env.get
assert!(scope.lookup("__env_vars__").is_some(), "env context must be seeded");

Type guard

fn env_context_seeded(scopes: &[Scope]) -> bool {
    scopes.iter().any(|s| s.variables.contains_key("__env_vars__"))
}

Try / catch

match result {
    Err(e) if e.to_string().contains("environment context missing") => {
        // inject __env_vars__ map and re-run
    }
    other => other?,
}

Prevention

When it happens

Trigger: Evaluating BAML code via the interpreter without the host setting `__env_vars__` in the initial scope; running in a context (REPL, test harness) that skips env-context setup.

Common situations: Embedding the baml-compiler interpreter in a custom runner that forgot to populate env vars; tests that bypass the normal CLI/runtime bootstrap; a version change altering how env context is injected.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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