{"record":{"id":"71dbf2b4ad82f668","repo":"BoundaryML/baml","slug":"environment-context-corrupted","errorCode":null,"errorMessage":"environment context corrupted","messagePattern":"environment context corrupted","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1793,"sourceCode":"                                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,\n                    run_llm_function,\n                    watch_handler,\n                    function_name,\n                )","sourceCodeStart":1775,"sourceCodeEnd":1811,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1775-L1811","documentation":"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.","triggerScenarios":"Host code declares `__env_vars__` with a List/String/other non-Map value, then interpreted BAML calls `env.get`.","commonSituations":"Custom runners storing the env snapshot with the wrong constructor; refactors changing BamlValue shapes without updating env injection; tests stubbing `__env_vars__` incorrectly.","solutions":["Inject `__env_vars__` specifically as a Map<String, BamlValue> of env entries","Fix the host stub/test to use the map constructor for the env snapshot","Check the baml-compiler version's expected env-context shape and align the injector","Avoid manually overriding `__env_vars__` if a runtime helper exists for it"],"exampleFix":"// host-side before\nscope.declare(\"__env_vars__\", BamlValue::String(\"A=1\")); // wrong shape\n// after\nlet env: HashMap<String, BamlValue> =\n    std::env::vars().map(|(k, v)| (k, BamlValue::String(v))).collect();\nscope.declare(\"__env_vars__\", BamlValue::Map(env));","handlingStrategy":"type-guard","validationCode":"// host-side: ensure the injected env snapshot is a Map\nassert matches!(scope.lookup(\"__env_vars__\"), Some(BamlValue::Map(_)));","typeGuard":"fn is_env_map(v: &BamlValue) -> bool {\n    matches!(v, BamlValue::Map(_))\n}","tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"environment context corrupted\") => {\n        // re-inject __env_vars__ as a proper Map\n    }\n    other => other?,\n}","preventionTips":["Inject __env_vars__ only as a Map<String, BamlValue>","Keep env-injection helpers in one shared place","Add unit tests for the env-context injection contract"],"tags":["baml","env","interpreter","corrupted-state"],"backgroundTag":"internal-invariant-violation","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"}