{"record":{"id":"f6036367afaab60a","repo":"BoundaryML/baml","slug":"environment-variable-not-found","errorCode":null,"errorMessage":"Environment variable '{}' not found","messagePattern":"Environment variable '(.+?)' not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1799,"sourceCode":"                        )?;\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                )\n                .await?;\n                let (arity, body, meta) = match callee {\n                    EvalValue::Function(a, b, m) => (a, b, m),\n                    _ => bail!(\"attempted to call non-function\"),\n                };\n","sourceCodeStart":1781,"sourceCodeEnd":1817,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1781-L1817","documentation":"The BAML interpreter evaluates an Expr::Var identifier by looking it up in the current environment scope map. When the key is absent from every scope it throws \"Environment variable '{}' not found\", meaning an identifier was referenced that was never declared or bound in the visible scope. This is the interpreter's equivalent of an unresolved-name/undefined-variable error.","triggerScenarios":"Evaluating a BAML expression that references a variable name not present in any active scope (e.g. a typo, use-before-declaration, or a variable only available at compile/check time but missing in the interpreter's environment map during evaluate_expr_with_context).","commonSituations":"Renaming a variable in a .baml file but missing a usage; referencing a function parameter from outside its scope; runtime interpretation (tests, preview) of code that type-checks only partially; interpreter not yet supporting some bindings the compiled path provides.","solutions":["Check the variable name in the error message against your .baml file for typos and fix the identifier","Ensure the variable is declared/assigned before the expression that references it","Verify the variable is in scope (not referenced outside the function/let block that defines it)","If it is a builtin or externally provided value, confirm the interpreter version supports binding it; upgrade baml"],"exampleFix":"// before\nlet result = undefined_var + 1\n// after\nlet undefined_var = 5\nlet result = undefined_var + 1","handlingStrategy":"validation","validationCode":"// Pre-check identifiers used in a BAML expression against declared bindings\nfunction validateIdentifiers(exprVars: string[], declared: Set<string>): string[] {\n  return exprVars.filter(v => !declared.has(v)); // empty means safe to evaluate\n}","typeGuard":"function isBound(name: string, scopes: Record<string, unknown>[]): boolean {\n  return scopes.some(s => Object.prototype.hasOwnProperty.call(s, name));\n}","tryCatchPattern":"try {\n  const result = evalBaml(expr, env);\n} catch (e) {\n  if (String(e).includes(\"Environment variable\")) {\n    const name = /'([^']+)'/.exec(String(e))?.[1];\n    throw new Error(`Undeclared BAML variable '${name}' — declare it before use`);\n  }\n  throw e;\n}","preventionTips":["Run the BAML type checker before interpretation to catch undeclared identifiers","Use an editor plugin/LSP for .baml files to flag unknown names","Grep for renamed variables after refactors to find stale references","Keep function parameters explicit and avoid relying on ambient bindings"],"tags":["interpreter","undefined-variable","scope"],"backgroundTag":"resource-not-found","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}