{"record":{"id":"2dba950302f64224","repo":"BoundaryML/baml","slug":"environment-context-missing","errorCode":null,"errorMessage":"environment context missing","messagePattern":"environment context missing","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1789,"sourceCode":"                        let key_val = expect_value(\n                            evaluate_expr(\n                                &args[0],\n                                scopes,\n                                thir,\n                                run_llm_function,\n                                watch_handler,\n                                function_name,\n                            )\n                            .await?,\n                        )?;\n\n                        let key = match key_val {\n                            BamlValueWithMeta::String(value, _) => value,\n                            _ => bail!(\"env.get argument must be a string\"),\n                        };\n\n                        let env_map = lookup(scopes, \"__env_vars__\")\n                            .ok_or_else(|| anyhow!(\"environment context missing\"))?;\n\n                        let map = match env_map {\n                            BamlValueWithMeta::Map(ref entries, _) => entries,\n                            _ => bail!(\"environment context corrupted\"),\n                        };\n\n                        if let Some(value) = map.get(&key) {\n                            return Ok(EvalValue::Value(value.clone()));\n                        } else {\n                            bail!(\"Environment variable '{}' not found\", key);\n                        }\n                    }\n                }\n\n                let callee = evaluate_expr(\n                    func,\n                    scopes,\n                    thir,","sourceCodeStart":1771,"sourceCodeEnd":1807,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1771-L1807","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Seed the scope with `__env_vars__` as a Map of environment strings before evaluation","Use the standard BAML runtime entry points that inject the environment context automatically","If `env.get` is unnecessary, remove the call to avoid the env-context dependency","Update the host integration to match the current baml-compiler scope setup API"],"exampleFix":"// host-side (Rust) before\nlet scopes = vec![Scope::new()]; // no env injected\n// after\nlet mut env: HashMap<String, BamlValue> = std::env::vars()\n    .map(|(k, v)| (k, BamlValue::String(v)))\n    .collect();\nscope.declare(\"__env_vars__\", BamlValue::Map(env));","handlingStrategy":"try-catch","validationCode":"// host-side: verify env context exists before running BAML that uses env.get\nassert!(scope.lookup(\"__env_vars__\").is_some(), \"env context must be seeded\");","typeGuard":"fn env_context_seeded(scopes: &[Scope]) -> bool {\n    scopes.iter().any(|s| s.variables.contains_key(\"__env_vars__\"))\n}","tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"environment context missing\") => {\n        // inject __env_vars__ map and re-run\n    }\n    other => other?,\n}","preventionTips":["Always seed __env_vars__ in custom interpreters","Use standard runtime entry points that inject env automatically","Add a startup assertion that the env context is present"],"tags":["baml","env","interpreter","missing-context"],"backgroundTag":"missing-env-var","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}